diff --git a/package.json b/package.json index 19a507e..6cacfa4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@snapstrat/switchboard", - "version": "1.0.0", + "version": "1.0.1", "scripts": { "dev": "vite dev", "build": "vite build && npm run package", diff --git a/src/lib/router/BrowserRouter.svelte b/src/lib/router/BrowserRouter.svelte index 63f76f9..38e051e 100644 --- a/src/lib/router/BrowserRouter.svelte +++ b/src/lib/router/BrowserRouter.svelte @@ -37,7 +37,11 @@ and displays the appropriate component based on the current route. setRouterContext(router); const currentAppRoute = $derived(router.currentRoute?.route); + const allRoutes = $derived(router.getAllRoutes()); const layouts = $derived(getAllLayouts(currentAppRoute?.layout)); + const lastLayout = $derived( + layouts.length > 0 ? layouts[layouts.length - 1] : undefined + ); @@ -49,16 +53,22 @@ and displays the appropriate component based on the current route. {#snippet layoutRender(remaining: LayoutData[])} {#if remaining.length === 0} - {@render currentAppRoute?.component?.()} + + {#each allRoutes.filter(it => it.layout === lastLayout) as route (route)} + {@render route?.component?.()} + {/each} {:else} - {@const next = remaining[0]} {#snippet renderer()} {@render layoutRender(remaining.slice(1))} {/snippet} - {@render next.renderer(renderer)} + {#each remaining as next (next)} + {#if next === remaining[0]} + {@render next.renderer(renderer)} + {/if} + {/each} {/if} {/snippet} -{@render layoutRender(layouts)} +{@render layoutRender(layouts)} \ No newline at end of file diff --git a/src/lib/router/Layout.svelte b/src/lib/router/Layout.svelte index ef84264..2b7fdd9 100644 --- a/src/lib/router/Layout.svelte +++ b/src/lib/router/Layout.svelte @@ -15,7 +15,7 @@ - - {@render routes()} \ No newline at end of file diff --git a/src/lib/router/Route.svelte b/src/lib/router/Route.svelte index 0d52019..51cad8f 100644 --- a/src/lib/router/Route.svelte +++ b/src/lib/router/Route.svelte @@ -20,7 +20,7 @@ The children of this component make the content that will be displayed when the + +{#snippet conditionalRender()} + {#if currentAppRoute === route} + {@render children?.()} + {/if} +{/snippet} \ No newline at end of file diff --git a/src/lib/router/Route404.svelte b/src/lib/router/Route404.svelte index e6bc500..0fff73f 100644 --- a/src/lib/router/Route404.svelte +++ b/src/lib/router/Route404.svelte @@ -11,13 +11,21 @@ \ No newline at end of file + + const currentAppRoute = $derived(router.currentRoute?.route); + + +{#snippet conditionalRender()} + {#if currentAppRoute === route} + {@render children?.()} + {/if} +{/snippet} \ No newline at end of file diff --git a/src/lib/router/impl/webRouter.svelte.ts b/src/lib/router/impl/webRouter.svelte.ts index 1f0da8c..cc5812c 100644 --- a/src/lib/router/impl/webRouter.svelte.ts +++ b/src/lib/router/impl/webRouter.svelte.ts @@ -19,8 +19,8 @@ type HistoryState = { * A router used for an entire application on the web. */ class WebRouter implements Router { - private readonly _routes: ApplicationRoute[] = []; - private readonly _routes404: ApplicationRoute[] = []; + private _routes: ApplicationRoute[] = $state.raw([]); + private _routes404: ApplicationRoute[] = $state.raw([]); // routes are stored in reverse order for easier matching // the most recently added routes are matched first @@ -59,6 +59,7 @@ class WebRouter implements Router { public get params(): RouteParams { return this.currentRoute?.queryParams ?? {}; } + public get queryParams(): RouteParams { return this.currentRoute?.queryParams ?? {}; } @@ -127,11 +128,12 @@ class WebRouter implements Router { } public registerRoute(route: ApplicationRoute): void { - this._routes.push(route); + this._routes = [...this._routes, route]; } public registerRoute404(route: ApplicationRoute): void { - this._routes404.push(route); + this._routes404 = [...this._routes404, route]; + // switch to the new route if it's a 404 route and the current route is undefined if (this.currentRoute?.route == undefined) { @@ -209,7 +211,7 @@ class WebRouter implements Router { } public getAllRoutes(): ApplicationRoute[] { - return [...this.routes]; + return [...this.routes, ...this.routes404]; } isRouter(): this is Router { return true; } diff --git a/src/routes/[...any]/+page.svelte b/src/routes/[...any]/+page.svelte index 049e6dd..1f53842 100644 --- a/src/routes/[...any]/+page.svelte +++ b/src/routes/[...any]/+page.svelte @@ -6,6 +6,7 @@ import { PageInfo } from '$lib'; import PersistenceLayout from './PersistenceLayout.svelte'; import EscapingLayouts from './EscapingLayouts.svelte'; + import { fly } from 'svelte/transition'; const router = createWebRouter() @@ -49,6 +50,18 @@ As such, it has some pretty esoteric routes and components for the sake of testi {@render linkTo("/route-params/foo/bar")} + +
+ helo go to 2 +
+
+ + +
+ helo from 2 go to 1 +
+
+ {@render identify('links-component-start')} Go to /links/component/finish