2015-11-22 23:41:28 +03:00
|
|
|
import React from 'react';
|
2016-05-22 14:42:52 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2016-05-28 23:04:00 +03:00
|
|
|
import { OVERLAY } from '../identifiers.js';
|
|
|
|
import Portal from '../hoc/Portal.js';
|
2015-11-22 23:41:28 +03:00
|
|
|
|
|
|
|
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,
|
2016-05-22 14:42:52 +03:00
|
|
|
onEscKeyDown: React.PropTypes.func,
|
|
|
|
theme: React.PropTypes.shape({
|
|
|
|
active: React.PropTypes.string.isRequired,
|
|
|
|
backdrop: React.PropTypes.string.isRequired,
|
|
|
|
invisible: React.PropTypes.string.isRequired,
|
|
|
|
overlay: React.PropTypes.string.isRequired
|
|
|
|
})
|
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 () {
|
2016-05-22 14:42:52 +03:00
|
|
|
const { active, className, children, invisible, onClick, theme } = this.props;
|
|
|
|
const _className = classnames(theme.overlay, {
|
|
|
|
[theme.active]: active,
|
|
|
|
[theme.invisible]: invisible
|
|
|
|
}, className);
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-04-10 18:43:46 +03:00
|
|
|
return (
|
|
|
|
<Portal>
|
2016-05-22 14:42:52 +03:00
|
|
|
<div className={_className}>
|
|
|
|
<div className={theme.backdrop} onClick={onClick} />
|
|
|
|
{children}
|
2016-04-10 18:43:46 +03:00
|
|
|
</div>
|
|
|
|
</Portal>
|
2016-03-05 22:29:42 +03:00
|
|
|
);
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-28 23:04:00 +03:00
|
|
|
export default themr(OVERLAY)(Overlay);
|
|
|
|
export { Overlay };
|