Files
flotilla/src/app/components/ModalContainer.svelte
T
2026-03-17 14:55:40 -07:00

58 lines
1.4 KiB
Svelte

<script lang="ts">
import {onMount, mount, unmount} from "svelte"
import Drawer from "@lib/components/Drawer.svelte"
import Dialog from "@lib/components/Dialog.svelte"
import {modal, modalStack, popModal} from "@app/util/modal"
const closeModal = () => {
if ($modal && !$modal.options.noEscape) {
popModal()
}
}
const onKeyDown = (e: any) => {
if (e.code === "Escape" && e.target === document.body) {
closeModal()
}
}
let element: HTMLElement
const instances: Record<string, any> = {}
onMount(() => {
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]
}
}
for (const item of $modalStack) {
if (instances[item.id]) {
continue
}
const {options, component, props} = item
const wrapper = options.drawer ? Drawer : Dialog
instances[item.id] = mount(wrapper as any, {
target: element,
props: {
onClose: closeModal,
noEscape: options.noEscape,
fullscreen: options.fullscreen,
children: {component, props},
},
})
}
})
})
</script>
<svelte:window onkeydown={onKeyDown} />
<div bind:this={element}></div>