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.
165 lines
3.8 KiB
165 lines
3.8 KiB
8 years ago
|
import React from 'react';
|
||
8 years ago
|
import PropTypes from 'prop-types';
|
||
7 years ago
|
import classNames from 'classnames';
|
||
6 years ago
|
import Icon from 'mastodon/components/icon';
|
||
4 years ago
|
import AnimatedNumber from 'mastodon/components/animated_number';
|
||
8 years ago
|
|
||
8 years ago
|
export default class IconButton extends React.PureComponent {
|
||
8 years ago
|
|
||
8 years ago
|
static propTypes = {
|
||
|
className: PropTypes.string,
|
||
|
title: PropTypes.string.isRequired,
|
||
|
icon: PropTypes.string.isRequired,
|
||
|
onClick: PropTypes.func,
|
||
5 years ago
|
onMouseDown: PropTypes.func,
|
||
|
onKeyDown: PropTypes.func,
|
||
5 years ago
|
onKeyPress: PropTypes.func,
|
||
8 years ago
|
size: PropTypes.number,
|
||
|
active: PropTypes.bool,
|
||
7 years ago
|
expanded: PropTypes.bool,
|
||
8 years ago
|
style: PropTypes.object,
|
||
|
activeStyle: PropTypes.object,
|
||
|
disabled: PropTypes.bool,
|
||
|
inverted: PropTypes.bool,
|
||
|
animate: PropTypes.bool,
|
||
8 years ago
|
overlay: PropTypes.bool,
|
||
7 years ago
|
tabIndex: PropTypes.string,
|
||
4 years ago
|
counter: PropTypes.number,
|
||
|
obfuscateCount: PropTypes.bool,
|
||
3 years ago
|
href: PropTypes.string,
|
||
2 years ago
|
ariaHidden: PropTypes.bool,
|
||
8 years ago
|
};
|
||
|
|
||
|
static defaultProps = {
|
||
|
size: 18,
|
||
|
active: false,
|
||
|
disabled: false,
|
||
|
animate: false,
|
||
8 years ago
|
overlay: false,
|
||
7 years ago
|
tabIndex: '0',
|
||
2 years ago
|
ariaHidden: false,
|
||
8 years ago
|
};
|
||
|
|
||
5 years ago
|
state = {
|
||
|
activate: false,
|
||
|
deactivate: false,
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
|
componentWillReceiveProps (nextProps) {
|
||
|
if (!nextProps.animate) return;
|
||
|
|
||
|
if (this.props.active && !nextProps.active) {
|
||
|
this.setState({ activate: false, deactivate: true });
|
||
|
} else if (!this.props.active && nextProps.active) {
|
||
|
this.setState({ activate: true, deactivate: false });
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
handleClick = (e) => {
|
||
8 years ago
|
e.preventDefault();
|
||
8 years ago
|
|
||
|
if (!this.props.disabled) {
|
||
8 years ago
|
this.props.onClick(e);
|
||
8 years ago
|
}
|
||
2 years ago
|
};
|
||
8 years ago
|
|
||
5 years ago
|
handleKeyPress = (e) => {
|
||
|
if (this.props.onKeyPress && !this.props.disabled) {
|
||
|
this.props.onKeyPress(e);
|
||
|
}
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
5 years ago
|
handleMouseDown = (e) => {
|
||
|
if (!this.props.disabled && this.props.onMouseDown) {
|
||
|
this.props.onMouseDown(e);
|
||
|
}
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
|
handleKeyDown = (e) => {
|
||
|
if (!this.props.disabled && this.props.onKeyDown) {
|
||
|
this.props.onKeyDown(e);
|
||
|
}
|
||
2 years ago
|
};
|
||
5 years ago
|
|
||
8 years ago
|
render () {
|
||
8 years ago
|
const 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
|
...(this.props.active ? this.props.activeStyle : {}),
|
||
8 years ago
|
};
|
||
|
|
||
7 years ago
|
const {
|
||
|
active,
|
||
|
className,
|
||
|
disabled,
|
||
|
expanded,
|
||
|
icon,
|
||
|
inverted,
|
||
|
overlay,
|
||
|
tabIndex,
|
||
|
title,
|
||
4 years ago
|
counter,
|
||
|
obfuscateCount,
|
||
3 years ago
|
href,
|
||
2 years ago
|
ariaHidden,
|
||
7 years ago
|
} = this.props;
|
||
8 years ago
|
|
||
5 years ago
|
const {
|
||
|
activate,
|
||
|
deactivate,
|
||
|
} = this.state;
|
||
|
|
||
7 years ago
|
const classes = classNames(className, 'icon-button', {
|
||
|
active,
|
||
|
disabled,
|
||
|
inverted,
|
||
5 years ago
|
activate,
|
||
|
deactivate,
|
||
7 years ago
|
overlayed: overlay,
|
||
4 years ago
|
'icon-button--with-counter': typeof counter !== 'undefined',
|
||
7 years ago
|
});
|
||
8 years ago
|
|
||
4 years ago
|
if (typeof counter !== 'undefined') {
|
||
|
style.width = 'auto';
|
||
|
}
|
||
|
|
||
3 years ago
|
let contents = (
|
||
|
<React.Fragment>
|
||
|
<Icon id={icon} fixedWidth aria-hidden='true' /> {typeof counter !== 'undefined' && <span className='icon-button__counter'><AnimatedNumber value={counter} obfuscate={obfuscateCount} /></span>}
|
||
|
</React.Fragment>
|
||
|
);
|
||
|
|
||
2 years ago
|
if (href && !this.prop) {
|
||
|
contents = (
|
||
|
<a href={href} target='_blank' rel='noopener noreferrer'>
|
||
3 years ago
|
{contents}
|
||
|
</a>
|
||
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
return (
|
||
5 years ago
|
<button
|
||
2 years ago
|
type='button'
|
||
5 years ago
|
aria-label={title}
|
||
|
aria-expanded={expanded}
|
||
2 years ago
|
aria-hidden={ariaHidden}
|
||
5 years ago
|
title={title}
|
||
|
className={classes}
|
||
|
onClick={this.handleClick}
|
||
|
onMouseDown={this.handleMouseDown}
|
||
|
onKeyDown={this.handleKeyDown}
|
||
|
onKeyPress={this.handleKeyPress}
|
||
|
style={style}
|
||
|
tabIndex={tabIndex}
|
||
|
disabled={disabled}
|
||
|
>
|
||
3 years ago
|
{contents}
|
||
5 years ago
|
</button>
|
||
8 years ago
|
);
|
||
|
}
|
||
|
|
||
8 years ago
|
}
|