2015-11-22 23:41:28 +03:00
|
|
|
import React from 'react';
|
2016-04-10 18:43:46 +03:00
|
|
|
import Portal from '../hoc/Portal';
|
2015-12-11 20:01:24 +03:00
|
|
|
import ClassNames from 'classnames';
|
2015-11-22 23:41:28 +03:00
|
|
|
import style from './style';
|
|
|
|
|
|
|
|
class Overlay extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
active: React.PropTypes.bool,
|
|
|
|
children: React.PropTypes.node,
|
|
|
|
className: React.PropTypes.string,
|
2015-12-11 20:01:24 +03:00
|
|
|
invisible: React.PropTypes.bool,
|
2016-03-21 00:27:36 +03:00
|
|
|
onClick: React.PropTypes.func,
|
|
|
|
onEscKeyDown: React.PropTypes.func
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2015-12-11 20:01:24 +03:00
|
|
|
invisible: false
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount () {
|
2016-03-21 00:27:36 +03:00
|
|
|
if (this.props.active) {
|
|
|
|
this.escKeyListener = document.body.addEventListener('keydown', this.handleEscKey.bind(this));
|
2016-04-20 21:02:38 +03:00
|
|
|
document.body.style.overflow = 'hidden';
|
2016-03-21 00:27:36 +03:00
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2016-04-20 21:02:38 +03:00
|
|
|
componentWillUpdate (nextProps) {
|
|
|
|
if (nextProps.active && !this.props.active) document.body.style.overflow = 'hidden';
|
|
|
|
if (!nextProps.active && this.props.active) document.body.style.overflow = null;
|
|
|
|
}
|
|
|
|
|
2015-11-22 23:41:28 +03:00
|
|
|
componentDidUpdate () {
|
2016-03-21 00:27:36 +03:00
|
|
|
if (this.props.active && !this.escKeyListener) {
|
|
|
|
this.escKeyListener = document.body.addEventListener('keydown', this.handleEscKey.bind(this));
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2016-03-21 00:27:36 +03:00
|
|
|
if (this.escKeyListener) {
|
2016-03-21 00:49:11 +03:00
|
|
|
document.body.removeEventListener('keydown', this.handleEscKey);
|
2016-03-21 00:27:36 +03:00
|
|
|
this.escKeyListener = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleEscKey (e) {
|
2016-03-21 00:49:11 +03:00
|
|
|
if (this.props.active && this.props.onEscKeyDown && e.which === 27) {
|
2016-03-21 00:27:36 +03:00
|
|
|
this.props.onEscKeyDown(e);
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:43:46 +03:00
|
|
|
render () {
|
2015-12-11 20:01:24 +03:00
|
|
|
const className = ClassNames(style.root, {
|
|
|
|
[style.active]: this.props.active,
|
|
|
|
[style.invisible]: this.props.invisible
|
|
|
|
}, this.props.className);
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-04-10 18:43:46 +03:00
|
|
|
return (
|
|
|
|
<Portal>
|
|
|
|
<div className={className}>
|
|
|
|
<div className={style.overlay} onClick={this.props.onClick} />
|
|
|
|
{this.props.children}
|
|
|
|
</div>
|
|
|
|
</Portal>
|
2016-03-05 22:29:42 +03:00
|
|
|
);
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Overlay;
|