2024-10-15 22:13:11 +00:00
|
|
|
<script lang="ts">
|
2026-02-02 17:51:36 +00:00
|
|
|
import {onMount, mount, unmount} from "svelte"
|
2024-10-15 22:13:11 +00:00
|
|
|
import Drawer from "@lib/components/Drawer.svelte"
|
2024-10-15 22:52:30 +00:00
|
|
|
import Dialog from "@lib/components/Dialog.svelte"
|
2026-06-08 21:14:40 +00:00
|
|
|
import {modal, modalStack, popModal} from "@app/modal"
|
2024-10-15 22:13:11 +00:00
|
|
|
|
2026-03-11 23:29:24 +00:00
|
|
|
const closeModal = () => {
|
2025-11-05 00:14:32 +00:00
|
|
|
if ($modal && !$modal.options.noEscape) {
|
2026-03-11 23:29:24 +00:00
|
|
|
popModal()
|
2025-11-05 00:14:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-07 17:04:54 +00:00
|
|
|
const onKeyDown = (event: KeyboardEvent) => {
|
|
|
|
|
const target = event.target
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
event.code === "Escape" &&
|
|
|
|
|
target instanceof Element &&
|
|
|
|
|
!target.closest("input, textarea, [contenteditable]")
|
|
|
|
|
) {
|
2026-03-11 23:29:24 +00:00
|
|
|
closeModal()
|
2024-10-15 22:13:11 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-24 17:26:54 +00:00
|
|
|
let element: HTMLElement
|
2026-03-11 23:29:24 +00:00
|
|
|
const instances: Record<string, any> = {}
|
2025-10-24 17:26:54 +00:00
|
|
|
|
|
|
|
|
onMount(() => {
|
2026-03-11 23:29:24 +00:00
|
|
|
return modalStack.subscribe($modalStack => {
|
|
|
|
|
const ids = $modalStack.map(({id}) => id)
|
|
|
|
|
|
|
|
|
|
for (const [id, instance] of Object.entries(instances)) {
|
|
|
|
|
if (!ids.includes(id)) {
|
|
|
|
|
unmount(instance, {outro: true})
|
|
|
|
|
delete instances[id]
|
|
|
|
|
}
|
2025-10-24 17:26:54 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-11 23:29:24 +00:00
|
|
|
for (const item of $modalStack) {
|
|
|
|
|
if (instances[item.id]) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {options, component, props} = item
|
2025-10-24 17:26:54 +00:00
|
|
|
const wrapper = options.drawer ? Drawer : Dialog
|
|
|
|
|
|
2026-03-11 23:29:24 +00:00
|
|
|
instances[item.id] = mount(wrapper as any, {
|
2025-10-24 17:26:54 +00:00
|
|
|
target: element,
|
|
|
|
|
props: {
|
2026-03-11 23:29:24 +00:00
|
|
|
onClose: closeModal,
|
2026-07-15 16:30:17 +00:00
|
|
|
size: options.size,
|
2026-03-17 21:55:40 +00:00
|
|
|
noEscape: options.noEscape,
|
2025-11-13 18:32:37 +00:00
|
|
|
fullscreen: options.fullscreen,
|
2026-01-30 22:14:12 +00:00
|
|
|
children: {component, props},
|
2025-10-24 17:26:54 +00:00
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
2024-10-15 22:13:11 +00:00
|
|
|
</script>
|
|
|
|
|
|
2025-02-04 00:37:14 +00:00
|
|
|
<svelte:window onkeydown={onKeyDown} />
|
2024-10-15 22:52:30 +00:00
|
|
|
|
2026-09-08 17:23:22 +00:00
|
|
|
<div bind:this={element} data-sveltekit-replacestate="true"></div>
|