@snapstrat/switchboard 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -37,7 +37,11 @@ and displays the appropriate component based on the current route.
37
37
  setRouterContext(router);
38
38
 
39
39
  const currentAppRoute = $derived(router.currentRoute?.route);
40
+ const allRoutes = $derived(router.getAllRoutes());
40
41
  const layouts = $derived(getAllLayouts(currentAppRoute?.layout));
42
+ const lastLayout = $derived(
43
+ layouts.length > 0 ? layouts[layouts.length - 1] : undefined
44
+ );
41
45
  </script>
42
46
 
43
47
  <Route404>
@@ -49,16 +53,22 @@ and displays the appropriate component based on the current route.
49
53
 
50
54
  {#snippet layoutRender(remaining: LayoutData[])}
51
55
  {#if remaining.length === 0}
52
- {@render currentAppRoute?.component?.()}
56
+ <!-- We need to render all of these to ensure transitions and lifecycle hooks work correctly -->
57
+ {#each allRoutes.filter(it => it.layout === lastLayout) as route (route)}
58
+ {@render route?.component?.()}
59
+ {/each}
53
60
  {:else}
54
- {@const next = remaining[0]}
55
61
 
56
62
  {#snippet renderer()}
57
63
  {@render layoutRender(remaining.slice(1))}
58
64
  {/snippet}
59
65
 
60
- {@render next.renderer(renderer)}
66
+ {#each remaining as next (next)}
67
+ {#if next === remaining[0]}
68
+ {@render next.renderer(renderer)}
69
+ {/if}
70
+ {/each}
61
71
  {/if}
62
72
  {/snippet}
63
73
 
64
- {@render layoutRender(layouts)}
74
+ {@render layoutRender(layouts)}
@@ -15,7 +15,7 @@
15
15
  <script lang="ts">
16
16
 
17
17
  import {
18
- type ApplicationRoute, getAllCanonicalLayouts,
18
+ type ApplicationRoute, getAllCanonicalLayouts, getAllLayouts,
19
19
  getLayout,
20
20
  getRouter,
21
21
  type LayoutData,
@@ -74,6 +74,4 @@
74
74
  })
75
75
  </script>
76
76
 
77
-
78
-
79
77
  {@render routes()}
@@ -20,7 +20,7 @@ The children of this component make the content that will be displayed when the
20
20
  <script lang="ts">
21
21
  import { onDestroy, onMount } from 'svelte';
22
22
  import {
23
- type ApplicationRoute, getLayout, getRouteContainer, getRouter, type LayoutData, RoutePath
23
+ type ApplicationRoute, getAllLayouts, getLayout, getRouteContainer, getRouter, type LayoutData, RoutePath
24
24
  } from '..';
25
25
 
26
26
  let {
@@ -31,7 +31,7 @@ The children of this component make the content that will be displayed when the
31
31
 
32
32
  let router = getRouter();
33
33
 
34
- let route : ApplicationRoute | undefined;
34
+ let route : ApplicationRoute | undefined = $state.raw();
35
35
 
36
36
  onMount(() => {
37
37
  const layout = getLayout();
@@ -44,16 +44,24 @@ The children of this component make the content that will be displayed when the
44
44
 
45
45
  route = {
46
46
  path: RoutePath.fromString(combinedPath),
47
- component: children,
47
+ component: conditionalRender,
48
48
  layout: container.isRouter() ? undefined : container as LayoutData
49
49
  };
50
50
  container.registerRoute(route);
51
51
  });
52
52
 
53
- onDestroy(() => {
53
+ onDestroy(async () => {
54
54
  if (route) {
55
55
  const container = getLayout() ?? router;
56
56
  container.unregisterRoute(route);
57
57
  }
58
58
  })
59
+
60
+ const currentAppRoute = $derived(router.currentRoute?.route);
59
61
  </script>
62
+
63
+ {#snippet conditionalRender()}
64
+ {#if currentAppRoute === route}
65
+ {@render children?.()}
66
+ {/if}
67
+ {/snippet}
@@ -11,13 +11,21 @@
11
11
 
12
12
  <script lang="ts">
13
13
  import { onDestroy, onMount } from 'svelte';
14
- import { type ApplicationRoute, getLayout, getRouteContainer, getRouter, type LayoutData, RoutePath } from '..';
14
+ import {
15
+ type ApplicationRoute,
16
+ getAllLayouts,
17
+ getLayout,
18
+ getRouteContainer,
19
+ getRouter,
20
+ type LayoutData,
21
+ RoutePath
22
+ } from '..';
15
23
 
16
24
  let { children, container }: Route404Props = $props();
17
25
 
18
26
  let router = getRouter();
19
27
 
20
- let route : ApplicationRoute | undefined;
28
+ let route : ApplicationRoute | undefined = $state.raw();
21
29
 
22
30
  onMount(() => {
23
31
  const layout = getLayout();
@@ -28,7 +36,7 @@
28
36
 
29
37
  route = {
30
38
  path: RoutePath.fromString(layoutPath, true),
31
- component: children,
39
+ component: conditionalRender,
32
40
  layout: container.isRouter() ? undefined : container as LayoutData
33
41
  };
34
42
  container.registerRoute404(route);
@@ -40,4 +48,12 @@
40
48
  container.unregisterRoute(route);
41
49
  }
42
50
  })
43
- </script>
51
+
52
+ const currentAppRoute = $derived(router.currentRoute?.route);
53
+ </script>
54
+
55
+ {#snippet conditionalRender()}
56
+ {#if currentAppRoute === route}
57
+ {@render children?.()}
58
+ {/if}
59
+ {/snippet}
@@ -5,8 +5,8 @@ import { tick } from 'svelte';
5
5
  * A router used for an entire application on the web.
6
6
  */
7
7
  class WebRouter {
8
- _routes = [];
9
- _routes404 = [];
8
+ _routes = $state.raw([]);
9
+ _routes404 = $state.raw([]);
10
10
  // routes are stored in reverse order for easier matching
11
11
  // the most recently added routes are matched first
12
12
  get routes() {
@@ -88,10 +88,10 @@ class WebRouter {
88
88
  return selectedRoute;
89
89
  }
90
90
  registerRoute(route) {
91
- this._routes.push(route);
91
+ this._routes = [...this._routes, route];
92
92
  }
93
93
  registerRoute404(route) {
94
- this._routes404.push(route);
94
+ this._routes404 = [...this._routes404, route];
95
95
  // switch to the new route if it's a 404 route and the current route is undefined
96
96
  if (this.currentRoute?.route == undefined) {
97
97
  this.switchTo(window.location.pathname, parseWindowSearchParams(), false);
@@ -158,7 +158,7 @@ class WebRouter {
158
158
  this._routes.splice(toRemove, 1);
159
159
  }
160
160
  getAllRoutes() {
161
- return [...this.routes];
161
+ return [...this.routes, ...this.routes404];
162
162
  }
163
163
  isRouter() { return true; }
164
164
  }
package/package.json CHANGED
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "name": "@snapstrat/switchboard",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
+ "license": "MIT",
5
+ "homepage": "https://github.com/snapstrat/switchboard",
4
6
  "repository": {
5
7
  "url": "https://github.com/snapstrat/switchboard"
6
8
  },