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.
138 lines
3.2 KiB
138 lines
3.2 KiB
![]()
7 years ago
|
import React from 'react';
|
||
![]()
7 years ago
|
import Motion from 'flavours/glitch/util/optional_motion';
|
||
![]()
7 years ago
|
import spring from 'react-motion/lib/spring';
|
||
![]()
7 years ago
|
import PropTypes from 'prop-types';
|
||
![]()
7 years ago
|
import classNames from 'classnames';
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
export default class IconButton extends React.PureComponent {
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
static propTypes = {
|
||
|
className: PropTypes.string,
|
||
|
title: PropTypes.string.isRequired,
|
||
|
icon: PropTypes.string.isRequired,
|
||
|
onClick: PropTypes.func,
|
||
|
size: PropTypes.number,
|
||
|
active: PropTypes.bool,
|
||
![]()
7 years ago
|
pressed: PropTypes.bool,
|
||
![]()
7 years ago
|
expanded: PropTypes.bool,
|
||
![]()
7 years ago
|
style: PropTypes.object,
|
||
|
activeStyle: PropTypes.object,
|
||
|
disabled: PropTypes.bool,
|
||
|
inverted: PropTypes.bool,
|
||
|
animate: PropTypes.bool,
|
||
![]()
7 years ago
|
flip: PropTypes.bool,
|
||
![]()
7 years ago
|
overlay: PropTypes.bool,
|
||
![]()
7 years ago
|
tabIndex: PropTypes.string,
|
||
![]()
7 years ago
|
label: PropTypes.string,
|
||
![]()
7 years ago
|
};
|
||
|
|
||
|
static defaultProps = {
|
||
|
size: 18,
|
||
|
active: false,
|
||
|
disabled: false,
|
||
|
animate: false,
|
||
![]()
7 years ago
|
overlay: false,
|
||
![]()
7 years ago
|
tabIndex: '0',
|
||
![]()
7 years ago
|
};
|
||
|
|
||
|
handleClick = (e) => {
|
||
![]()
8 years ago
|
e.preventDefault();
|
||
![]()
8 years ago
|
|
||
|
if (!this.props.disabled) {
|
||
![]()
7 years ago
|
this.props.onClick(e);
|
||
![]()
8 years ago
|
}
|
||
![]()
7 years ago
|
}
|
||
![]()
8 years ago
|
|
||
|
render () {
|
||
![]()
7 years ago
|
let style = {
|
||
![]()
8 years ago
|
fontSize: `${this.props.size}px`,
|
||
![]()
7 years ago
|
height: `${this.props.size * 1.28571429}px`,
|
||
![]()
8 years ago
|
lineHeight: `${this.props.size}px`,
|
||
![]()
7 years ago
|
...this.props.style,
|
||
![]()
7 years ago
|
...(this.props.active ? this.props.activeStyle : {}),
|
||
![]()
8 years ago
|
};
|
||
![]()
7 years ago
|
if (!this.props.label) {
|
||
|
style.width = `${this.props.size * 1.28571429}px`;
|
||
|
} else {
|
||
|
style.textAlign = 'left';
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
7 years ago
|
const {
|
||
|
active,
|
||
|
animate,
|
||
|
className,
|
||
|
disabled,
|
||
|
expanded,
|
||
|
icon,
|
||
|
inverted,
|
||
![]()
7 years ago
|
flip,
|
||
![]()
7 years ago
|
overlay,
|
||
|
pressed,
|
||
|
tabIndex,
|
||
|
title,
|
||
|
} = this.props;
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
const classes = classNames(className, 'icon-button', {
|
||
|
active,
|
||
|
disabled,
|
||
|
inverted,
|
||
|
overlayed: overlay,
|
||
|
});
|
||
![]()
7 years ago
|
|
||
![]()
7 years ago
|
const flipDeg = flip ? -180 : -360;
|
||
|
const rotateDeg = active ? flipDeg : 0;
|
||
![]()
7 years ago
|
|
||
|
const motionDefaultStyle = {
|
||
|
rotate: rotateDeg,
|
||
|
};
|
||
|
|
||
|
const springOpts = {
|
||
|
stiffness: this.props.flip ? 60 : 120,
|
||
|
damping: 7,
|
||
|
};
|
||
|
const motionStyle = {
|
||
![]()
7 years ago
|
rotate: animate ? spring(rotateDeg, springOpts) : 0,
|
||
![]()
7 years ago
|
};
|
||
|
|
||
![]()
7 years ago
|
if (!animate) {
|
||
|
// Perf optimization: avoid unnecessary <Motion> components unless
|
||
|
// we actually need to animate.
|
||
|
return (
|
||
|
<button
|
||
|
aria-label={title}
|
||
|
aria-pressed={pressed}
|
||
|
aria-expanded={expanded}
|
||
|
title={title}
|
||
|
className={classes}
|
||
|
onClick={this.handleClick}
|
||
|
style={style}
|
||
|
tabIndex={tabIndex}
|
||
|
>
|
||
|
<i className={`fa fa-fw fa-${icon}`} aria-hidden='true' />
|
||
|
</button>
|
||
|
);
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
return (
|
||
![]()
7 years ago
|
<Motion defaultStyle={motionDefaultStyle} style={motionStyle}>
|
||
![]()
8 years ago
|
{({ rotate }) =>
|
||
|
<button
|
||
![]()
7 years ago
|
aria-label={title}
|
||
|
aria-pressed={pressed}
|
||
|
aria-expanded={expanded}
|
||
|
title={title}
|
||
|
className={classes}
|
||
![]()
8 years ago
|
onClick={this.handleClick}
|
||
![]()
7 years ago
|
style={style}
|
||
![]()
7 years ago
|
tabIndex={tabIndex}
|
||
![]()
7 years ago
|
>
|
||
![]()
7 years ago
|
<i style={{ transform: `rotate(${rotate}deg)` }} className={`fa fa-fw fa-${icon}`} aria-hidden='true' />
|
||
![]()
7 years ago
|
{this.props.label}
|
||
![]()
8 years ago
|
</button>
|
||
|
}
|
||
|
</Motion>
|
||
![]()
8 years ago
|
);
|
||
|
}
|
||
|
|
||
![]()
7 years ago
|
}
|