feat: add a modal backstack (#591)
This commit is contained in:
1 parent
04a9b37c6f
commit
7f60470290
2 files changed
+47
-12
No files matched your search
Vendored
+2
-1
@@ -23,7 +23,8 @@ declare global {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
interface PageState {
|
interface PageState {
|
||||||
openImage: url
|
openImage?: string
|
||||||
|
openModals?: string[]
|
||||||
}
|
}
|
||||||
// interface Platform {}
|
// interface Platform {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,8 @@
|
|||||||
import { Button } from '../index.js'
|
import { Button } from '../index.js'
|
||||||
import Portal from '../popover/Portal.svelte'
|
import Portal from '../popover/Portal.svelte'
|
||||||
import type { Snippet } from 'svelte'
|
import type { Snippet } from 'svelte'
|
||||||
|
import { page } from '$app/state'
|
||||||
|
import { pushState, replaceState } from '$app/navigation'
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
action?: string | undefined
|
action?: string | undefined
|
||||||
@@ -34,7 +36,47 @@
|
|||||||
class: clazz = '',
|
class: clazz = '',
|
||||||
}: Props = $props()
|
}: Props = $props()
|
||||||
|
|
||||||
|
const modalId = crypto.randomUUID()
|
||||||
|
|
||||||
let el = $state<HTMLElement>()
|
let el = $state<HTMLElement>()
|
||||||
|
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?.()
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<Portal>
|
<Portal>
|
||||||
@@ -47,11 +89,7 @@ bg-white/50 dark:bg-black/50 box-border p-4"
|
|||||||
transition:fade|global={{ duration: 100 }}
|
transition:fade|global={{ duration: 100 }}
|
||||||
onclick={e => {
|
onclick={e => {
|
||||||
// @ts-expect-error html node hell
|
// @ts-expect-error html node hell
|
||||||
if (!el.contains(e.target)) {
|
if (!el.contains(e.target)) onclose()
|
||||||
open = false
|
|
||||||
|
|
||||||
ondismissed?.()
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<div
|
<div
|
||||||
@@ -73,10 +111,7 @@ bg-white/50 dark:bg-black/50 box-border p-4"
|
|||||||
class="absolute top-0 right-0 m-2 text-slate-600 dark:text-zinc-400"
|
class="absolute top-0 right-0 m-2 text-slate-600 dark:text-zinc-400"
|
||||||
color="tertiary"
|
color="tertiary"
|
||||||
size="square-sm"
|
size="square-sm"
|
||||||
onclick={() => {
|
onclick={() => onclose()}
|
||||||
open = false
|
|
||||||
ondismissed?.()
|
|
||||||
}}
|
|
||||||
>
|
>
|
||||||
{#snippet prefix()}
|
{#snippet prefix()}
|
||||||
<Icon src={XMark} size="20" mini />
|
<Icon src={XMark} size="20" mini />
|
||||||
@@ -100,8 +135,7 @@ bg-white/50 dark:bg-black/50 box-border p-4"
|
|||||||
class="w-full"
|
class="w-full"
|
||||||
onclick={() => {
|
onclick={() => {
|
||||||
onaction?.()
|
onaction?.()
|
||||||
open = false
|
onclose()
|
||||||
ondismissed?.()
|
|
||||||
}}
|
}}
|
||||||
color="primary"
|
color="primary"
|
||||||
size="lg"
|
size="lg"
|
||||||
|
|||||||
Reference in new issue
Block a user