| | |
| | | import micromorph from "micromorph" |
| | | import { CanonicalSlug, RelativeURL } from "../../path" |
| | | |
| | | // adapted from `micromorph` |
| | | // https://github.com/natemoo-re/micromorph |
| | |
| | | return { url: new URL(href), scroll: 'routerNoscroll' in a.dataset ? false : undefined } |
| | | } |
| | | |
| | | function notifyNav(url: CanonicalSlug) { |
| | | const event: CustomEventMap["nav"] = new CustomEvent("nav", { detail: { url } }) |
| | | document.dispatchEvent(event) |
| | | } |
| | | |
| | | let p: DOMParser |
| | | async function navigate(url: URL, isBack: boolean = false) { |
| | | p = p || new DOMParser() |
| | |
| | | const elementsToAdd = html.head.querySelectorAll(':not([spa-preserve])') |
| | | elementsToAdd.forEach(el => document.head.appendChild(el)) |
| | | |
| | | if (!document.activeElement?.closest('[data-persist]')) { |
| | | document.body.focus() |
| | | } |
| | | notifyNav(document.body.dataset.slug!) |
| | | delete announcer.dataset.persist |
| | | } |
| | | |
| | | window.spaNavigate = navigate |
| | | |
| | | function createRouter() { |
| | | if (typeof window !== "undefined") { |
| | | window.addEventListener("click", async (event) => { |
| | |
| | | return |
| | | }) |
| | | } |
| | | |
| | | return new class Router { |
| | | go(pathname: string) { |
| | | go(pathname: RelativeURL) { |
| | | const url = new URL(pathname, window.location.toString()) |
| | | return navigate(url, false) |
| | | } |
| | |
| | | } |
| | | |
| | | createRouter() |
| | | notifyNav(document.body.dataset.slug!) |
| | | |
| | | if (!customElements.get('route-announcer')) { |
| | | const attrs = { |