Allow nested modals

This commit is contained in:
Jon Staab
2026-03-11 16:29:24 -07:00
parent bfc997ba37
commit 19f532c12e
4 changed files with 59 additions and 34 deletions
+2 -3
View File
@@ -1,12 +1,11 @@
<script lang="ts">
import type {Snippet} from "svelte"
import {page} from "$app/stores"
import {pubkey} from "@welshman/app"
import Dialog from "@lib/components/Dialog.svelte"
import Landing from "@app/components/Landing.svelte"
import Toast from "@app/components/Toast.svelte"
import PrimaryNav from "@app/components/PrimaryNav.svelte"
import {modals} from "@app/util/modal"
import {modal} from "@app/util/modal"
interface Props {
children: Snippet
@@ -20,7 +19,7 @@
<PrimaryNav>
{@render children?.()}
</PrimaryNav>
{:else if !$modals[$page.url.hash.slice(1)]}
{:else if !$modal}
<Dialog children={{component: Landing, props: {}}} />
{/if}
</div>
+21 -13
View File
@@ -2,38 +2,46 @@
import {onMount, mount, unmount} from "svelte"
import Drawer from "@lib/components/Drawer.svelte"
import Dialog from "@lib/components/Dialog.svelte"
import {modal, clearModals} from "@app/util/modal"
import {modal, modalStack, popModal} from "@app/util/modal"
const closeModals = () => {
const closeModal = () => {
if ($modal && !$modal.options.noEscape) {
clearModals()
popModal()
}
}
const onKeyDown = (e: any) => {
if (e.code === "Escape" && e.target === document.body) {
closeModals()
closeModal()
}
}
let element: HTMLElement
let instance: any | undefined
const instances: Record<string, any> = {}
onMount(() => {
return modal.subscribe($modal => {
if (instance) {
unmount(instance, {outro: true})
instance = undefined
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]
}
}
if ($modal) {
const {options, component, props} = $modal
for (const item of $modalStack) {
if (instances[item.id]) {
continue
}
const {options, component, props} = item
const wrapper = options.drawer ? Drawer : Dialog
instance = mount(wrapper as any, {
instances[item.id] = mount(wrapper as any, {
target: element,
props: {
onClose: closeModals,
onClose: closeModal,
fullscreen: options.fullscreen,
children: {component, props},
},