156 lines
4.1 KiB
JavaScript
156 lines
4.1 KiB
JavaScript
/** One document-wide Dock shell. The host owns routes and active nav items. */
|
|
export function mountDock({
|
|
navigation,
|
|
main,
|
|
trigger,
|
|
scrim,
|
|
t = (ru) => ru,
|
|
onOpenChange = () => {},
|
|
}) {
|
|
if (
|
|
![navigation, main, trigger, scrim].every((e) => e instanceof HTMLElement)
|
|
)
|
|
throw new TypeError(
|
|
"Dock needs navigation, main, trigger and scrim elements",
|
|
);
|
|
const abort = new AbortController(),
|
|
{ signal } = abort;
|
|
const initialInert = main.inert;
|
|
let open = false,
|
|
pinned = false,
|
|
timer,
|
|
disposed = false;
|
|
const focusable = () =>
|
|
[
|
|
...navigation.querySelectorAll(
|
|
'button:not(:disabled),a[href],input:not(:disabled),[tabindex="0"]',
|
|
),
|
|
].filter(
|
|
(e) => e.getClientRects().length && !e.closest("[inert],[hidden]"),
|
|
);
|
|
function setOpen(next, restore = false) {
|
|
if (disposed || open === next) return;
|
|
clearTimeout(timer);
|
|
open = next;
|
|
document.body.classList.toggle("dock-open", open);
|
|
trigger.setAttribute("aria-expanded", String(open));
|
|
trigger.setAttribute(
|
|
"aria-label",
|
|
open
|
|
? t("Свернуть меню", "Collapse navigation")
|
|
: t("Развернуть меню", "Expand navigation"),
|
|
);
|
|
main.inert = open || initialInert;
|
|
if (open) {
|
|
document
|
|
.querySelectorAll('[popover="auto"]:popover-open')
|
|
.forEach((e) => e.hidePopover());
|
|
navigation.setAttribute("role", "dialog");
|
|
navigation.setAttribute("aria-modal", "true");
|
|
} else {
|
|
pinned = false;
|
|
navigation.removeAttribute("role");
|
|
navigation.removeAttribute("aria-modal");
|
|
if (restore || navigation.contains(document.activeElement))
|
|
trigger.focus({ preventScroll: true });
|
|
}
|
|
navigation.dispatchEvent(
|
|
new CustomEvent("anidev:dockchange", { detail: { open } }),
|
|
);
|
|
onOpenChange(open);
|
|
}
|
|
trigger.setAttribute("aria-expanded", "false");
|
|
trigger.addEventListener(
|
|
"click",
|
|
() => {
|
|
if (open && pinned) setOpen(false, true);
|
|
else {
|
|
setOpen(true);
|
|
pinned = true;
|
|
}
|
|
},
|
|
{ signal },
|
|
);
|
|
navigation.addEventListener(
|
|
"pointerenter",
|
|
(e) => {
|
|
clearTimeout(timer);
|
|
if (
|
|
e.pointerType === "mouse" &&
|
|
matchMedia("(hover:hover) and (pointer:fine)").matches
|
|
)
|
|
setOpen(true);
|
|
},
|
|
{ signal },
|
|
);
|
|
navigation.addEventListener(
|
|
"pointerleave",
|
|
() => {
|
|
if (!pinned && !navigation.contains(document.activeElement))
|
|
timer = setTimeout(() => setOpen(false), 180);
|
|
},
|
|
{ signal },
|
|
);
|
|
navigation.addEventListener(
|
|
"focusout",
|
|
(e) => {
|
|
if (
|
|
!pinned &&
|
|
!navigation.contains(e.relatedTarget) &&
|
|
!navigation.matches(":hover")
|
|
)
|
|
timer = setTimeout(() => setOpen(false), 180);
|
|
},
|
|
{ signal },
|
|
);
|
|
scrim.addEventListener("click", () => setOpen(false, true), { signal });
|
|
document.addEventListener(
|
|
"keydown",
|
|
(e) => {
|
|
if (!open || e.defaultPrevented) return;
|
|
if (e.key === "Escape") {
|
|
const popup = document.querySelector('[popover="auto"]:popover-open');
|
|
if (popup) return;
|
|
e.preventDefault();
|
|
setOpen(false, true);
|
|
}
|
|
if (e.key === "Tab") {
|
|
const items = focusable(),
|
|
first = items[0],
|
|
last = items.at(-1);
|
|
if (!navigation.contains(document.activeElement)) {
|
|
e.preventDefault();
|
|
(e.shiftKey ? last : first)?.focus();
|
|
} else if (e.shiftKey && document.activeElement === first) {
|
|
e.preventDefault();
|
|
last?.focus();
|
|
} else if (!e.shiftKey && document.activeElement === last) {
|
|
e.preventDefault();
|
|
first?.focus();
|
|
}
|
|
}
|
|
},
|
|
{ signal },
|
|
);
|
|
return {
|
|
open() {
|
|
setOpen(true);
|
|
pinned = true;
|
|
},
|
|
close() {
|
|
setOpen(false, true);
|
|
},
|
|
get isOpen() {
|
|
return open;
|
|
},
|
|
dispose() {
|
|
if (disposed) return;
|
|
setOpen(false);
|
|
clearTimeout(timer);
|
|
abort.abort();
|
|
disposed = true;
|
|
main.inert = initialInert;
|
|
},
|
|
};
|
|
}
|