Skip to content

Commit

Permalink
Check for existence of list item before calling Array.from.
Browse files Browse the repository at this point in the history
  • Loading branch information
jffng committed Aug 9, 2024
1 parent a931482 commit 4eea77b
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions private/src/scripts/modules/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,35 @@ let subMenus = [];
// if menu has lost focus inadvertently, restore it
const setupFocusTrap = () => {
const lastMenuItem = pageHeader.querySelector('.mobile-menu > ul > li:last-of-type');
const menuItemClassList = `.${Array.from(lastMenuItem.classList).join('.')}`;
let previousFocus;
if ( lastMenuItem ) {
const menuItemClassList = `.${Array.from(lastMenuItem.classList).join('.')}`;
let previousFocus;

mobileMenu.addEventListener('focusout', (event) => {
previousFocus = event.target;
});
mobileMenu.addEventListener('focusout', (event) => {
previousFocus = event.target;
});

document.addEventListener('focusin', (event) => {
const closestMenuItemToPrevious = previousFocus?.closest(menuItemClassList);
const originWasLastItem = previousFocus === lastMenuItem.firstElementChild;
const originWasChildOfLastItem = closestMenuItemToPrevious === lastMenuItem;
document.addEventListener('focusin', (event) => {
const closestMenuItemToPrevious = previousFocus?.closest(menuItemClassList);
const originWasLastItem = previousFocus === lastMenuItem.firstElementChild;
const originWasChildOfLastItem = closestMenuItemToPrevious === lastMenuItem;

// previous element wasn't in the mobile nav, or wasn't the last item in it
if (!originWasLastItem && !originWasChildOfLastItem) {
return;
}
// previous element wasn't in the mobile nav, or wasn't the last item in it
if (!originWasLastItem && !originWasChildOfLastItem) {
return;
}

// focus is still within the mobile nav
if (event.target.closest('.mobile-menu')) {
return;
}
// focus is still within the mobile nav
if (event.target.closest('.mobile-menu')) {
return;
}

event.preventDefault();
event.preventDefault();

previousFocus = undefined;
menuToggle.focus();
});
previousFocus = undefined;
menuToggle.focus();
});
}
};

// close all sub menus
Expand Down

0 comments on commit 4eea77b

Please sign in to comment.