close modals with Escape key
This commit is contained in:
parent
f798142a29
commit
8dd4fd98cb
@ -34,14 +34,17 @@
|
|||||||
setupCloser();
|
setupCloser();
|
||||||
|
|
||||||
triggerElement.classList.add(MODAL_TRIGGER_CLASS);
|
triggerElement.classList.add(MODAL_TRIGGER_CLASS);
|
||||||
triggerElement.addEventListener('click', open, false);
|
triggerElement.addEventListener('click', openHandler, false);
|
||||||
|
|
||||||
modalElement.classList.add(JS_INITIALIZED_CLASS);
|
modalElement.classList.add(JS_INITIALIZED_CLASS);
|
||||||
}
|
}
|
||||||
|
|
||||||
function open(event) {
|
function openHandler(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
open();
|
||||||
|
}
|
||||||
|
|
||||||
|
function open() {
|
||||||
modalElement.classList.add(MODAL_OPEN_CLASS);
|
modalElement.classList.add(MODAL_OPEN_CLASS);
|
||||||
overlayElement.classList.add(OVERLAY_CLASS);
|
overlayElement.classList.add(OVERLAY_CLASS);
|
||||||
document.body.insertBefore(overlayElement, modalElement);
|
document.body.insertBefore(overlayElement, modalElement);
|
||||||
@ -51,12 +54,20 @@
|
|||||||
if (modalUrl && MODAL_DYNAMIC_FLAG in modalElement.dataset) {
|
if (modalUrl && MODAL_DYNAMIC_FLAG in modalElement.dataset) {
|
||||||
fillModal(modalUrl);
|
fillModal(modalUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
document.addEventListener('keyup', keyupHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
function close(event) {
|
function closeHandler(event) {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
|
||||||
|
function close() {
|
||||||
overlayElement.classList.remove(OVERLAY_OPEN_CLASS);
|
overlayElement.classList.remove(OVERLAY_OPEN_CLASS);
|
||||||
modalElement.classList.remove(MODAL_OPEN_CLASS);
|
modalElement.classList.remove(MODAL_OPEN_CLASS);
|
||||||
|
|
||||||
|
document.removeEventListener('keyup', keyupHandler);
|
||||||
};
|
};
|
||||||
|
|
||||||
function setupForm() {
|
function setupForm() {
|
||||||
@ -70,8 +81,8 @@
|
|||||||
if (MODAL_CLOSABLE_FLAG in modalElement.dataset) {
|
if (MODAL_CLOSABLE_FLAG in modalElement.dataset) {
|
||||||
modalElement.insertBefore(closerElement, null);
|
modalElement.insertBefore(closerElement, null);
|
||||||
closerElement.classList.add(CLOSER_CLASS);
|
closerElement.classList.add(CLOSER_CLASS);
|
||||||
closerElement.addEventListener('click', close, false);
|
closerElement.addEventListener('click', closeHandler, false);
|
||||||
overlayElement.addEventListener('click', close, false);
|
overlayElement.addEventListener('click', closeHandler, false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,6 +116,12 @@
|
|||||||
setupForm();
|
setupForm();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function keyupHandler(event) {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
setup();
|
setup();
|
||||||
};
|
};
|
||||||
})();
|
})();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user