regenbogenkarte/ts/hamburger.ts
nachtjasmin e98c3de747
chore: initial fork of the closed-source project
This commit includes all data from the current map, excluding:

- the actual data
- the deployment script to the server

We've added a comprehensive README for newcomers, so that they can
understand what's going on.

Co-authored-by: xenia <xhartmann@posteo.de>
2022-09-04 10:28:56 +02:00

28 lines
959 B
TypeScript

const openClass = "open";
export const setupHamburger = () => {
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();
});
};