Close mobile menu with esc key.

This commit is contained in:
Fredrik Jonsson 2024-02-25 15:36:02 +01:00
commit 608bf2538d

View file

@ -15,14 +15,23 @@
nav.classList.add('js-nav-open');
sheet.setAttribute('aria-hidden', 'false');
toggle.setAttribute('aria-expanded', 'true');
document.body.addEventListener('keydown', closeonesc);
}
function navclose() {
nav.classList.remove('js-nav-open');
sheet.setAttribute('aria-hidden', 'true');
toggle.setAttribute('aria-expanded', 'false');
document.body.removeEventListener('keydown', closeonesc);
}
// Close the nav sheet with "esc" key.
function closeonesc(e) {
if (e.key === 'Escape') {
navclose();
}
};
// Toggle the mobile nav sheet.
const toggles = document.querySelectorAll('.mobile-nav__cover, .mobile-nav__toggle');
toggles.forEach(function (toggle) {