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 +20 -16
- package/dist/match.d.ts +1 -1
- package/dist/match.js +1 -1
- package/dist/router.svelte.d.ts +2 -2
- package/dist/router.svelte.js +1 -1
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# svelte-navigator-lite
|
|
2
2
|
|
|
3
|
+
[](https://github.com/jeremyjfleming/svelte-navigator-lite/actions/workflows/npm-publish-github-packages.yml)
|
|
4
|
+
[](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',
|
|
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.
|
|
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
|
-
##
|
|
102
|
+
## Route meta
|
|
100
103
|
|
|
101
|
-
|
|
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
|
-
<!--
|
|
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
|
-
`
|
|
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 `
|
|
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
package/dist/match.js
CHANGED
package/dist/router.svelte.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
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;
|
package/dist/router.svelte.js
CHANGED
|
@@ -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
|
|
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