95 lines
1.8 KiB
JavaScript
Raw Normal View History

2020-02-05 17:45:48 -05:00
import classNames from 'classnames/bind'
export default class Button extends PureComponent {
static propTypes = {
text: PropTypes.node,
2020-02-05 17:45:48 -05:00
href: PropTypes.string,
onClick: PropTypes.func,
disabled: PropTypes.bool,
block: PropTypes.bool,
secondary: PropTypes.bool,
children: PropTypes.node,
2020-02-05 17:45:48 -05:00
className: PropTypes.string,
}
state = {
hovering: false,
}
handleOnMouseEnter = () => {
this.setState({
hovering: true,
})
}
handleOnMouseLeave = () => {
this.setState({
hovering: false,
})
}
handleClick = (e) => {
if (!this.props.disabled && this.props.onClick) {
2020-02-05 17:45:48 -05:00
this.props.onClick(e)
}
}
setRef = (c) => {
2020-02-05 17:45:48 -05:00
this.node = c
}
focus() {
2020-02-05 17:45:48 -05:00
this.node.focus()
}
render () {
2020-02-05 17:45:48 -05:00
const { secondary, block, className, disabled, text, children, href } = this.props
const { hovering } = this.state
const cx = classNames.bind(styles)
const classes = cx(className, {
default: 1,
noUnderline: 1,
font: 1,
colorWhite: 1,
circle: 1,
cursorPointer: 1,
textAlignCenter: 1,
paddingVertical10PX: 1,
paddingHorizontal15PX: 1,
2020-02-08 01:12:01 -05:00
width100PC: block,
2020-02-05 17:45:48 -05:00
backgroundColorBrand: !hovering,
backgroundColorBrandDark: hovering,
})
if (href) {
return (
<a
className={classes}
href={href}
onMouseEnter={() => this.handleOnMouseEnter()}
onMouseLeave={() => this.handleOnMouseLeave()}
>
{text || children}
</a>
)
}
return (
<button
ref={this.setRef}
2020-02-05 17:45:48 -05:00
disabled={disabled}
onClick={this.handleClick}
className={classes}
onMouseEnter={() => this.handleOnMouseEnter()}
onMouseLeave={() => this.handleOnMouseLeave()}
>
2020-02-05 17:45:48 -05:00
{text || children}
</button>
);
}
}