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"> <script lang="ts">
import type {Snippet} from "svelte" import type {Snippet} from "svelte"
import {page} from "$app/stores"
import {pubkey} from "@welshman/app" import {pubkey} from "@welshman/app"
import Dialog from "@lib/components/Dialog.svelte" import Dialog from "@lib/components/Dialog.svelte"
import Landing from "@app/components/Landing.svelte" import Landing from "@app/components/Landing.svelte"
import Toast from "@app/components/Toast.svelte" import Toast from "@app/components/Toast.svelte"
import PrimaryNav from "@app/components/PrimaryNav.svelte" import PrimaryNav from "@app/components/PrimaryNav.svelte"
import {modals} from "@app/util/modal" import {modal} from "@app/util/modal"
interface Props { interface Props {
children: Snippet children: Snippet
@@ -20,7 +19,7 @@
<PrimaryNav> <PrimaryNav>
{@render children?.()} {@render children?.()}
</PrimaryNav> </PrimaryNav>
{:else if !$modals[$page.url.hash.slice(1)]} {:else if !$modal}
<Dialog children={{component: Landing, props: {}}} /> <Dialog children={{component: Landing, props: {}}} />
{/if} {/if}
</div> </div>
+21 -13
View File
@@ -2,38 +2,46 @@
import {onMount, mount, unmount} from "svelte" import {onMount, mount, unmount} from "svelte"
import Drawer from "@lib/components/Drawer.svelte" import Drawer from "@lib/components/Drawer.svelte"
import Dialog from "@lib/components/Dialog.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) { if ($modal && !$modal.options.noEscape) {
clearModals() popModal()
} }
} }
const onKeyDown = (e: any) => { const onKeyDown = (e: any) => {
if (e.code === "Escape" && e.target === document.body) { if (e.code === "Escape" && e.target === document.body) {
closeModals() closeModal()
} }
} }
let element: HTMLElement let element: HTMLElement
let instance: any | undefined const instances: Record<string, any> = {}
onMount(() => { onMount(() => {
return modal.subscribe($modal => { return modalStack.subscribe($modalStack => {
if (instance) { const ids = $modalStack.map(({id}) => id)
unmount(instance, {outro: true})
instance = undefined for (const [id, instance] of Object.entries(instances)) {
if (!ids.includes(id)) {
unmount(instance, {outro: true})
delete instances[id]
}
} }
if ($modal) { for (const item of $modalStack) {
const {options, component, props} = $modal if (instances[item.id]) {
continue
}
const {options, component, props} = item
const wrapper = options.drawer ? Drawer : Dialog const wrapper = options.drawer ? Drawer : Dialog
instance = mount(wrapper as any, { instances[item.id] = mount(wrapper as any, {
target: element, target: element,
props: { props: {
onClose: closeModals, onClose: closeModal,
fullscreen: options.fullscreen, fullscreen: options.fullscreen,
children: {component, props}, children: {component, props},
}, },
+33 -3
View File
@@ -1,5 +1,5 @@
import type {Component} from "svelte" import type {Component} from "svelte"
import {writable} from "svelte/store" import {get, writable} from "svelte/store"
import {randomId, always, assoc, Emitter} from "@welshman/lib" import {randomId, always, assoc, Emitter} from "@welshman/lib"
import {deriveDeduplicated} from "@welshman/store" import {deriveDeduplicated} from "@welshman/store"
import {goto} from "$app/navigation" import {goto} from "$app/navigation"
@@ -7,6 +7,7 @@ import {page} from "$app/stores"
export type ModalOptions = { export type ModalOptions = {
drawer?: boolean drawer?: boolean
nested?: boolean
noEscape?: boolean noEscape?: boolean
fullscreen?: boolean fullscreen?: boolean
replaceState?: boolean replaceState?: boolean
@@ -24,8 +25,18 @@ export const emitter = new Emitter()
export const modals = writable<Record<string, Modal>>({}) export const modals = writable<Record<string, Modal>>({})
const getIdsFromHash = (hash: string) => hash.slice(1).split(",").filter(Boolean)
export const modalStack = deriveDeduplicated([page, modals], ([$page, $modals]) => {
return getIdsFromHash($page.url.hash)
.map(id => $modals[id])
.filter(Boolean)
})
export const modal = deriveDeduplicated([page, modals], ([$page, $modals]) => { export const modal = deriveDeduplicated([page, modals], ([$page, $modals]) => {
return $modals[$page.url.hash.slice(1)] const ids = getIdsFromHash($page.url.hash)
return $modals[ids.at(-1) || ""]
}) })
export const pushModal = ( export const pushModal = (
@@ -35,10 +46,12 @@ export const pushModal = (
) => { ) => {
const id = randomId() const id = randomId()
const path = options.path || "" const path = options.path || ""
const existingIds = getIdsFromHash(get(page).url.hash)
const ids = options.nested ? [...existingIds, id] : [id]
modals.update(assoc(id, {id, component, props, options})) modals.update(assoc(id, {id, component, props, options}))
goto(path + "#" + id, {replaceState: options.replaceState}) goto(path + "#" + ids.join(","), {replaceState: options.replaceState})
return id return id
} }
@@ -49,7 +62,24 @@ export const pushDrawer = (
options: ModalOptions = {}, options: ModalOptions = {},
) => pushModal(component, props, {...options, drawer: true}) ) => pushModal(component, props, {...options, drawer: true})
export const popModal = () => {
const url = get(page).url
const ids = getIdsFromHash(url.hash)
if (ids.length === 0) {
return
}
const next = ids.slice(0, -1).join(",")
const hash = next ? `#${next}` : ""
goto(url.pathname + url.search + hash, {replaceState: true})
}
export const clearModals = () => { export const clearModals = () => {
const url = get(page).url
goto(url.pathname + url.search, {replaceState: true})
modals.update(always({})) modals.update(always({}))
emitter.emit("close") emitter.emit("close")
} }
+3 -15
View File
@@ -3,16 +3,16 @@
import {between, throttle} from "@welshman/lib" import {between, throttle} from "@welshman/lib"
import {isMobile} from "@lib/html" import {isMobile} from "@lib/html"
import Button from "@lib/components/Button.svelte" import Button from "@lib/components/Button.svelte"
import Dialog from "@lib/components/Dialog.svelte"
import Tippy from "@lib/components/Tippy.svelte" import Tippy from "@lib/components/Tippy.svelte"
import IconPickerModal from "@app/components/IconPickerModal.svelte" import IconPickerModal from "@app/components/IconPickerModal.svelte"
import IconPickerPopover from "@app/components/IconPickerPopover.svelte" import IconPickerPopover from "@app/components/IconPickerPopover.svelte"
import {pushModal, popModal} from "@app/util/modal"
const {...props} = $props() const {...props} = $props()
const open = () => { const open = () => {
if (isMobile) { if (isMobile) {
showIconPicker = true pushModal(IconPickerModal, {onSelect: onClick}, {nested: true})
} else { } else {
popover?.show() popover?.show()
} }
@@ -20,7 +20,7 @@
const close = () => { const close = () => {
if (isMobile) { if (isMobile) {
showIconPicker = false popModal()
} else { } else {
popover?.hide() popover?.hide()
} }
@@ -41,7 +41,6 @@
} }
}) })
let showIconPicker = $state(false)
let popover: Instance | undefined = $state() let popover: Instance | undefined = $state()
</script> </script>
@@ -56,14 +55,3 @@
{@render props.children?.()} {@render props.children?.()}
</Button> </Button>
</Tippy> </Tippy>
{#if showIconPicker}
<Dialog
onClose={close}
children={{
component: IconPickerModal,
props: {
onSelect: onClick,
},
}} />
{/if}