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.

91 lines
2.1 KiB

  1. import React from 'react';
  2. import { Motion, spring } from 'react-motion';
  3. import PropTypes from 'prop-types';
  4. class IconButton extends React.PureComponent {
  5. static propTypes = {
  6. className: PropTypes.string,
  7. title: PropTypes.string.isRequired,
  8. icon: PropTypes.string.isRequired,
  9. onClick: PropTypes.func,
  10. size: PropTypes.number,
  11. active: PropTypes.bool,
  12. style: PropTypes.object,
  13. activeStyle: PropTypes.object,
  14. disabled: PropTypes.bool,
  15. inverted: PropTypes.bool,
  16. animate: PropTypes.bool,
  17. overlay: PropTypes.bool
  18. };
  19. static defaultProps = {
  20. size: 18,
  21. active: false,
  22. disabled: false,
  23. animate: false,
  24. overlay: false
  25. };
  26. handleClick = (e) => {
  27. e.preventDefault();
  28. if (!this.props.disabled) {
  29. this.props.onClick(e);
  30. }
  31. }
  32. render () {
  33. let style = {
  34. fontSize: `${this.props.size}px`,
  35. width: `${this.props.size * 1.28571429}px`,
  36. height: `${this.props.size * 1.28571429}px`,
  37. lineHeight: `${this.props.size}px`,
  38. ...this.props.style
  39. };
  40. if (this.props.active) {
  41. style = { ...style, ...this.props.activeStyle };
  42. }
  43. const classes = ['icon-button'];
  44. if (this.props.active) {
  45. classes.push('active');
  46. }
  47. if (this.props.disabled) {
  48. classes.push('disabled');
  49. }
  50. if (this.props.inverted) {
  51. classes.push('inverted');
  52. }
  53. if (this.props.overlay) {
  54. classes.push('overlayed');
  55. }
  56. if (this.props.className) {
  57. classes.push(this.props.className)
  58. }
  59. return (
  60. <Motion defaultStyle={{ rotate: this.props.active ? -360 : 0 }} style={{ rotate: this.props.animate ? spring(this.props.active ? -360 : 0, { stiffness: 120, damping: 7 }) : 0 }}>
  61. {({ rotate }) =>
  62. <button
  63. aria-label={this.props.title}
  64. title={this.props.title}
  65. className={classes.join(' ')}
  66. onClick={this.handleClick}
  67. style={style}>
  68. <i style={{ transform: `rotate(${rotate}deg)` }} className={`fa fa-fw fa-${this.props.icon}`} aria-hidden='true' />
  69. </button>
  70. }
  71. </Motion>
  72. );
  73. }
  74. }
  75. export default IconButton;