2024-08-06 20:10:10 +00:00
|
|
|
import type {ComponentType} from "svelte"
|
2024-08-09 23:22:09 +00:00
|
|
|
import {randomId, Emitter} from "@welshman/lib"
|
2024-08-09 00:22:17 +00:00
|
|
|
import {goto} from "$app/navigation"
|
2024-08-06 20:10:10 +00:00
|
|
|
|
2024-08-09 23:22:09 +00:00
|
|
|
export const emitter = new Emitter()
|
|
|
|
|
|
2024-08-06 20:10:10 +00:00
|
|
|
export const modals = new Map()
|
|
|
|
|
|
2024-10-01 18:47:21 +00:00
|
|
|
export type ModalOptions = {
|
|
|
|
|
drawer?: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const pushModal = (component: ComponentType, props: Record<string, any> = {}, options: ModalOptions = {}) => {
|
2024-08-06 20:10:10 +00:00
|
|
|
const id = randomId()
|
|
|
|
|
|
|
|
|
|
// TODO: fix memory leak here by listening to history somehow
|
2024-10-01 18:47:21 +00:00
|
|
|
modals.set(id, {component, props, options})
|
2024-08-09 00:22:17 +00:00
|
|
|
goto("#" + id)
|
2024-08-06 20:10:10 +00:00
|
|
|
|
|
|
|
|
return id
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-09 23:22:09 +00:00
|
|
|
export const clearModal = () => {
|
2024-08-16 17:50:38 +00:00
|
|
|
goto("#")
|
|
|
|
|
emitter.emit("close")
|
2024-08-09 23:22:09 +00:00
|
|
|
}
|