svelte-navigator-lite 2.1.0 → 2.2.0

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.
package/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # svelte-navigator-lite
2
2
 
3
+ [![Publish Package](https://github.com/jeremyjfleming/svelte-navigator-lite/actions/workflows/npm-publish-github-packages.yml/badge.svg)](https://github.com/jeremyjfleming/svelte-navigator-lite/actions/workflows/npm-publish-github-packages.yml)
4
+ [![npm version](https://img.shields.io/npm/v/svelte-navigator-lite)](https://www.npmjs.com/package/svelte-navigator-lite)
5
+
3
6
  A lightweight, pattern-based router for Svelte 5. Routes are defined with familiar URL patterns, guards are reusable named functions, and the reactive `router` singleton integrates directly with Svelte runes.
4
7
 
5
8
  ## Installation
@@ -28,10 +31,10 @@ Define your routes and guards, then call `createRouter` on mount.
28
31
  routes: [
29
32
  { name: 'cal', pattern: '/cal' },
30
33
  { name: 'cal-date', pattern: '/cal/:mm/:dd/:yyyy' },
31
- { name: 'event', pattern: '/event/:eventId', overlay: true },
32
- { name: 'edit', pattern: '/event/:eventId/edit', overlay: true },
33
- { name: 'login', pattern: '/login', guards: ['unauth'], overlay: true },
34
- { name: 'signup', pattern: '/signup', guards: ['unauth'], overlay: true },
34
+ { name: 'event', pattern: '/event/:eventId', meta: { overlay: true } },
35
+ { name: 'edit', pattern: '/event/:eventId/edit', meta: { overlay: true } },
36
+ { name: 'login', pattern: '/login', guards: ['unauth'], meta: { overlay: true } },
37
+ { name: 'signup', pattern: '/signup', guards: ['unauth'], meta: { overlay: true } },
35
38
  ],
36
39
  guards: {
37
40
  auth: { condition: () => !auth.isValid(), redirectTo: 'login' },
@@ -43,7 +46,7 @@ Define your routes and guards, then call `createRouter` on mount.
43
46
  </script>
44
47
 
45
48
  <AppShell />
46
- {#if router.overlay}
49
+ {#if router.meta?.overlay}
47
50
  <svelte:component this={currentOverlay} />
48
51
  {/if}
49
52
  ```
@@ -87,7 +90,7 @@ import { router } from 'svelte-navigator-lite';
87
90
  router.route // current route name: string
88
91
  router.params // path params: Record<string, string>
89
92
  router.searchParams // query params: Record<string, string>
90
- router.overlay // true if the current route has overlay: true
93
+ router.meta // meta object for the current route (or undefined)
91
94
  router.notFound // true if the URL matched no route and the fallback was used
92
95
 
93
96
  router.is('cal') // true if current route === 'cal'
@@ -96,15 +99,16 @@ router.matches(['cal', 'event']) // true if current route is in the list
96
99
 
97
100
  All properties are reactive — use them in Svelte templates or `$effect` blocks and they update automatically on navigation.
98
101
 
99
- ## Overlays
102
+ ## Route meta
100
103
 
101
- Mark a route with `overlay: true` to indicate it renders on top of the base layout rather than replacing it. The router exposes `router.overlay` so your root component can conditionally render the overlay without any separate bookkeeping.
104
+ Attach arbitrary metadata to any route via the `meta` field. The router exposes `router.meta` so your components can read it reactively.
102
105
 
103
106
  ```typescript
104
107
  routes: [
105
- { name: 'cal', pattern: '/cal' },
106
- { name: 'event', pattern: '/event/:eventId', overlay: true },
107
- { name: 'edit', pattern: '/event/:eventId/edit', overlay: true },
108
+ { name: 'cal', pattern: '/cal' },
109
+ { name: 'event', pattern: '/event/:eventId', meta: { overlay: true } },
110
+ { name: 'edit', pattern: '/event/:eventId/edit', meta: { overlay: true } },
111
+ { name: 'admin', pattern: '/admin', meta: { role: 'admin' } },
108
112
  ]
109
113
  ```
110
114
 
@@ -112,13 +116,13 @@ routes: [
112
116
  <!-- The base layout always renders -->
113
117
  <AppShell />
114
118
 
115
- <!-- Overlay components mount on top when their route is active -->
116
- {#if router.overlay}
119
+ <!-- Use any meta value to drive rendering -->
120
+ {#if router.meta?.overlay}
117
121
  <svelte:component this={overlayMap[router.route]} />
118
122
  {/if}
119
123
  ```
120
124
 
121
- `overlay` is purely a metadata flag — the router does not render anything itself. How overlays are displayed (modal, drawer, fullscreen panel, etc.) is entirely up to your components.
125
+ `meta` is not interpreted by the router it is plain data for your components to use however they need.
122
126
 
123
127
  ## Guards
124
128
 
@@ -181,7 +185,7 @@ await goto('/some/path', { replaceState: true }); // replace instead of push
181
185
 
182
186
  ## Migrating from v1
183
187
 
184
- The `rootPath` + `segments` route definition is replaced by a single `pattern` string, `routeGuards` inline on each route are replaced by named guards defined once, and the new `overlay` flag replaces any separate modal-route maps you maintained manually.
188
+ The `rootPath` + `segments` route definition is replaced by a single `pattern` string, `routeGuards` inline on each route are replaced by named guards defined once, and the new `meta` field replaces any separate modal-route maps you maintained manually.
185
189
 
186
190
  ```typescript
187
191
  // v1
@@ -192,6 +196,6 @@ The `rootPath` + `segments` route definition is replaced by a single `pattern` s
192
196
  }
193
197
 
194
198
  // v2
195
- { name: 'edit', pattern: '/event/:eventId/edit', guards: ['auth'], overlay: true }
199
+ { name: 'edit', pattern: '/event/:eventId/edit', guards: ['auth'], meta: { overlay: true } }
196
200
  // guard defined once: auth: { condition: () => !auth.isValid(), redirectTo: 'login' }
197
201
  ```
package/dist/match.d.ts CHANGED
@@ -5,7 +5,7 @@ type CompiledRoute = {
5
5
  regex: RegExp;
6
6
  paramNames: string[];
7
7
  guards: string[];
8
- overlay: boolean;
8
+ meta: Record<string, unknown> | undefined;
9
9
  };
10
10
  export type RouteMatch = {
11
11
  name: string;
package/dist/match.js CHANGED
@@ -20,7 +20,7 @@ export function compileRoutes(routes) {
20
20
  regex: new RegExp(`^${regexStr}/?$`),
21
21
  paramNames,
22
22
  guards: route.guards ?? [],
23
- overlay: route.overlay ?? false,
23
+ meta: route.meta,
24
24
  };
25
25
  });
26
26
  }
@@ -10,7 +10,7 @@ declare function _createRouter(): {
10
10
  readonly params: Record<string, string>;
11
11
  readonly searchParams: Record<string, string>;
12
12
  readonly notFound: boolean;
13
- readonly overlay: boolean;
13
+ readonly meta: Record<string, unknown> | undefined;
14
14
  is(route: string): boolean;
15
15
  matches(routes: string[]): boolean;
16
16
  parseUrl: (url: string) => void;
@@ -23,7 +23,7 @@ export declare const router: {
23
23
  readonly params: Record<string, string>;
24
24
  readonly searchParams: Record<string, string>;
25
25
  readonly notFound: boolean;
26
- readonly overlay: boolean;
26
+ readonly meta: Record<string, unknown> | undefined;
27
27
  is(route: string): boolean;
28
28
  matches(routes: string[]): boolean;
29
29
  parseUrl: (url: string) => void;
@@ -70,7 +70,7 @@ function _createRouter() {
70
70
  get params() { return state.params; },
71
71
  get searchParams() { return state.searchParams; },
72
72
  get notFound() { return state.notFound; },
73
- get overlay() { return compiled.find(r => r.name === state.current)?.overlay ?? false; },
73
+ get meta() { return compiled.find(r => r.name === state.current)?.meta; },
74
74
  is(route) { return state.current === route; },
75
75
  matches(routes) { return routes.includes(state.current); },
76
76
  parseUrl,
package/dist/types.d.ts CHANGED
@@ -2,7 +2,7 @@ export type RouteDefinition = {
2
2
  name: string;
3
3
  pattern: string;
4
4
  guards?: string[];
5
- overlay?: boolean;
5
+ meta?: Record<string, unknown>;
6
6
  };
7
7
  export type Guard = {
8
8
  condition: () => boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "svelte-navigator-lite",
3
- "version": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "A lightweight router for Svelte 5",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",