Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/native. An initiative which aims to make it easy to create complex, light-weight, and perfor okikio.github.io/native
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219const MOBILE_MEDIA_QUERY = "(max-width: 40rem)";
interface SidebarElements { aside: HTMLElement; toggle: HTMLButtonElement; overlay: HTMLButtonElement;}
class Accordion { private readonly el: HTMLDetailsElement; private readonly summary: HTMLElement; private readonly content: HTMLElement; private animation: Animation | null = null; private isClosing = false; private isExpanding = false;
constructor(el: HTMLDetailsElement) { const summary = el.querySelector("summary"); const content = el.querySelector<HTMLElement>(".content");
if (!(summary instanceof HTMLElement) || !(content instanceof HTMLElement)) { throw new Error("Accordion markup is missing its summary or content element."); }
this.el = el; this.summary = summary; this.content = content; this.summary.addEventListener("click", this.onClick); }
// These listeners live on the static sidebar elements for the full page lifecycle, // so there is no intermediate teardown step before the page unloads. private onClick = (event: MouseEvent) => { event.preventDefault(); this.el.style.overflow = "hidden";
if (this.isClosing || !this.el.open) { this.open(); return; }
if (this.isExpanding || this.el.open) { this.shrink(); } };
private shrink() { this.isClosing = true;
const startHeight = `${this.el.offsetHeight}px`; const endHeight = `${this.summary.offsetHeight}px`;
this.animation?.cancel(); this.animation = this.el.animate( { height: [startHeight, endHeight] }, { duration: 400, easing: "ease-out" } );
this.animation.onfinish = () => this.onAnimationFinish(false); this.animation.oncancel = () => { this.isClosing = false; }; }
private open() { this.el.style.height = `${this.el.offsetHeight}px`; this.el.open = true; window.requestAnimationFrame(() => this.expand()); }
private expand() { this.isExpanding = true;
const startHeight = `${this.el.offsetHeight}px`; const endHeight = `${this.summary.offsetHeight + this.content.offsetHeight}px`;
this.animation?.cancel(); this.animation = this.el.animate( { height: [startHeight, endHeight] }, { duration: 400, easing: "ease-out" } );
this.animation.onfinish = () => this.onAnimationFinish(true); this.animation.oncancel = () => { this.isExpanding = false; }; }
private onAnimationFinish(open: boolean) { this.el.open = open; this.animation = null; this.isClosing = false; this.isExpanding = false; this.el.style.height = ""; this.el.style.overflow = ""; }}
function getSidebar() { return document.querySelector<HTMLElement>("[data-sidebar]");}
function getSidebarElements(): SidebarElements | null { const aside = getSidebar(); const toggle = document.querySelector<HTMLButtonElement>("[data-sidebar-toggle]"); const overlay = document.querySelector<HTMLButtonElement>("[data-sidebar-overlay]");
if (!(aside instanceof HTMLElement) || !(toggle instanceof HTMLButtonElement) || !(overlay instanceof HTMLButtonElement)) { return null; }
return { aside, toggle, overlay };}
function isMobileViewport() { return window.matchMedia(MOBILE_MEDIA_QUERY).matches;}
function setSidebarOpen(elements: SidebarElements, open: boolean) { const shouldOpen = isMobileViewport() ? open : true;
elements.aside.dataset.open = String(shouldOpen); elements.overlay.dataset.open = String(shouldOpen); elements.toggle.setAttribute("aria-expanded", String(shouldOpen)); elements.toggle.setAttribute( "aria-label", shouldOpen ? "Close documentation navigation" : "Open documentation navigation" ); elements.overlay.setAttribute("aria-hidden", String(!(shouldOpen && isMobileViewport())));
document.body.dataset.sidebarOpen = String(shouldOpen && isMobileViewport());
if (shouldOpen && isMobileViewport()) { const firstFocusable = elements.aside.querySelector<HTMLAnchorElement | HTMLButtonElement>( "a[href], button:not([disabled])" );
firstFocusable?.focus(); }}
function cleanURL(value: string | URL) { return new URL(value, window.location.href).pathname.replace(/\/$/, "").replace(/\/index$/, "");}
export function inFocus(aside: HTMLElement | null = getSidebar()) { if (!(aside instanceof HTMLElement)) { return; }
const currentPath = cleanURL(window.location.href);
aside.querySelectorAll<HTMLAnchorElement>("a[href]").forEach((anchor) => { anchor.classList.remove("active");
if (cleanURL(anchor.href) !== currentPath) { return; }
anchor.classList.add("active");
let parent: HTMLElement | null = anchor.parentElement; while (parent && parent !== aside) { if (parent instanceof HTMLDetailsElement && !parent.open) { parent.open = true; }
parent = parent.parentElement; }
anchor.scrollIntoView({ behavior: "smooth" }); });}
export function init(aside: HTMLElement | null = getSidebar()) { if (!(aside instanceof HTMLElement)) { return; }
aside.querySelectorAll<HTMLDetailsElement>("details").forEach((details) => { if (details.dataset.accordionReady === "true") { return; }
details.dataset.accordionReady = "true"; new Accordion(details); });}
export function startSidebar() { const elements = getSidebarElements(); if (!elements) { return; }
init(elements.aside); inFocus(elements.aside); setSidebarOpen(elements, false);
if (elements.toggle.dataset.bound === "true") { return; }
const syncViewportState = () => setSidebarOpen(elements, elements.aside.dataset.open === "true"); const closeSidebar = () => setSidebarOpen(elements, false); const toggleSidebar = () => setSidebarOpen(elements, elements.aside.dataset.open !== "true"); const onKeydown = (event: KeyboardEvent) => { if (event.key === "Escape") { closeSidebar(); } };
elements.toggle.addEventListener("click", toggleSidebar); elements.overlay.addEventListener("click", closeSidebar); window.addEventListener("resize", syncViewportState, { passive: true }); document.addEventListener("keydown", onKeydown); elements.toggle.dataset.bound = "true";}