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.
97 lines
2.2 KiB
97 lines
2.2 KiB
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import { Motion, spring } from 'react-motion';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
8 years ago
|
|
||
8 years ago
|
class IconButton extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
constructor (props, context) {
|
||
|
super(props, context);
|
||
|
this.handleClick = this.handleClick.bind(this);
|
||
|
}
|
||
8 years ago
|
|
||
|
handleClick (e) {
|
||
|
e.preventDefault();
|
||
8 years ago
|
|
||
|
if (!this.props.disabled) {
|
||
8 years ago
|
this.props.onClick(e);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
|
render () {
|
||
8 years ago
|
let style = {
|
||
8 years ago
|
fontSize: `${this.props.size}px`,
|
||
8 years ago
|
width: `${this.props.size * 1.28571429}px`,
|
||
8 years ago
|
height: `${this.props.size * 1.28571429}px`,
|
||
8 years ago
|
lineHeight: `${this.props.size}px`,
|
||
8 years ago
|
...this.props.style
|
||
8 years ago
|
};
|
||
|
|
||
8 years ago
|
if (this.props.active) {
|
||
|
style = { ...style, ...this.props.activeStyle };
|
||
|
}
|
||
|
|
||
8 years ago
|
const classes = ['icon-button'];
|
||
|
|
||
|
if (this.props.active) {
|
||
|
classes.push('active');
|
||
|
}
|
||
|
|
||
|
if (this.props.disabled) {
|
||
|
classes.push('disabled');
|
||
|
}
|
||
|
|
||
|
if (this.props.inverted) {
|
||
|
classes.push('inverted');
|
||
|
}
|
||
|
|
||
|
if (this.props.overlay) {
|
||
|
classes.push('overlayed');
|
||
|
}
|
||
|
|
||
8 years ago
|
if (this.props.className) {
|
||
|
classes.push(this.props.className)
|
||
|
}
|
||
|
|
||
8 years ago
|
return (
|
||
8 years ago
|
<Motion defaultStyle={{ rotate: this.props.active ? -360 : 0 }} style={{ rotate: this.props.animate ? spring(this.props.active ? -360 : 0, { stiffness: 120, damping: 7 }) : 0 }}>
|
||
|
{({ rotate }) =>
|
||
|
<button
|
||
|
aria-label={this.props.title}
|
||
|
title={this.props.title}
|
||
8 years ago
|
className={classes.join(' ')}
|
||
8 years ago
|
onClick={this.handleClick}
|
||
|
style={style}>
|
||
|
<i style={{ transform: `rotate(${rotate}deg)` }} className={`fa fa-fw fa-${this.props.icon}`} aria-hidden='true' />
|
||
|
</button>
|
||
|
}
|
||
|
</Motion>
|
||
8 years ago
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
}
|
||
|
|
||
|
IconButton.propTypes = {
|
||
8 years ago
|
className: PropTypes.string,
|
||
8 years ago
|
title: PropTypes.string.isRequired,
|
||
|
icon: PropTypes.string.isRequired,
|
||
|
onClick: PropTypes.func,
|
||
|
size: PropTypes.number,
|
||
|
active: PropTypes.bool,
|
||
|
style: PropTypes.object,
|
||
|
activeStyle: PropTypes.object,
|
||
|
disabled: PropTypes.bool,
|
||
|
inverted: PropTypes.bool,
|
||
|
animate: PropTypes.bool,
|
||
|
overlay: PropTypes.bool
|
||
|
};
|
||
|
|
||
|
IconButton.defaultProps = {
|
||
|
size: 18,
|
||
|
active: false,
|
||
|
disabled: false,
|
||
|
animate: false,
|
||
|
overlay: false
|
||
|
};
|
||
8 years ago
|
|
||
|
export default IconButton;
|