-
-
Notifications
You must be signed in to change notification settings - Fork 510
Expand file tree
/
Copy pathIcon.jsx
More file actions
64 lines (54 loc) · 1.46 KB
/
Icon.jsx
File metadata and controls
64 lines (54 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cls from "classnames";
import PropTypes from "prop-types";
import iconArrowRight from "../assets/icon-arrow-right.svg";
import iconMoon from "../assets/icon-moon.svg";
import iconPin from "../assets/icon-pin.svg";
import iconSun from "../assets/icon-sun.svg";
import PureComponent from "../lib/PureComponent.jsx";
import * as styles from "./Icon.css";
const ICONS = {
"arrow-right": {
src: iconArrowRight,
size: [7, 13],
},
pin: {
src: iconPin,
size: [12, 18],
},
moon: {
src: iconMoon,
size: [24, 24],
},
sun: {
src: iconSun,
size: [24, 24],
},
};
export default class Icon extends PureComponent {
static propTypes = {
className: PropTypes.string,
name: PropTypes.string.isRequired,
size: PropTypes.number,
rotate: PropTypes.number,
};
render({ className }) {
return <i className={cls(styles.icon, className)} style={this.style} />;
}
get style() {
const { name, size, rotate } = this.props;
const icon = ICONS[name];
if (!icon) throw new TypeError(`Can't find "${name}" icon.`);
let [width, height] = icon.size;
if (size) {
const ratio = size / Math.max(width, height);
width = Math.min(Math.ceil(width * ratio), size);
height = Math.min(Math.ceil(height * ratio), size);
}
return {
backgroundImage: `url(${icon.src})`,
width: `${width}px`,
height: `${height}px`,
transform: rotate ? `rotate(${rotate}deg)` : "",
};
}
}