Skip to content

Commit

Permalink
Merge pull request #673 from openstad/fix/mobileMenu-keyboardFocus
Browse files Browse the repository at this point in the history
added keyboard focus trap to mobile menu
  • Loading branch information
rudivanhierden authored Nov 6, 2024
2 parents 32baaed + 6ba2545 commit 775186f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions apps/cms-server/modules/asset/ui/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,44 @@ function adjustMenu() {
closeButtonSpan.textContent = 'Menu tonen';
}

function trapFocus(element) {
const focusableElements = element.querySelectorAll('a, button, input, textarea, select, details, [tabindex]:not([tabindex="-1"])');
const firstFocusableElement = focusableElements[0];
const lastFocusableElement = focusableElements[focusableElements.length - 1];

element.addEventListener('keydown', (event) => {
const isTabPressed = (event.key === 'Tab' || event.keyCode === 9);

if (!isTabPressed) {
return;
}

if (event.shiftKey) {
if (document.activeElement === firstFocusableElement) {
lastFocusableElement.focus();
event.preventDefault();
}
} else {
if (document.activeElement === lastFocusableElement) {
firstFocusableElement.focus();
event.preventDefault();
}
}
});
}


closeButton.addEventListener('click', () => {
const isExpanded = closeButton.getAttribute('aria-expanded') === 'true';
closeButton.setAttribute('aria-expanded', !isExpanded);
mainMenuContainer.setAttribute('aria-hidden', isExpanded);

navContainer.classList.toggle('--show');
closeButtonSpan.textContent = isExpanded ? 'Menu tonen' : 'Menu verbergen';

if (!isExpanded) {
trapFocus(navContainer);
}
});

navbar.addEventListener('focusout', (event) => {
Expand Down
4 changes: 2 additions & 2 deletions packages/resource-overview/src/resource-overview.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
}

.osc-resource-overview-content .osc-resource-overview-resource-collection {
grid-template-columns: 1fr;
grid-template-columns: 1fr !important;
}

.osc-resource-overview-content.full
.osc-resource-overview-resource-collection {
grid-template-columns: 1fr;
grid-template-columns: 1fr !important;
}
#stem-begroot-filter {
grid-column: 1 / -1;
Expand Down

0 comments on commit 775186f

Please sign in to comment.