2016-06-04 00:44:33 +03:00
|
|
|
import React, { Component, PropTypes } from 'react';
|
2016-05-22 14:42:52 +03:00
|
|
|
import classnames from 'classnames';
|
|
|
|
import { themr } from 'react-css-themr';
|
2017-01-26 20:05:32 +03:00
|
|
|
import { OVERLAY } from '../identifiers';
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2016-06-04 00:44:33 +03:00
|
|
|
class Overlay extends Component {
|
2015-11-22 23:41:28 +03:00
|
|
|
static propTypes = {
|
2016-06-04 00:44:33 +03:00
|
|
|
active: PropTypes.bool,
|
|
|
|
children: PropTypes.node,
|
|
|
|
className: PropTypes.string,
|
2017-01-05 04:42:18 +03:00
|
|
|
lockScroll: PropTypes.bool,
|
2016-06-04 00:44:33 +03:00
|
|
|
onClick: PropTypes.func,
|
|
|
|
onEscKeyDown: PropTypes.func,
|
|
|
|
theme: PropTypes.shape({
|
|
|
|
active: PropTypes.string,
|
|
|
|
backdrop: PropTypes.string,
|
2017-01-26 20:05:32 +03:00
|
|
|
overlay: PropTypes.string,
|
|
|
|
}),
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static defaultProps = {
|
2017-01-26 20:05:32 +03:00
|
|
|
lockScroll: true,
|
2015-11-22 23:41:28 +03:00
|
|
|
};
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentDidMount() {
|
2017-01-05 04:42:18 +03:00
|
|
|
const { active, lockScroll, onEscKeyDown } = this.props;
|
|
|
|
if (onEscKeyDown) document.body.addEventListener('keydown', this.handleEscKey.bind(this));
|
|
|
|
if (active && lockScroll) document.body.style.overflow = 'hidden';
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentWillUpdate(nextProps) {
|
2017-01-05 04:42:18 +03:00
|
|
|
if (this.props.lockScroll) {
|
|
|
|
const becomingActive = nextProps.active && !this.props.active;
|
|
|
|
const becomingUnactive = !nextProps.active && this.props.active;
|
|
|
|
|
|
|
|
if (becomingActive) {
|
|
|
|
document.body.style.overflow = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (becomingUnactive && !document.querySelectorAll('[data-react-toolbox="overlay"]')[1]) {
|
|
|
|
document.body.style.overflow = '';
|
|
|
|
}
|
|
|
|
}
|
2016-04-20 21:02:38 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
2017-01-05 04:42:18 +03:00
|
|
|
if (this.props.active && !prevProps.active && this.props.onEscKeyDown) {
|
|
|
|
document.body.addEventListener('keydown', this.handleEscKey.bind(this));
|
2016-03-21 00:27:36 +03:00
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
componentWillUnmount() {
|
2017-01-05 04:42:18 +03:00
|
|
|
if (this.props.active && this.props.lockScroll) {
|
|
|
|
if (!document.querySelectorAll('[data-react-toolbox="overlay"]')[1]) {
|
|
|
|
document.body.style.overflow = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.props.onEscKeyDown) {
|
|
|
|
document.body.removeEventListener('keydown', this.handleEscKey);
|
|
|
|
}
|
2016-03-21 00:27:36 +03:00
|
|
|
}
|
|
|
|
|
2016-10-12 16:08:49 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
handleClick = (event) => {
|
2017-01-05 04:42:18 +03:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
if (this.props.onClick) {
|
|
|
|
this.props.onClick(event);
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 23:41:28 +03:00
|
|
|
|
2017-01-26 20:05:32 +03:00
|
|
|
render() {
|
2017-01-05 04:42:18 +03:00
|
|
|
const { active, className, lockScroll, theme, onEscKeyDown, ...other } = this.props; // eslint-disable-line
|
2016-04-10 18:43:46 +03:00
|
|
|
return (
|
2017-01-05 04:42:18 +03:00
|
|
|
<div
|
|
|
|
{...other}
|
|
|
|
onClick={this.handleClick}
|
|
|
|
className={classnames(theme.overlay, {
|
2017-01-26 20:05:32 +03:00
|
|
|
[theme.active]: active,
|
2017-01-05 04:42:18 +03:00
|
|
|
}, className)}
|
|
|
|
/>
|
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 };
|