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.

96 lines
2.2 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. constructor (props, context) {
  6. super(props, context);
  7. this.handleClick = this.handleClick.bind(this);
  8. }
  9. handleClick (e) {
  10. e.preventDefault();
  11. if (!this.props.disabled) {
  12. this.props.onClick(e);
  13. }
  14. }
  15. render () {
  16. let style = {
  17. fontSize: `${this.props.size}px`,
  18. width: `${this.props.size * 1.28571429}px`,
  19. height: `${this.props.size * 1.28571429}px`,
  20. lineHeight: `${this.props.size}px`,
  21. ...this.props.style
  22. };
  23. if (this.props.active) {
  24. style = { ...style, ...this.props.activeStyle };
  25. }
  26. const classes = ['icon-button'];
  27. if (this.props.active) {
  28. classes.push('active');
  29. }
  30. if (this.props.disabled) {
  31. classes.push('disabled');
  32. }
  33. if (this.props.inverted) {
  34. classes.push('inverted');
  35. }
  36. if (this.props.overlay) {
  37. classes.push('overlayed');
  38. }
  39. if (this.props.className) {
  40. classes.push(this.props.className)
  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={classes.join(' ')}
  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. IconButton.propTypes = {
  59. className: PropTypes.string,
  60. title: PropTypes.string.isRequired,
  61. icon: PropTypes.string.isRequired,
  62. onClick: PropTypes.func,
  63. size: PropTypes.number,
  64. active: PropTypes.bool,
  65. style: PropTypes.object,
  66. activeStyle: PropTypes.object,
  67. disabled: PropTypes.bool,
  68. inverted: PropTypes.bool,
  69. animate: PropTypes.bool,
  70. overlay: PropTypes.bool
  71. };
  72. IconButton.defaultProps = {
  73. size: 18,
  74. active: false,
  75. disabled: false,
  76. animate: false,
  77. overlay: false
  78. };
  79. export default IconButton;