-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
30 lines (25 loc) · 955 Bytes
/
Copy pathscript.js
File metadata and controls
30 lines (25 loc) · 955 Bytes
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
30
const nav = document.querySelector('.site-nav');
const toggle = document.querySelector('.nav-toggle');
const menuLinks = document.querySelectorAll('.site-nav__links a');
const mobileQuery = window.matchMedia('(max-width: 640px)');
if (nav && toggle) {
const setMenuState = (isOpen) => {
nav.classList.toggle('is-open', isOpen);
toggle.setAttribute('aria-expanded', String(isOpen));
};
toggle.addEventListener('click', () => {
setMenuState(!nav.classList.contains('is-open'));
});
menuLinks.forEach((link) => {
link.addEventListener('click', () => {
if (mobileQuery.matches) {
setMenuState(false);
}
});
});
mobileQuery.addEventListener('change', (event) => {
if (!event.matches) {
setMenuState(false);
}
});
}