2015-10-11 20:07:09 +03:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
2015-10-23 02:26:12 +03:00
|
|
|
import style from './style';
|
2015-09-18 20:21:54 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
class Ripple extends React.Component {
|
2015-10-20 08:40:51 +03:00
|
|
|
static propTypes = {
|
2015-10-04 16:12:53 +03:00
|
|
|
centered: React.PropTypes.bool,
|
2015-09-18 20:21:54 +03:00
|
|
|
className: React.PropTypes.string,
|
2015-10-04 16:12:53 +03:00
|
|
|
loading: React.PropTypes.bool,
|
|
|
|
spread: React.PropTypes.number
|
2015-10-20 08:40:51 +03:00
|
|
|
};
|
2015-09-18 20:21:54 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
static defaultProps = {
|
|
|
|
centered: false,
|
|
|
|
className: '',
|
|
|
|
loading: false,
|
|
|
|
spread: 2
|
|
|
|
};
|
2015-09-18 20:21:54 +03:00
|
|
|
|
2015-10-20 08:40:51 +03:00
|
|
|
state = {
|
|
|
|
active: false,
|
|
|
|
restarting: false,
|
|
|
|
top: null,
|
|
|
|
left: null,
|
|
|
|
width: null
|
|
|
|
};
|
2015-09-18 20:21:54 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
start = ({ pageX, pageY }) => {
|
|
|
|
document.addEventListener('mouseup', this.handleEnd);
|
2015-09-18 20:21:54 +03:00
|
|
|
const {top, left, width} = this._getDescriptor(pageX, pageY);
|
|
|
|
this.setState({active: false, restarting: true, width: 0}, () => {
|
2015-10-11 20:07:09 +03:00
|
|
|
this.refs.ripple.offsetWidth; //eslint-disable-line no-unused-expressions
|
2015-09-18 20:21:54 +03:00
|
|
|
this.setState({active: true, restarting: false, top: top, left: left, width: width});
|
|
|
|
});
|
2015-10-22 02:31:17 +03:00
|
|
|
};
|
2015-09-18 20:21:54 +03:00
|
|
|
|
2015-10-22 02:31:17 +03:00
|
|
|
handleEnd = () => {
|
|
|
|
document.removeEventListener('mouseup', this.handleEnd);
|
2015-09-18 20:21:54 +03:00
|
|
|
this.setState({active: false});
|
2015-10-22 02:31:17 +03:00
|
|
|
};
|
2015-09-18 20:21:54 +03:00
|
|
|
|
|
|
|
_getDescriptor (pageX, pageY) {
|
2015-10-11 20:07:09 +03:00
|
|
|
let { left, top, height, width } = ReactDOM.findDOMNode(this).getBoundingClientRect();
|
2015-09-18 20:21:54 +03:00
|
|
|
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
|
2015-09-18 20:21:54 +03:00
|
|
|
};
|
2015-10-20 08:40:51 +03:00
|
|
|
}
|
2015-09-18 20:21:54 +03:00
|
|
|
|
|
|
|
render () {
|
|
|
|
let { left, top, width } = this.state;
|
2015-10-04 16:12:53 +03:00
|
|
|
let rippleStyle = {left: left, top: top, width: width, height: width};
|
|
|
|
let className = style[this.props.loading ? 'loading' : 'normal'];
|
2015-10-02 18:39:26 +03:00
|
|
|
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}`;
|
2015-09-18 20:21:54 +03:00
|
|
|
|
|
|
|
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} />
|
2015-09-18 20:21:54 +03:00
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
2015-10-21 13:25:07 +03:00
|
|
|
}
|
2015-10-22 02:31:17 +03:00
|
|
|
|
|
|
|
export default Ripple;
|