2022-09-04 08:26:20 +00:00
|
|
|
const openClass = "open";
|
|
|
|
|
2022-10-26 12:22:36 +00:00
|
|
|
export const setupInfoButton = () => {
|
2022-09-04 08:26:20 +00:00
|
|
|
const menu = document.getElementById("menu")!;
|
|
|
|
const openBtn = document.getElementById("open-menu-btn")!;
|
|
|
|
const closeBtn = document.getElementById("close-menu-btn")!;
|
|
|
|
|
|
|
|
const elems = [menu, document.body];
|
|
|
|
let isOpen = false;
|
|
|
|
const openMenu = () => {
|
|
|
|
isOpen = true;
|
|
|
|
elems.forEach((e) => e.classList.add(openClass));
|
|
|
|
openBtn.setAttribute("aria-expanded", "true");
|
|
|
|
closeBtn.focus(); // a11y: focus the element that can close the menu
|
|
|
|
};
|
|
|
|
const closeMenu = () => {
|
|
|
|
isOpen = false;
|
|
|
|
elems.forEach((e) => e.classList.remove(openClass));
|
|
|
|
openBtn.setAttribute("aria-expanded", "false");
|
|
|
|
openBtn.focus(); // a11y: focus the element that can opened the menu
|
|
|
|
};
|
|
|
|
|
|
|
|
openBtn.addEventListener("click", () => openMenu());
|
|
|
|
closeBtn.addEventListener("click", () => closeMenu());
|
|
|
|
document.body.addEventListener("keydown", (e) => {
|
|
|
|
if (e.key === "Escape" && isOpen) closeMenu();
|
|
|
|
});
|
|
|
|
};
|