Skip to content
This repository has been archived by the owner on Jun 10, 2023. It is now read-only.

Add support for multiple submenus on desktop. #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 28 additions & 18 deletions assets/js/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ const container = document.querySelector(".container");
const menu = document.querySelector(".menu");
const mobileMenuTrigger = document.querySelector(".menu-trigger");
const desktopMenu = document.querySelector(".menu__inner--desktop");
const desktopMenuTrigger = document.querySelector(".menu__sub-inner-more-trigger");
const menuMore = document.querySelector(".menu__sub-inner-more");
const allMenuMores = document.querySelectorAll(".menu__sub-inner-more");
const mobileQuery = getComputedStyle(document.body).getPropertyValue("--phoneWidth");
const isMobile = () => window.matchMedia(mobileQuery).matches;
const isMobileMenu = () => {
mobileMenuTrigger && mobileMenuTrigger.classList.toggle("hidden", !isMobile());
menu && menu.classList.toggle("hidden", isMobile());
menuMore && menuMore.classList.toggle("hidden", !isMobile());
allMenuMores.forEach(menuMore => menuMore.classList.toggle("hidden", !isMobile()));
};

// Common

menu && menu.addEventListener("click", e => e.stopPropagation());
menuMore && menuMore.addEventListener("click", e => e.stopPropagation());
allMenuMores.forEach(menuMore => menuMore.addEventListener("click", e => e.stopPropagation()));

isMobileMenu();

document.body.addEventListener("click", () => {
if (!isMobile() && menuMore && !menuMore.classList.contains("hidden")) {
menuMore.classList.add("hidden");
if (!isMobile()) {
allMenuMores.forEach(menuMore => menuMore.classList.add("hidden"));
} else if (isMobile() && !menu.classList.contains("hidden")) {
menu.classList.add("hidden");
}
Expand All @@ -39,16 +38,27 @@ mobileMenuTrigger &&

// Desktop menu

desktopMenuTrigger &&
desktopMenuTrigger.addEventListener("click", e => {
e.stopPropagation();
menuMore && menuMore.classList.toggle("hidden");

if (
menuMore &&
menuMore.getBoundingClientRect().right > container.getBoundingClientRect().right
) {
menuMore.style.left = "auto";
menuMore.style.right = 0;
}
desktopMenu &&
desktopMenu.querySelectorAll(".menu__sub-inner").forEach(desktopSubMenu => {
const desktopMenuTrigger = desktopSubMenu.querySelector(".menu__sub-inner-more-trigger");
const menuMore = desktopSubMenu.querySelector(".menu__sub-inner-more");
desktopMenuTrigger.addEventListener("click", e => {
// Hide other menus if this one was clicked.
allMenuMores.forEach(otherMenuMore => {
if (otherMenuMore !== menuMore) {
otherMenuMore.classList.add("hidden");
}
});

e.stopPropagation();
menuMore && menuMore.classList.toggle("hidden");

if (
menuMore &&
menuMore.getBoundingClientRect().right > container.getBoundingClientRect().right
) {
menuMore.style.left = "auto";
menuMore.style.right = 0;
}
});
});
2 changes: 1 addition & 1 deletion static/assets/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.