Skip to content

Commit

Permalink
Merge pull request #1140 from City-of-Helsinki/UHF-9699
Browse files Browse the repository at this point in the history
UHF-9699: Added possibility to close menu toggle with esc
  • Loading branch information
annadruid authored Dec 19, 2024
2 parents cb36eff + 318f92c commit 9c7d466
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/js/desktop-menu-toggle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/nav-toggle-dropdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/js/nav-toggle.min.js

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/js/desktop-menu-toggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ function getSiblings(n) {
return getChildren(n.parentNode.firstChild, n);
}

function handleEscKey(event) {
if (event.key === 'Escape') {
closeOpenItems();
}
}

document.addEventListener('DOMContentLoaded', function startDesktopMenu() {
// Find all menu items with children menus.
const itemsWithVisibleChildren = document.querySelectorAll('.desktop-menu .menu--level-0 > .menu__item--item-below');
Expand All @@ -91,6 +97,9 @@ document.addEventListener('DOMContentLoaded', function startDesktopMenu() {
item.addEventListener('mouseleave', mouseLeave, false);
}
}

// Add keydown event listener to handle 'Esc' key
document.addEventListener('keydown', handleEscKey);
});

// Functionality when other menu item is clicked while one is open or
Expand Down
2 changes: 1 addition & 1 deletion src/js/nav-global/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ const MobilePanel = {
* Close menu on Escape button click if it is open.
*/
document.addEventListener('keydown', (e) => {
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27) && this.isOpen()) {
if (e.key === 'Escape' && this.isOpen()) {
this.close();
this.toggleButton.focus();
}
Expand Down
2 changes: 1 addition & 1 deletion src/js/nav-toggle/nav-toggle-dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class NavToggleDropdown {
addListeners() {
// Close menu on ESC
document.addEventListener('keydown', (e) => {
if ((e.key === 'Escape' || e.key === 'Esc' || e.keyCode === 27) && this.isOpen()) {
if (e.key === 'Escape' && this.isOpen()) {
this.close();
this.buttonInstance.focus();
}
Expand Down

0 comments on commit 9c7d466

Please sign in to comment.