|
| 1 | +import { getBreakpoint, onBreakpointChange } from "../misc/breakpoint"; |
| 2 | + |
| 3 | +const NAV_OPEN_CLASS = "nav-open"; |
| 4 | + |
| 5 | +const FAB_OPEN_ICON = "bi-list"; |
| 6 | +const FAB_CLOSE_ICON = "bi-x-lg"; |
| 7 | + |
| 8 | +let isOpen = true; |
| 9 | + |
| 10 | +export default function setupNavigation() { |
| 11 | + const elements = getNavigationElements(); |
| 12 | + isOpen = elements.nav.classList.contains(NAV_OPEN_CLASS); |
| 13 | + |
| 14 | + setupEventListeners(elements); |
| 15 | + initAriaState(elements); |
| 16 | + |
| 17 | + onBreakpointChange(bp => applyBreakpoint(bp === "sm", elements)); |
| 18 | + applyBreakpoint(getBreakpoint() === "sm", elements); |
| 19 | +} |
| 20 | + |
| 21 | +function setupEventListeners(elements: NavigationElements) { |
| 22 | + elements.fab.addEventListener("click", onNavigationToggle(elements)); |
| 23 | + elements.fab.addEventListener("keydown", (e) => { |
| 24 | + if (e.key === " ") { e.preventDefault(); onNavigationToggle(elements)(); } |
| 25 | + }); |
| 26 | + elements.blur.addEventListener("click", closeNav(elements)); |
| 27 | + document.addEventListener("keydown", (e) => { |
| 28 | + if (e.key === "Escape" && isOpen) closeNav(elements)(); |
| 29 | + }); |
| 30 | + document.addEventListener("keydown", onFocusTrap(elements)); |
| 31 | +} |
| 32 | + |
| 33 | +function initAriaState(elements: NavigationElements) { |
| 34 | + elements.fab.setAttribute("role", "button"); |
| 35 | + elements.fab.setAttribute("aria-expanded", String(isOpen)); |
| 36 | + elements.fab.setAttribute("aria-label", isOpen ? "Close navigation" : "Open navigation"); |
| 37 | + if (!isOpen) elements.links.setAttribute("inert", ""); |
| 38 | +} |
| 39 | + |
| 40 | +function applyBreakpoint(isMobile: boolean, elements: NavigationElements) { |
| 41 | + if (isMobile) { |
| 42 | + elements.nav.setAttribute("role", "dialog"); |
| 43 | + elements.nav.setAttribute("aria-modal", "true"); |
| 44 | + elements.nav.setAttribute("aria-label", "Navigation"); |
| 45 | + if (isOpen) closeNav(elements)(); |
| 46 | + } else { |
| 47 | + elements.nav.removeAttribute("role"); |
| 48 | + elements.nav.removeAttribute("aria-modal"); |
| 49 | + elements.nav.removeAttribute("aria-label"); |
| 50 | + if (!isOpen) { |
| 51 | + elements.nav.classList.add(NAV_OPEN_CLASS); |
| 52 | + elements.links.removeAttribute("inert"); |
| 53 | + isOpen = true; |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +interface NavigationElements { |
| 59 | + nav: HTMLElement, |
| 60 | + blur: HTMLDivElement, |
| 61 | + fab: HTMLDivElement, |
| 62 | + fabIcon: HTMLElement, |
| 63 | + links: HTMLUListElement, |
| 64 | +} |
| 65 | + |
| 66 | +function getNavigationElements(): NavigationElements { |
| 67 | + const baseSelector = ".page-chrome > nav"; |
| 68 | + |
| 69 | + const nav = tryQuerySelector(baseSelector) as HTMLElement; |
| 70 | + const blur = tryQuerySelector(`${baseSelector} > .nav-blur`) as HTMLDivElement; |
| 71 | + const fab = tryQuerySelector(`${baseSelector} > .nav-fab`) as HTMLDivElement; |
| 72 | + const fabIcon = tryQuerySelector(`${baseSelector} > .nav-fab > i`) as HTMLElement; |
| 73 | + const links = tryQuerySelector(`${baseSelector} > .nav-links`) as HTMLUListElement; |
| 74 | + |
| 75 | + return { nav, blur, fab, fabIcon, links }; |
| 76 | +} |
| 77 | + |
| 78 | +const tryQuerySelector = (selector: Parameters<typeof document.querySelector>[0]) => { |
| 79 | + const element = document.querySelector(selector); |
| 80 | + if (element == null) { |
| 81 | + throw new Error(`Couldn't query selector: ${selector}`); |
| 82 | + } |
| 83 | + |
| 84 | + return element; |
| 85 | +} |
| 86 | + |
| 87 | +const onNavigationToggle = (elements: NavigationElements) => |
| 88 | + () => isOpen |
| 89 | + ? closeNav(elements)() |
| 90 | + : openNav(elements)(); |
| 91 | + |
| 92 | +const onFocusTrap = (elements: NavigationElements) => |
| 93 | + (e: KeyboardEvent) => { |
| 94 | + if (!isOpen || e.key !== "Tab") return; |
| 95 | + |
| 96 | + const focusable: HTMLElement[] = [ |
| 97 | + elements.fab, |
| 98 | + ...Array.from(elements.links.querySelectorAll<HTMLElement>("a")), |
| 99 | + ]; |
| 100 | + |
| 101 | + const first = focusable[0]; |
| 102 | + const last = focusable[focusable.length - 1]; |
| 103 | + |
| 104 | + if (e.shiftKey && document.activeElement === first) { |
| 105 | + e.preventDefault(); |
| 106 | + last.focus(); |
| 107 | + } else if (!e.shiftKey && document.activeElement === last) { |
| 108 | + e.preventDefault(); |
| 109 | + first.focus(); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | +const closeNav = (elements: NavigationElements) => |
| 114 | + () => { |
| 115 | + elements.nav.classList.remove(NAV_OPEN_CLASS); |
| 116 | + elements.fabIcon.classList.remove(FAB_CLOSE_ICON); |
| 117 | + elements.fabIcon.classList.add(FAB_OPEN_ICON); |
| 118 | + elements.fab.setAttribute("aria-expanded", "false"); |
| 119 | + elements.fab.setAttribute("aria-label", "Open navigation"); |
| 120 | + elements.links.setAttribute("inert", ""); |
| 121 | + elements.fab.focus(); |
| 122 | + isOpen = false; |
| 123 | + }; |
| 124 | + |
| 125 | +const openNav = (elements: NavigationElements) => |
| 126 | + () => { |
| 127 | + elements.nav.classList.add(NAV_OPEN_CLASS); |
| 128 | + elements.fabIcon.classList.remove(FAB_OPEN_ICON); |
| 129 | + elements.fabIcon.classList.add(FAB_CLOSE_ICON); |
| 130 | + elements.fab.setAttribute("aria-expanded", "true"); |
| 131 | + elements.fab.setAttribute("aria-label", "Close navigation"); |
| 132 | + elements.links.removeAttribute("inert"); |
| 133 | + isOpen = true; |
| 134 | + elements.links.querySelector<HTMLElement>("a")?.focus(); |
| 135 | + }; |
0 commit comments