mirror of
https://github.com/momenbasel/keyFinder.git
synced 2026-11-02 18:44:12 +01:00
27 lines
783 B
JavaScript
27 lines
783 B
JavaScript
const menu = document.querySelector('.menu-toggle');
|
|
const navigation = document.querySelector('#site-navigation');
|
|
|
|
function closeMenu() {
|
|
menu.setAttribute('aria-expanded', 'false');
|
|
navigation.classList.remove('open');
|
|
}
|
|
|
|
menu.addEventListener('click', () => {
|
|
const open = menu.getAttribute('aria-expanded') !== 'true';
|
|
menu.setAttribute('aria-expanded', String(open));
|
|
navigation.classList.toggle('open', open);
|
|
});
|
|
|
|
navigation.addEventListener('click', event => {
|
|
if (event.target.closest('a')) closeMenu();
|
|
});
|
|
|
|
document.addEventListener('keydown', event => {
|
|
if (event.key === 'Escape' && menu.getAttribute('aria-expanded') === 'true') {
|
|
closeMenu();
|
|
menu.focus();
|
|
}
|
|
});
|
|
|
|
window.matchMedia('(min-width: 769px)').addEventListener('change', closeMenu);
|