react-toolbox/components/ripple/index.jsx

68 lines
1.9 KiB
React
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
2015-10-23 02:26:12 +03:00
import style from './style';
2015-10-22 02:31:17 +03:00
class Ripple extends React.Component {
static propTypes = {
2015-10-04 16:12:53 +03:00
centered: React.PropTypes.bool,
className: React.PropTypes.string,
2015-10-04 16:12:53 +03:00
loading: React.PropTypes.bool,
spread: React.PropTypes.number
};
static defaultProps = {
centered: false,
className: '',
loading: false,
spread: 2
};
state = {
active: false,
restarting: false,
top: null,
left: null,
width: null
};
2015-10-22 02:31:17 +03:00
start = ({ pageX, pageY }) => {
document.addEventListener('mouseup', this.handleEnd);
const {top, left, width} = this._getDescriptor(pageX, pageY);
this.setState({active: false, restarting: true, width: 0}, () => {
this.refs.ripple.offsetWidth; //eslint-disable-line no-unused-expressions
this.setState({active: true, restarting: false, top, left, width});
});
2015-10-22 02:31:17 +03:00
};
2015-10-22 02:31:17 +03:00
handleEnd = () => {
document.removeEventListener('mouseup', this.handleEnd);
this.setState({active: false});
2015-10-22 02:31:17 +03:00
};
_getDescriptor (pageX, pageY) {
const { left, top, height, width } = ReactDOM.findDOMNode(this).getBoundingClientRect();
return {
2015-10-04 16:12:53 +03:00
left: this.props.centered ? width / 2 : pageX - left,
top: this.props.centered ? height / 2 : pageY - top,
width: width * this.props.spread
};
}
render () {
const { left, top, width } = this.state;
const rippleStyle = {left, top, width, height: width};
2015-10-04 16:12:53 +03:00
let className = style[this.props.loading ? 'loading' : 'normal'];
if (this.state.active) className += ` ${style.active}`;
if (this.state.restarting) className += ` ${style.restarting}`;
2015-10-04 16:12:53 +03:00
if (this.props.className) className += ` ${this.props.className}`;
return (
2015-10-06 05:42:56 +03:00
<span data-react-toolbox='ripple' className={style.wrapper}>
2015-10-04 16:12:53 +03:00
<span ref="ripple" role='ripple' className={className} style={rippleStyle} />
</span>
);
}
2015-10-21 13:25:07 +03:00
}
2015-10-22 02:31:17 +03:00
export default Ripple;