react-toolbox/components/overlay/index.jsx

62 lines
1.4 KiB
React
Raw Normal View History

2015-11-14 23:38:17 +03:00
import React from 'react';
import ReactDOM from 'react-dom';
import style from './style';
class Overlay extends React.Component {
static propTypes = {
active: React.PropTypes.bool,
className: React.PropTypes.string,
2015-11-15 06:17:07 +03:00
onClick: React.PropTypes.func,
opacity: React.PropTypes.number
2015-11-14 23:38:17 +03:00
};
static defaultProps = {
2015-11-15 06:17:07 +03:00
opacity: 0.5
2015-11-14 23:38:17 +03:00
};
componentDidMount () {
2015-11-16 12:18:49 +03:00
this.app = document.querySelector('[data-react-toolbox="app"]') || document.body;
2015-11-14 23:38:17 +03:00
this.node = document.createElement('div');
this.node.setAttribute('data-react-toolbox', 'overlay');
2015-11-14 23:38:17 +03:00
this.app.appendChild(this.node);
this.handleRender();
}
componentDidUpdate () {
this.handleRender();
}
componentWillUnmount () {
ReactDOM.unmountComponentAtNode(this.node);
this.app.removeChild(this.node);
}
handleRender () {
let className = style.root;
const overlayStyle = {};
2015-11-15 06:17:07 +03:00
if (this.props.active) {
className += ` ${style.active}`;
overlayStyle.opacity = this.props.opacity;
2015-11-15 06:17:07 +03:00
}
if (this.props.className) className += ` ${className}`;
2015-11-14 23:38:17 +03:00
ReactDOM.render(
<div className={className}>
2015-11-15 06:17:07 +03:00
<div
className={style.overlay}
onClick={this.props.onClick}
style={overlayStyle}
/>
2015-11-14 23:38:17 +03:00
{this.props.children}
</div>
, this.node);
}
render () {
return React.DOM.noscript();
}
}
export default Overlay;