Fix double-mounting of entire app when using goto with hashes in dev mode
This commit is contained in:
parent
9b094b85e1
commit
24a506ea85
1 changed files with 34 additions and 15 deletions
|
|
@ -2,7 +2,6 @@ import type {Component} from "svelte"
|
||||||
import {get, 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 {page} from "$app/stores"
|
import {page} from "$app/stores"
|
||||||
import type {DialogSize} from "@lib/components/Dialog.svelte"
|
import type {DialogSize} from "@lib/components/Dialog.svelte"
|
||||||
|
|
||||||
|
|
@ -29,14 +28,39 @@ export const modals = writable<Record<string, Modal>>({})
|
||||||
|
|
||||||
const getIdsFromHash = (hash: string) => hash.slice(1).split(",").filter(Boolean)
|
const getIdsFromHash = (hash: string) => hash.slice(1).split(",").filter(Boolean)
|
||||||
|
|
||||||
export const modalStack = deriveDeduplicated([page, modals], ([$page, $modals]) => {
|
// The modal stack lives in the url hash, but it is written with the History API directly rather
|
||||||
return getIdsFromHash($page.url.hash)
|
// than SvelteKit's `goto`. A programmatic hash `goto` runs a full navigation, which re-renders the
|
||||||
|
// whole layout tree and (in dev) leaves a second, orphaned app shell mounted; a History update does
|
||||||
|
// not navigate. `history.state` is carried through untouched, so SvelteKit's own popstate handler
|
||||||
|
// treats a back as a same-entry hash change and does nothing, while this store drives the open
|
||||||
|
// modals.
|
||||||
|
const modalHash = writable(typeof location === "undefined" ? "" : location.hash)
|
||||||
|
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
window.addEventListener("popstate", () => modalHash.set(location.hash))
|
||||||
|
}
|
||||||
|
|
||||||
|
const setModalHash = (hash: string, replace: boolean) => {
|
||||||
|
const {pathname, search} = get(page).url
|
||||||
|
const url = pathname + search + hash
|
||||||
|
|
||||||
|
if (replace) {
|
||||||
|
history.replaceState(history.state, "", url)
|
||||||
|
} else {
|
||||||
|
history.pushState(history.state, "", url)
|
||||||
|
}
|
||||||
|
|
||||||
|
modalHash.set(hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
export const modalStack = deriveDeduplicated([modalHash, modals], ([$hash, $modals]) => {
|
||||||
|
return getIdsFromHash($hash)
|
||||||
.map(id => $modals[id])
|
.map(id => $modals[id])
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
})
|
})
|
||||||
|
|
||||||
export const modal = deriveDeduplicated([page, modals], ([$page, $modals]) => {
|
export const modal = deriveDeduplicated([modalHash, modals], ([$hash, $modals]) => {
|
||||||
const ids = getIdsFromHash($page.url.hash)
|
const ids = getIdsFromHash($hash)
|
||||||
|
|
||||||
return $modals[ids.at(-1) || ""]
|
return $modals[ids.at(-1) || ""]
|
||||||
})
|
})
|
||||||
|
|
@ -47,13 +71,12 @@ export const pushModal = (
|
||||||
options: ModalOptions = {},
|
options: ModalOptions = {},
|
||||||
) => {
|
) => {
|
||||||
const id = randomId()
|
const id = randomId()
|
||||||
const path = options.path || ""
|
const existingIds = getIdsFromHash(get(modalHash))
|
||||||
const existingIds = getIdsFromHash(get(page).url.hash)
|
|
||||||
const ids = options.nested ? [...existingIds, id] : [id]
|
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 + "#" + ids.join(","), {replaceState: options.replaceState})
|
setModalHash("#" + ids.join(","), Boolean(options.replaceState))
|
||||||
|
|
||||||
return id
|
return id
|
||||||
}
|
}
|
||||||
|
|
@ -65,23 +88,19 @@ export const pushDrawer = (
|
||||||
) => pushModal(component, props, {...options, drawer: true})
|
) => pushModal(component, props, {...options, drawer: true})
|
||||||
|
|
||||||
export const popModal = () => {
|
export const popModal = () => {
|
||||||
const url = get(page).url
|
const ids = getIdsFromHash(get(modalHash))
|
||||||
const ids = getIdsFromHash(url.hash)
|
|
||||||
|
|
||||||
if (ids.length === 0) {
|
if (ids.length === 0) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const next = ids.slice(0, -1).join(",")
|
const next = ids.slice(0, -1).join(",")
|
||||||
const hash = next ? `#${next}` : ""
|
|
||||||
|
|
||||||
goto(url.pathname + url.search + hash, {replaceState: true})
|
setModalHash(next ? `#${next}` : "", true)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const clearModals = () => {
|
export const clearModals = () => {
|
||||||
const url = get(page).url
|
setModalHash("", true)
|
||||||
|
|
||||||
goto(url.pathname + url.search, {replaceState: true})
|
|
||||||
modals.update(always({}))
|
modals.update(always({}))
|
||||||
emitter.emit("close")
|
emitter.emit("close")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue