flotilla/src/app/modal.ts

99 lines
2.6 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"
import {randomId, last, always, assoc, Emitter} from "@welshman/lib"
import {deriveDeduplicated} from "@welshman/store"
import {goto, pushState, replaceState} from "$app/navigation"
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>>({})
// Open modal ids live in SvelteKit's page state (shallow routing): each modal owns a history entry
// without a navigation, and any `goto` that doesn't pass `state` along closes them.
export const modalStack = deriveDeduplicated([page, modals], ([$page, $modals]) =>
($page.state?.modals ?? []).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)
export type NavigateOptions = Parameters<typeof goto>[1] & {keepModal?: boolean}
2026-09-10 15:44:21 +00:00
// An open modal owns the current history entry, so a navigation that drops it takes that entry over
export const navigate = (path: string, {keepModal, ...options}: NavigateOptions = {}) => {
const {state} = get(page)
const modalIsOpen = Boolean(state.modals?.length)
if (keepModal && modalIsOpen) {
return goto(path, {...options, state, replaceState: true})
}
return goto(path, {...options, replaceState: options.replaceState || modalIsOpen})
}
2026-09-10 15:44:21 +00:00
export const pop = () => history.back()
2026-09-10 15:44:21 +00:00
export const popPath = async () => {
while (get(modal)) {
const popped = new Promise(resolve =>
window.addEventListener("popstate", resolve, {once: true}),
)
history.back()
await popped
}
}
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 = get(page).state.modals ?? []
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
if (options.replaceState) {
replaceState("", {modals: ids})
} else {
pushState("", {modals: ids})
}
2024-08-06 20:10:10 +00:00
return id
}
2026-03-11 23:29:24 +00:00
export const popModal = () => {
const ids = get(page).state.modals ?? []
2026-03-11 23:29:24 +00:00
if (ids.length > 0) {
replaceState("", {modals: ids.slice(0, -1)})
2026-03-11 23:29:24 +00:00
}
}
2024-10-15 22:52:30 +00:00
export const clearModals = () => {
replaceState("", {})
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
}