The code powering m.abunchtell.com https://m.abunchtell.com
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

96 行
2.4 KiB

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