mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-12-29 03:20:32 +00:00
Better JS
This commit is contained in:
parent
a688c73b79
commit
3816b0aa57
1 changed files with 14 additions and 12 deletions
|
@ -379,6 +379,11 @@ let BookWyrm = new class {
|
||||||
const modalButton = element.currentTarget;
|
const modalButton = element.currentTarget;
|
||||||
const targetModalId = modalButton.dataset.modalOpen;
|
const targetModalId = modalButton.dataset.modalOpen;
|
||||||
const htmlElement = document.querySelector('html');
|
const htmlElement = document.querySelector('html');
|
||||||
|
const modal = document.getElementById(targetModalId);
|
||||||
|
|
||||||
|
if (!modal) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
function handleModalOpen(modalElement) {
|
function handleModalOpen(modalElement) {
|
||||||
|
@ -387,10 +392,11 @@ let BookWyrm = new class {
|
||||||
modalElement.getElementsByClassName('modal-card')[0].focus();
|
modalElement.getElementsByClassName('modal-card')[0].focus();
|
||||||
|
|
||||||
const closeButtons = modalElement.querySelectorAll("[data-modal-close]");
|
const closeButtons = modalElement.querySelectorAll("[data-modal-close]");
|
||||||
|
|
||||||
closeButtons.forEach((button) => {
|
closeButtons.forEach((button) => {
|
||||||
button.addEventListener(
|
button.addEventListener(
|
||||||
'click',
|
'click',
|
||||||
function() { handleModalClose(modalElement) },
|
function() { handleModalClose(modalElement) }
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -400,7 +406,7 @@ let BookWyrm = new class {
|
||||||
if (event.key === 'Escape') {
|
if (event.key === 'Escape') {
|
||||||
handleModalClose(modalElement);
|
handleModalClose(modalElement);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
modalElement.addEventListener('keydown', handleFocusTrap)
|
modalElement.addEventListener('keydown', handleFocusTrap)
|
||||||
|
@ -432,12 +438,12 @@ let BookWyrm = new class {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.shiftKey ) /* shift + tab */ {
|
if (event.shiftKey ) /* Shift + tab */ {
|
||||||
if (document.activeElement === firstFocusableEl) {
|
if (document.activeElement === firstFocusableEl) {
|
||||||
lastFocusableEl.focus();
|
lastFocusableEl.focus();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
} else /* tab */ {
|
} else /* Tab */ {
|
||||||
if (document.activeElement === lastFocusableEl) {
|
if (document.activeElement === lastFocusableEl) {
|
||||||
firstFocusableEl.focus();
|
firstFocusableEl.focus();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
@ -445,11 +451,7 @@ let BookWyrm = new class {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const modal = document.getElementById(targetModalId);
|
// Open modal
|
||||||
if (modal) {
|
|
||||||
// Open the modal
|
|
||||||
handleModalOpen(modal);
|
handleModalOpen(modal);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}();
|
}();
|
||||||
|
|
Loading…
Reference in a new issue