-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasthead-menu.js
48 lines (34 loc) · 1.61 KB
/
masthead-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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const mastheadMenu = document.querySelector("[data-masthead-menu]");
const mastheadMenuToggle = mastheadMenu?.querySelector("[data-masthead-menu-toggle]");
const onMastheadMenuToggleClick = (event) => {
event.preventDefault();
const target = event.currentTarget;
const expanded = target.getAttribute("aria-expanded") === "true" || false;
target.setAttribute("aria-expanded", !expanded);
}
mastheadMenuToggle?.addEventListener("click", onMastheadMenuToggleClick);
const mastheadMenuClose = mastheadMenu?.querySelector("[data-masthead-menu-close]");
const onMastheadMenuCloseClick = (event) => {
event.preventDefault();
const target = mastheadMenuToggle;
target.setAttribute("aria-expanded", false);
}
mastheadMenuClose?.addEventListener("click", onMastheadMenuCloseClick);
let prevWindowWidth = window.innerWidth;
function setAriaExpanded() {
// If the previous window width was greater than or equal to 768, set aria-expanded to false so that the menu is collapsed
if (window.innerWidth < 768) {
if (prevWindowWidth >= 768) {
mastheadMenuToggle?.setAttribute('aria-expanded', 'false');
}
} else {
// Otherwise, set aria-expanded to true so that the menu is expanded
mastheadMenuToggle?.setAttribute('aria-expanded', 'true');
}
// Update the previous window width so the next time this function is called, we can compare the previous width to the current width
prevWindowWidth = window.innerWidth;
}
// Set the initial value of aria-expanded
setAriaExpanded();
// Update the value of aria-expanded when the window is resized
window.addEventListener('resize', setAriaExpanded);