Files
halo_theme_terminal/src/module/navigation.js
T
witcher ca00daddf6 feat: release Terminal-Witcher 3.1.17 and project handoff
Ship responsive navigation, configurable profile and four-section homepage, article/life category separation, natural-ratio book covers, and independent border settings. Include release history and complete context handoff documentation.
2026-11-16 18:30:36 +08:00

123 lines
5.0 KiB
JavaScript

export function registerNavigation() {
const header = document.querySelector(".header");
if (!header) return;
function syncScrolledHeader() {
header.classList.toggle("is-scrolled", window.scrollY > 8);
}
window.addEventListener("scroll", syncScrolledHeader, {passive: true});
window.addEventListener("pageshow", syncScrolledHeader);
syncScrolledHeader();
const navigation = document.getElementById("site-navigation");
const toggle = header?.querySelector(".navigation-toggle");
if (!navigation || !toggle) return;
// Keep this breakpoint in sync with $navigation-mobile-width.
const mobile = window.matchMedia("(max-width: 899px)");
const root = document.documentElement;
const menus = [...navigation.querySelectorAll(".menu")];
const background = [...document.querySelectorAll(".wrapper > .container, .wrapper > .footer")];
const previousInert = new Map();
let open = false;
function setSubmenu(menu, expanded) {
menu.classList.toggle("is-open", expanded);
menu.querySelector(".menu-trigger")?.setAttribute("aria-expanded", String(expanded));
}
function closeSubmenus() {
menus.forEach(menu => setSubmenu(menu, false));
}
function setOpen(expanded, restoreFocus = true) {
const wasOpen = open;
open = mobile.matches && expanded;
root.classList.toggle("navigation-open", open);
toggle.setAttribute("aria-expanded", String(open));
toggle.setAttribute("aria-label", open ? toggle.dataset.labelClose : toggle.dataset.labelOpen);
navigation.inert = mobile.matches && !open;
if (navigation.inert) navigation.setAttribute("aria-hidden", "true");
else navigation.removeAttribute("aria-hidden");
if (open) {
toggle.focus({preventScroll: true});
header.setAttribute("role", "dialog");
header.setAttribute("aria-modal", "true");
header.setAttribute("aria-label", navigation.getAttribute("aria-label"));
background.forEach(element => {
if (!previousInert.has(element)) previousInert.set(element, element.inert);
element.inert = true;
});
} else {
header.removeAttribute("role");
header.removeAttribute("aria-modal");
header.removeAttribute("aria-label");
previousInert.forEach((inert, element) => { element.inert = inert; });
previousInert.clear();
closeSubmenus();
if (wasOpen && restoreFocus) toggle.focus({preventScroll: true});
}
}
toggle.addEventListener("click", () => setOpen(!open));
header.addEventListener("click", event => {
if (event.target.closest("a[href]")) {
setOpen(false);
closeSubmenus();
}
});
menus.forEach(menu => {
const trigger = menu.querySelector(".menu-trigger");
menu.addEventListener("pointerenter", event => {
if (!mobile.matches && event.pointerType !== "touch") setSubmenu(menu, true);
});
menu.addEventListener("pointerleave", () => {
if (!menu.contains(document.activeElement)) setSubmenu(menu, false);
});
menu.addEventListener("focusin", event => {
if (!mobile.matches && event.target !== trigger) setSubmenu(menu, true);
});
menu.addEventListener("focusout", event => {
if (!menu.contains(event.relatedTarget)) setSubmenu(menu, false);
});
trigger?.addEventListener("click", () => {
setSubmenu(menu, !menu.classList.contains("is-open"));
});
});
document.addEventListener("keydown", event => {
if (event.key === "Escape") {
if (open) {
event.preventDefault();
setOpen(false);
} else {
const activeMenu = menus.find(menu => menu.classList.contains("is-open"));
if (activeMenu) {
activeMenu.querySelector(".menu-trigger")?.focus({preventScroll: true});
closeSubmenus();
}
}
}
if (!open || event.key !== "Tab") return;
const focusable = [...header.querySelectorAll("a[href], button")]
.filter(element => !element.disabled && element.getClientRects().length && !element.closest("[inert]"));
const first = focusable[0];
const last = focusable[focusable.length - 1];
if (event.shiftKey && document.activeElement === first) {
event.preventDefault();
last?.focus({preventScroll: true});
} else if (!event.shiftKey && document.activeElement === last) {
event.preventDefault();
first?.focus({preventScroll: true});
}
});
mobile.addEventListener("change", () => setOpen(false));
document.addEventListener("pjax:send", () => setOpen(false, false));
window.addEventListener("pageshow", () => setOpen(false, false));
setOpen(false, false);
}