react-toolbox/components/ripple/index.jsx

68 lines
2.0 KiB
React
Raw Normal View History

import React from 'react';
import ReactDOM from 'react-dom';
2015-10-21 09:13:24 +03:00
import autobind from 'autobind-decorator'
import style from './style.scss';
2015-10-21 09:13:24 +03:00
@autobind
export default 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
};
start ({ pageX, pageY }) {
document.addEventListener('mouseup', this.end);
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: top, left: left, width: width});
});
}
end () {
document.removeEventListener('mouseup', this.end);
this.setState({active: false});
}
_getDescriptor (pageX, pageY) {
let { 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 () {
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'];
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>
);
}
};