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"
|
2026-09-10 15:44:21 +00:00
|
|
|
import {randomId, last, call, always, assoc, Emitter} from "@welshman/lib"
|
2026-02-03 18:32:54 +00:00
|
|
|
import {deriveDeduplicated} from "@welshman/store"
|
2026-09-08 17:23:22 +00:00
|
|
|
import {goto} from "$app/navigation"
|
2025-09-03 22:29:57 +00:00
|
|
|
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
|
2025-11-05 00:14:32 +00:00
|
|
|
noEscape?: boolean
|
2024-10-17 22:02:03 +00:00
|
|
|
fullscreen?: boolean
|
2026-07-15 16:30:17 +00:00
|
|
|
size?: DialogSize
|
2024-10-24 21:18:32 +00:00
|
|
|
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-09-10 15:44:21 +00:00
|
|
|
export const getIdsFromHash = (hash: string) => hash.slice(1).split(",").filter(Boolean)
|
2026-03-11 23:29:24 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export const modalHash = writable("")
|
2026-08-28 00:37:36 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export const modalStack = deriveDeduplicated([modalHash, modals], ([$hash, $modals]) => {
|
|
|
|
|
return getIdsFromHash($hash)
|
|
|
|
|
.map(id => $modals[id])
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
})
|
2026-08-28 02:31:29 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export const modal = deriveDeduplicated(modalStack, last)
|
|
|
|
|
|
|
|
|
|
// Base push which handles both modals and paths
|
|
|
|
|
|
|
|
|
|
export type PushParams = {
|
|
|
|
|
pathname?: string
|
|
|
|
|
search?: string
|
|
|
|
|
hash?: string
|
2026-08-28 00:37:36 +00:00
|
|
|
}
|
|
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export type PushOptions = Parameters<typeof goto>[1]
|
|
|
|
|
|
|
|
|
|
export const push = (params: PushParams, options: PushOptions) => {
|
|
|
|
|
const $page = get(page)
|
|
|
|
|
const $pathname = params.pathname ?? $page.url.pathname
|
|
|
|
|
const $search = params.search ?? $page.url.search
|
|
|
|
|
const $hash = params.hash ?? $page.url.hash
|
|
|
|
|
const url = $pathname + $search + $hash
|
2026-08-28 00:37:36 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
if (params.pathname !== $page.url.pathname) {
|
|
|
|
|
goto(url, options)
|
|
|
|
|
} else if (options.replaceState) {
|
2026-08-28 00:37:36 +00:00
|
|
|
history.replaceState(history.state, "", url)
|
|
|
|
|
} else {
|
|
|
|
|
history.pushState(history.state, "", url)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export const pop = () => history.back()
|
2026-09-08 17:23:22 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
// push/pop path, for navigations that clear modals and query params
|
2026-09-08 17:23:22 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export const pushPath = (pathname: string, options: PushOptions) =>
|
|
|
|
|
push({pathname, search: "", hash: ""}, options)
|
2026-09-09 19:24:44 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
export const popPath = async () => {
|
|
|
|
|
while (get(modal)) {
|
|
|
|
|
await history.back()
|
2026-09-09 19:24:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-09-08 17:23:22 +00:00
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
// push/pop/clear modal
|
2025-09-03 22:29:57 +00:00
|
|
|
|
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()
|
2026-08-28 00:37:36 +00:00
|
|
|
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
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
push({
|
|
|
|
|
hash: "#" + ids.join(","),
|
|
|
|
|
}, {
|
|
|
|
|
replaceState: Boolean(options.replaceState),
|
|
|
|
|
})
|
2024-08-06 20:10:10 +00:00
|
|
|
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 23:29:24 +00:00
|
|
|
export const popModal = () => {
|
2026-08-28 00:37:36 +00:00
|
|
|
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(",")
|
|
|
|
|
|
2026-09-10 15:44:21 +00:00
|
|
|
push({
|
|
|
|
|
hash: next ? `#${next}` : "",
|
|
|
|
|
}, {
|
|
|
|
|
replaceState: true,
|
|
|
|
|
})
|
2026-03-11 23:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-15 22:52:30 +00:00
|
|
|
export const clearModals = () => {
|
2026-09-10 15:44:21 +00:00
|
|
|
push({hash: ""}, {replaceState: 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
|
|
|
}
|
2026-09-10 15:44:21 +00:00
|
|
|
|
|
|
|
|
// Sync window history with the current hash directly. Svelte's navigation stuff
|
|
|
|
|
// is buggy, and results in double-mounted pages in dev.
|
|
|
|
|
call(() => {
|
|
|
|
|
window.addEventListener("popstate", () => modalHash.set(location.hash))
|
|
|
|
|
page.subscribe($page => modalHash.set($page.url?.hash ?? ""))
|
|
|
|
|
})
|