You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.9 KiB

  1. import PureRenderMixin from 'react-addons-pure-render-mixin';
  2. import { Motion, spring } from 'react-motion';
  3. const IconButton = React.createClass({
  4. propTypes: {
  5. title: React.PropTypes.string.isRequired,
  6. icon: React.PropTypes.string.isRequired,
  7. onClick: React.PropTypes.func,
  8. size: React.PropTypes.number,
  9. active: React.PropTypes.bool,
  10. style: React.PropTypes.object,
  11. activeStyle: React.PropTypes.object,
  12. disabled: React.PropTypes.bool,
  13. inverted: React.PropTypes.bool,
  14. animate: React.PropTypes.bool
  15. },
  16. getDefaultProps () {
  17. return {
  18. size: 18,
  19. active: false,
  20. disabled: false,
  21. animate: false
  22. };
  23. },
  24. mixins: [PureRenderMixin],
  25. handleClick (e) {
  26. e.preventDefault();
  27. if (!this.props.disabled) {
  28. this.props.onClick();
  29. }
  30. },
  31. render () {
  32. let style = {
  33. fontSize: `${this.props.size}px`,
  34. width: `${this.props.size * 1.28571429}px`,
  35. height: `${this.props.size}px`,
  36. lineHeight: `${this.props.size}px`,
  37. ...this.props.style
  38. };
  39. if (this.props.active) {
  40. style = { ...style, ...this.props.activeStyle };
  41. }
  42. return (
  43. <Motion defaultStyle={{ rotate: this.props.active ? -360 : 0 }} style={{ rotate: this.props.animate ? spring(this.props.active ? -360 : 0, { stiffness: 120, damping: 7 }) : 0 }}>
  44. {({ rotate }) =>
  45. <button
  46. aria-label={this.props.title}
  47. title={this.props.title}
  48. className={`icon-button ${this.props.active ? 'active' : ''} ${this.props.disabled ? 'disabled' : ''} ${this.props.inverted ? 'inverted' : ''}`}
  49. onClick={this.handleClick}
  50. style={style}>
  51. <i style={{ transform: `rotate(${rotate}deg)` }} className={`fa fa-fw fa-${this.props.icon}`} aria-hidden='true' />
  52. </button>
  53. }
  54. </Motion>
  55. );
  56. }
  57. });
  58. export default IconButton;