Better JS

This commit is contained in:
Joachim 2021-11-28 18:34:54 +01:00
parent a688c73b79
commit 3816b0aa57

View file

@ -379,6 +379,11 @@ let BookWyrm = new class {
const modalButton = element.currentTarget;
const targetModalId = modalButton.dataset.modalOpen;
const htmlElement = document.querySelector('html');
const modal = document.getElementById(targetModalId);
if (!modal) {
return;
}
// Helper functions
function handleModalOpen(modalElement) {
@ -387,10 +392,11 @@ let BookWyrm = new class {
modalElement.getElementsByClassName('modal-card')[0].focus();
const closeButtons = modalElement.querySelectorAll("[data-modal-close]");
closeButtons.forEach((button) => {
button.addEventListener(
'click',
function() { handleModalClose(modalElement) },
function() { handleModalClose(modalElement) }
);
});
@ -400,7 +406,7 @@ let BookWyrm = new class {
if (event.key === 'Escape') {
handleModalClose(modalElement);
}
},
}
);
modalElement.addEventListener('keydown', handleFocusTrap)
@ -432,12 +438,12 @@ let BookWyrm = new class {
return;
}
if (event.shiftKey ) /* shift + tab */ {
if (event.shiftKey ) /* Shift + tab */ {
if (document.activeElement === firstFocusableEl) {
lastFocusableEl.focus();
event.preventDefault();
}
} else /* tab */ {
} else /* Tab */ {
if (document.activeElement === lastFocusableEl) {
firstFocusableEl.focus();
event.preventDefault();
@ -445,11 +451,7 @@ let BookWyrm = new class {
}
}
const modal = document.getElementById(targetModalId);
if (modal) {
// Open the modal
// Open modal
handleModalOpen(modal);
}
}
}();