Icon.jsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import {Component} from 'react';
  2. import cls from 'classnames';
  3. import iconArrowRight from '../assets/icon-arrow-right.svg';
  4. import iconPin from '../assets/icon-pin.svg';
  5. import s from './Icon.css';
  6. const ICONS = {
  7. 'arrow-right': {
  8. src: iconArrowRight,
  9. size: [7, 13],
  10. },
  11. pin: {
  12. src: iconPin,
  13. size: [12, 18],
  14. },
  15. };
  16. export default class Icon extends Component {
  17. get style() {
  18. const {name, size, rotate} = this.props;
  19. const icon = ICONS[name];
  20. if (!icon) {
  21. throw new TypeError(`Can't find "${name}" icon.`);
  22. }
  23. let [width, height] = icon.size;
  24. if (size) {
  25. const ratio = size / Math.max(width, height);
  26. width = Math.min(Math.ceil(width * ratio), size);
  27. height = Math.min(Math.ceil(height * ratio), size);
  28. }
  29. return {
  30. backgroundImage: `url(${icon.src})`,
  31. width: `${width}px`,
  32. height: `${height}px`,
  33. transform: rotate ? `rotate(${rotate}deg)` : '',
  34. };
  35. }
  36. render() {
  37. const {className} = this.props;
  38. return <i className={cls(s.icon, className)} style={this.style} />;
  39. }
  40. }