2017-08-06 16:08:03 -04:00
|
|
|
|
|
|
|
class Overlay {
|
|
|
|
|
|
|
|
constructor(elem) {
|
|
|
|
this.container = elem;
|
|
|
|
elem.addEventListener('click', event => {
|
|
|
|
if (event.target === elem) return this.hide();
|
|
|
|
});
|
2019-08-24 13:26:28 -04:00
|
|
|
|
|
|
|
window.addEventListener('keyup', event => {
|
|
|
|
if (event.key === 'Escape') {
|
|
|
|
this.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-07 06:34:40 -04:00
|
|
|
let closeButtons = elem.querySelectorAll('.popup-header-close');
|
2017-08-06 16:08:03 -04:00
|
|
|
for (let i=0; i < closeButtons.length; i++) {
|
|
|
|
closeButtons[i].addEventListener('click', this.hide.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 13:26:28 -04:00
|
|
|
hide() { this.toggle(false); }
|
|
|
|
show() { this.toggle(true); }
|
|
|
|
|
2017-08-06 16:08:03 -04:00
|
|
|
toggle(show = true) {
|
|
|
|
let start = Date.now();
|
|
|
|
let duration = 240;
|
|
|
|
|
|
|
|
function setOpacity() {
|
|
|
|
let elapsedTime = (Date.now() - start);
|
|
|
|
let targetOpacity = show ? (elapsedTime / duration) : 1-(elapsedTime / duration);
|
|
|
|
this.container.style.opacity = targetOpacity;
|
|
|
|
if (elapsedTime > duration) {
|
2017-08-07 14:32:31 -04:00
|
|
|
this.container.style.display = show ? 'flex' : 'none';
|
2019-08-24 13:26:28 -04:00
|
|
|
if (show) {
|
|
|
|
this.focusOnBody();
|
|
|
|
}
|
2017-08-06 16:08:03 -04:00
|
|
|
this.container.style.opacity = '';
|
|
|
|
} else {
|
|
|
|
requestAnimationFrame(setOpacity.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
requestAnimationFrame(setOpacity.bind(this));
|
|
|
|
}
|
|
|
|
|
2019-08-24 13:26:28 -04:00
|
|
|
focusOnBody() {
|
|
|
|
const body = this.container.querySelector('.popup-body');
|
|
|
|
if (body) {
|
|
|
|
body.focus();
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 16:08:03 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-11-09 16:17:35 -05:00
|
|
|
export default Overlay;
|