-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobile-menu.js
29 lines (24 loc) · 1.02 KB
/
mobile-menu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(() => {
const mobileMenu = document.querySelector('.js-menu-container');
const openMenuBtn = document.querySelector('.js-open-menu');
const closeMenuBtn = document.querySelector('.js-close-menu');
const toggleMenu = () => {
const isMenuOpen =
openMenuBtn.getAttribute('aria-expanded') === 'true' || false;
openMenuBtn.setAttribute('aria-expanded', !isMenuOpen);
mobileMenu.classList.toggle('is-open');
const scrollLockMethod = !isMenuOpen
? 'disableBodyScroll'
: 'enableBodyScroll';
bodyScrollLock[scrollLockMethod](document.body);
};
openMenuBtn.addEventListener('click', toggleMenu);
closeMenuBtn.addEventListener('click', toggleMenu);
// Close the mobile menu on wider screens if the device orientation changes
window.matchMedia('(min-width: 768px)').addEventListener('change', e => {
if (!e.matches) return;
mobileMenu.classList.remove('is-open');
openMenuBtn.setAttribute('aria-expanded', false);
bodyScrollLock.enableBodyScroll(document.body);
});
})();