hugo-theme-re-terminal/assets/js/menu.js

45 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2022-06-21 13:50:51 +00:00
const container = document.querySelector(".container");
2022-11-07 23:34:01 +00:00
const allMenus = document.querySelectorAll(".menu");
2022-06-21 13:50:51 +00:00
2022-11-07 23:34:01 +00:00
// Hide menus on body click
2022-06-21 13:50:51 +00:00
document.body.addEventListener("click", () => {
2022-11-07 23:34:01 +00:00
allMenus.forEach(menu => {
if (menu.classList.contains("open")) {
menu.classList.remove("open");
}
});
2022-06-21 13:50:51 +00:00
});
2022-11-07 23:34:01 +00:00
// Reset menus on resize
window.addEventListener("resize", () => {
allMenus.forEach(menu => {
menu.classList.remove("open");
2022-06-21 13:50:51 +00:00
});
2022-11-07 23:34:01 +00:00
});
2022-06-21 13:50:51 +00:00
2022-11-07 23:34:01 +00:00
// Handle desktop menu
allMenus.forEach(menu => {
const trigger = menu.querySelector(".menu__trigger");
const dropdown = menu.querySelector(".menu__dropdown");
2022-06-21 13:50:51 +00:00
2022-11-07 23:34:01 +00:00
trigger.addEventListener("click", e => {
2022-06-21 13:50:51 +00:00
e.stopPropagation();
2022-11-07 23:34:01 +00:00
if (menu.classList.contains("open")) {
menu.classList.remove("open");
} else {
// Close all menus...
allMenus.forEach(m => m.classList.remove("open"));
// ...before opening the current one
menu.classList.add("open");
}
if (dropdown.getBoundingClientRect().right > container.getBoundingClientRect().right) {
dropdown.style.left = "auto";
dropdown.style.right = 0;
2022-06-21 13:50:51 +00:00
}
});
2022-11-07 23:34:01 +00:00
dropdown.addEventListener("click", e => e.stopPropagation());
});