2018-02-07 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2017-07-11 15:37:43 +00:00
|
|
|
import { browserHistory } from 'react-router';
|
2016-08-15 21:06:12 +00:00
|
|
|
|
2020-04-29 22:34:37 +00:00
|
|
|
import ExitIcon from '../../../images/exit.svg';
|
2017-07-11 15:37:43 +00:00
|
|
|
|
|
|
|
class Overlay extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.close = this.close.bind(this);
|
2018-03-02 16:40:31 +00:00
|
|
|
this.handleClick = this.handleClick.bind(this);
|
|
|
|
this.handleClickOutside = this.handleClickOutside.bind(this);
|
|
|
|
this.keyPressHandle = this.keyPressHandle.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
document.addEventListener('mousedown', this.handleClick, false);
|
|
|
|
document.addEventListener('keydown', this.keyPressHandle);
|
2017-07-11 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-03-02 16:40:31 +00:00
|
|
|
this.node.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('mousedown', this.handleClick, false);
|
|
|
|
document.removeEventListener('keydown', this.keyPressHandle);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleClick(e) {
|
|
|
|
if (this.node.contains(e.target)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-19 20:21:25 +00:00
|
|
|
this.handleClickOutside(e);
|
2018-03-02 16:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleClickOutside() {
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
keyPressHandle(e) {
|
|
|
|
// escape key code = 27.
|
|
|
|
// So here we are checking if the key pressed was Escape key.
|
|
|
|
if (e.keyCode === 27) {
|
|
|
|
this.close();
|
|
|
|
}
|
2017-07-11 15:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
2019-06-19 20:21:25 +00:00
|
|
|
// Only close if it is the last (and therefore the topmost overlay)
|
|
|
|
const overlays = document.getElementsByClassName('overlay');
|
|
|
|
if (this.node.parentElement.parentElement !== overlays[overlays.length - 1]) return;
|
|
|
|
|
2017-07-11 15:37:43 +00:00
|
|
|
if (!this.props.closeOverlay) {
|
|
|
|
browserHistory.push(this.props.previousPath);
|
|
|
|
} else {
|
|
|
|
this.props.closeOverlay();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
ariaLabel,
|
|
|
|
title,
|
2019-10-21 00:05:35 +00:00
|
|
|
children,
|
|
|
|
actions,
|
2019-12-11 14:12:00 +00:00
|
|
|
isFixedHeight,
|
2017-07-11 15:37:43 +00:00
|
|
|
} = this.props;
|
|
|
|
return (
|
2019-12-11 14:12:00 +00:00
|
|
|
<div className={`overlay ${isFixedHeight ? 'overlay--is-fixed-height' : ''}`}>
|
2017-07-11 15:37:43 +00:00
|
|
|
<div className="overlay__content">
|
|
|
|
<section
|
|
|
|
role="main"
|
|
|
|
aria-label={ariaLabel}
|
2018-03-02 16:40:31 +00:00
|
|
|
ref={(node) => { this.node = node; }}
|
2017-07-11 15:37:43 +00:00
|
|
|
className="overlay__body"
|
|
|
|
>
|
|
|
|
<header className="overlay__header">
|
|
|
|
<h2 className="overlay__title">{title}</h2>
|
2019-10-21 00:05:35 +00:00
|
|
|
<div className="overlay__actions">
|
|
|
|
{actions}
|
2020-05-05 23:03:58 +00:00
|
|
|
<button className="overlay__close-button" onClick={this.close} aria-label={`Close ${title} overlay`} >
|
|
|
|
<ExitIcon focusable="false" aria-hidden="true" />
|
2019-10-21 00:05:35 +00:00
|
|
|
</button>
|
|
|
|
</div>
|
2017-07-11 15:37:43 +00:00
|
|
|
</header>
|
|
|
|
{children}
|
|
|
|
</section>
|
|
|
|
</div>
|
2016-08-15 21:06:12 +00:00
|
|
|
</div>
|
2017-07-11 15:37:43 +00:00
|
|
|
);
|
|
|
|
}
|
2016-08-15 21:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Overlay.propTypes = {
|
2017-07-11 15:37:43 +00:00
|
|
|
children: PropTypes.element,
|
2019-10-21 00:05:35 +00:00
|
|
|
actions: PropTypes.element,
|
2017-07-11 15:37:43 +00:00
|
|
|
closeOverlay: PropTypes.func,
|
|
|
|
title: PropTypes.string,
|
|
|
|
ariaLabel: PropTypes.string,
|
2019-12-11 14:12:00 +00:00
|
|
|
previousPath: PropTypes.string,
|
|
|
|
isFixedHeight: PropTypes.bool,
|
2017-02-22 19:29:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Overlay.defaultProps = {
|
2017-07-11 15:37:43 +00:00
|
|
|
children: null,
|
2019-10-21 00:05:35 +00:00
|
|
|
actions: null,
|
2017-07-11 15:37:43 +00:00
|
|
|
title: 'Modal',
|
|
|
|
closeOverlay: null,
|
2017-09-14 18:51:36 +00:00
|
|
|
ariaLabel: 'modal',
|
2019-12-11 14:12:00 +00:00
|
|
|
previousPath: '/',
|
|
|
|
isFixedHeight: false,
|
2016-08-15 21:06:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Overlay;
|