flotilla/src/app/modal.ts

110 lines
3 KiB
TypeScript
Raw Normal View History

2025-02-03 23:01:42 +00:00
import type {Component} from "svelte"
2026-03-11 23:29:24 +00:00
import {get, writable} from "svelte/store"
2024-10-15 22:52:30 +00:00
import {randomId, always, assoc, Emitter} from "@welshman/lib"
import {deriveDeduplicated} from "@welshman/store"
import {page} from "$app/stores"
2026-07-15 16:30:17 +00:00
import type {DialogSize} from "@lib/components/Dialog.svelte"
2024-08-06 20:10:10 +00:00
2024-10-01 18:47:21 +00:00
export type ModalOptions = {
drawer?: boolean
2026-03-11 23:29:24 +00:00
nested?: boolean
noEscape?: boolean
2024-10-17 22:02:03 +00:00
fullscreen?: boolean
2026-07-15 16:30:17 +00:00
size?: DialogSize
replaceState?: boolean
2024-11-27 00:39:49 +00:00
path?: string
2024-10-01 18:47:21 +00:00
}
2024-10-15 22:52:30 +00:00
export type Modal = {
id: string
2025-02-03 23:01:42 +00:00
component: Component
2024-10-15 22:52:30 +00:00
props: Record<string, any>
options: ModalOptions
}
export const emitter = new Emitter()
export const modals = writable<Record<string, Modal>>({})
2026-03-11 23:29:24 +00:00
const getIdsFromHash = (hash: string) => hash.slice(1).split(",").filter(Boolean)
// The modal stack lives in the url hash, but it is written with the History API directly rather
// than SvelteKit's `goto`. A programmatic hash `goto` runs a full navigation, which re-renders the
// whole layout tree and (in dev) leaves a second, orphaned app shell mounted; a History update does
// not navigate. `history.state` is carried through untouched, so SvelteKit's own popstate handler
// treats a back as a same-entry hash change and does nothing, while this store drives the open
// modals.
const modalHash = writable(typeof location === "undefined" ? "" : location.hash)
if (typeof window !== "undefined") {
window.addEventListener("popstate", () => modalHash.set(location.hash))
2026-08-28 02:31:29 +00:00
// Close modals on navigate
page.subscribe($page => modalHash.set($page.url?.hash ?? ""))
}
const setModalHash = (hash: string, replace: boolean) => {
const {pathname, search} = get(page).url
const url = pathname + search + hash
if (replace) {
history.replaceState(history.state, "", url)
} else {
history.pushState(history.state, "", url)
}
modalHash.set(hash)
}
export const modalStack = deriveDeduplicated([modalHash, modals], ([$hash, $modals]) => {
return getIdsFromHash($hash)
2026-03-11 23:29:24 +00:00
.map(id => $modals[id])
.filter(Boolean)
})
export const modal = deriveDeduplicated([modalHash, modals], ([$hash, $modals]) => {
const ids = getIdsFromHash($hash)
2026-03-11 23:29:24 +00:00
return $modals[ids.at(-1) || ""]
})
2024-10-08 18:39:16 +00:00
export const pushModal = (
2025-02-03 23:40:00 +00:00
component: Component<any>,
2024-10-08 18:39:16 +00:00
props: Record<string, any> = {},
options: ModalOptions = {},
) => {
2024-08-06 20:10:10 +00:00
const id = randomId()
const existingIds = getIdsFromHash(get(modalHash))
2026-03-11 23:29:24 +00:00
const ids = options.nested ? [...existingIds, id] : [id]
2024-08-06 20:10:10 +00:00
2024-10-15 22:52:30 +00:00
modals.update(assoc(id, {id, component, props, options}))
2024-10-11 22:29:54 +00:00
setModalHash("#" + ids.join(","), Boolean(options.replaceState))
2024-08-06 20:10:10 +00:00
return id
}
2024-10-09 16:31:50 +00:00
export const pushDrawer = (
2025-02-03 23:40:00 +00:00
component: Component<any>,
2024-10-09 16:31:50 +00:00
props: Record<string, any> = {},
options: ModalOptions = {},
) => pushModal(component, props, {...options, drawer: true})
2026-03-11 23:29:24 +00:00
export const popModal = () => {
const ids = getIdsFromHash(get(modalHash))
2026-03-11 23:29:24 +00:00
if (ids.length === 0) {
return
}
const next = ids.slice(0, -1).join(",")
setModalHash(next ? `#${next}` : "", true)
2026-03-11 23:29:24 +00:00
}
2024-10-15 22:52:30 +00:00
export const clearModals = () => {
setModalHash("", true)
2024-10-15 22:52:30 +00:00
modals.update(always({}))
2024-08-16 17:50:38 +00:00
emitter.emit("close")
2024-08-09 23:22:09 +00:00
}