diff --git a/src/app.d.ts b/src/app.d.ts index 609b8040..a3c055e3 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -23,7 +23,8 @@ declare global { } } interface PageState { - openImage: url + openImage?: string + openModals?: string[] } // interface Platform {} } diff --git a/src/lib/mono/modal/Modal.svelte b/src/lib/mono/modal/Modal.svelte index 1fef377a..1f4918e9 100644 --- a/src/lib/mono/modal/Modal.svelte +++ b/src/lib/mono/modal/Modal.svelte @@ -7,6 +7,8 @@ import { Button } from '../index.js' import Portal from '../popover/Portal.svelte' import type { Snippet } from 'svelte' + import { page } from '$app/state' + import { pushState, replaceState } from '$app/navigation' interface Props { action?: string | undefined @@ -34,7 +36,47 @@ class: clazz = '', }: Props = $props() + const modalId = crypto.randomUUID() + let el = $state() + let hasHistoryEntry = false + + $effect(() => { + console.log(modalId, open, hasHistoryEntry) + }) + + $effect(() => { + if (open && modalId && !hasHistoryEntry) { + pushState('', { + openModals: [...(page.state.openModals ?? []), modalId], + }) + hasHistoryEntry = true + } + }) + + $effect(() => { + if (modalId && hasHistoryEntry) { + const currentModals = page.state.openModals ?? [] + const isInHistory = currentModals.includes(modalId) + + if (!isInHistory && open) { + replaceState('', { + openModals: currentModals.filter(id => id !== modalId), + }) + hasHistoryEntry = false + open = false + } + } + }) + + function onclose() { + open = false + + if ((page.state.openModals ?? []).includes(modalId)) history.back() + hasHistoryEntry = false + + ondismissed?.() + } @@ -47,11 +89,7 @@ bg-white/50 dark:bg-black/50 box-border p-4" transition:fade|global={{ duration: 100 }} onclick={e => { // @ts-expect-error html node hell - if (!el.contains(e.target)) { - open = false - - ondismissed?.() - } + if (!el.contains(e.target)) onclose() }} >
{ - open = false - ondismissed?.() - }} + onclick={() => onclose()} > {#snippet prefix()} @@ -100,8 +135,7 @@ bg-white/50 dark:bg-black/50 box-border p-4" class="w-full" onclick={() => { onaction?.() - open = false - ondismissed?.() + onclose() }} color="primary" size="lg"