flotilla/src/app/components/ModalContainer.svelte

39 lines
961 B
Svelte
Raw Normal View History

<script lang="ts">
import {page} from "$app/stores"
import Drawer from "@lib/components/Drawer.svelte"
2024-10-15 22:52:30 +00:00
import Dialog from "@lib/components/Dialog.svelte"
import {modals, clearModals} from "@app/modal"
2024-10-15 22:52:30 +00:00
const onKeyDown = (e: any) => {
if (e.code === "Escape" && e.target === document.body) {
clearModals()
}
}
2025-02-04 00:37:14 +00:00
let modal: any = $state()
const hash = $derived($page.url.hash.slice(1))
const hashIsValid = $derived(Boolean($modals[hash]))
2024-10-15 22:52:30 +00:00
2025-02-04 00:37:14 +00:00
$effect(() => {
if ($modals[hash]) {
modal = $modals[hash]
}
})
</script>
2025-02-04 00:37:14 +00:00
<svelte:window onkeydown={onKeyDown} />
2024-10-15 22:52:30 +00:00
{#if hashIsValid && modal?.options?.drawer}
2024-10-17 22:02:03 +00:00
<Drawer onClose={clearModals} {...modal.options}>
2024-10-15 22:52:30 +00:00
{#key modal.id}
2025-02-04 00:37:14 +00:00
<modal.component {...modal.props} />
{/key}
2024-10-15 22:52:30 +00:00
</Drawer>
{:else if hashIsValid && modal}
2024-10-17 22:02:03 +00:00
<Dialog onClose={clearModals} {...modal.options}>
2024-10-15 22:52:30 +00:00
{#key modal.id}
2025-02-04 00:37:14 +00:00
<modal.component {...modal.props} />
{/key}
2024-10-15 22:52:30 +00:00
</Dialog>
{/if}