lit-navigation-router 0.5.0 → 0.6.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/CHANGELOG.md +33 -7
- package/README.md +64 -30
- package/development/router.d.ts +17 -50
- package/development/router.d.ts.map +1 -1
- package/development/router.js +34 -97
- package/development/router.js.map +1 -1
- package/development/routes.d.ts +57 -113
- package/development/routes.d.ts.map +1 -1
- package/development/routes.js +276 -205
- package/development/routes.js.map +1 -1
- package/package.json +6 -6
- package/src/router.ts +37 -101
- package/src/routes.ts +334 -240
package/development/routes.d.ts
CHANGED
|
@@ -23,39 +23,23 @@ export interface PathRouteConfig extends BaseRouteConfig {
|
|
|
23
23
|
path: string;
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* A RouteConfig that matches against a given [`URLPattern`](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern)
|
|
26
|
+
* A RouteConfig that matches against a given [`URLPattern`](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern).
|
|
27
27
|
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* origin. This means the pattern is limited to checking `pathname`, `search`
|
|
31
|
-
* and `hash`.
|
|
28
|
+
* Routes are only checked within the current origin, so only `pathname`,
|
|
29
|
+
* `search` and `hash` are matched.
|
|
32
30
|
*/
|
|
33
31
|
export interface URLPatternRouteConfig extends BaseRouteConfig {
|
|
34
32
|
pattern: URLPatternLike;
|
|
35
33
|
}
|
|
36
34
|
/**
|
|
37
|
-
* The part of `URLPattern` this router uses.
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* `.d.ts` is self-contained: the `/// <reference types="urlpattern-polyfill" />`
|
|
41
|
-
* above is not carried into declaration output, and `URLPattern` is not in
|
|
42
|
-
* TypeScript's bundled `lib.dom`, so a published package typed against the
|
|
43
|
-
* global fails a consumer build with `TS2304: Cannot find name 'URLPattern'`.
|
|
44
|
-
* A real `URLPattern` satisfies this, so passing one still type-checks.
|
|
35
|
+
* The part of `URLPattern` this router uses. Structural, not a reference to the
|
|
36
|
+
* global: `URLPattern` is not in TypeScript's `lib.dom`, so typing against it
|
|
37
|
+
* fails a consumer build with `TS2304`.
|
|
45
38
|
*/
|
|
46
39
|
export interface URLPatternLike {
|
|
47
|
-
/**
|
|
48
|
-
* The pathname pattern string, as `URLPattern.prototype.pathname` returns
|
|
49
|
-
* it. Read to tell a trailing wildcard from any other positional group —
|
|
50
|
-
* the groups object alone cannot (see `tailOf`).
|
|
51
|
-
*/
|
|
40
|
+
/** Read by `tailOf`, which the groups object alone cannot answer. */
|
|
52
41
|
readonly pathname: string;
|
|
53
|
-
/**
|
|
54
|
-
* The hash pattern string. `'*'` means the route places no constraint on the
|
|
55
|
-
* fragment, which is what `URLPattern` fills in for any component the caller
|
|
56
|
-
* omits. `Router` reads this to decide whether a fragment-only navigation is
|
|
57
|
-
* its business or the browser's.
|
|
58
|
-
*/
|
|
42
|
+
/** `'*'` means unconstrained. `Router` gates hash interception on this. */
|
|
59
43
|
readonly hash: string;
|
|
60
44
|
test(input: RouteLocation): boolean;
|
|
61
45
|
exec(input: RouteLocation): {
|
|
@@ -77,23 +61,15 @@ export interface URLPatternLike {
|
|
|
77
61
|
} | null;
|
|
78
62
|
}
|
|
79
63
|
/**
|
|
80
|
-
* The parts of a location this router matches against
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
* `search` and `hash` carry no leading `?` or `#`. `URLPattern` canonicalises
|
|
84
|
-
* either form away on an init input, but only for a real `URLPattern` — the
|
|
85
|
-
* structural `URLPatternLike` above admits other implementations, so the
|
|
86
|
-
* delimiters are stripped here rather than left to the pattern.
|
|
64
|
+
* The parts of a location this router matches against. `search` and `hash`
|
|
65
|
+
* carry no leading `?` or `#`: a real `URLPattern` canonicalises either form
|
|
66
|
+
* away, but `URLPatternLike` admits implementations that do not.
|
|
87
67
|
*/
|
|
88
68
|
export interface RouteLocation {
|
|
89
69
|
pathname: string;
|
|
90
70
|
search: string;
|
|
91
71
|
hash: string;
|
|
92
72
|
}
|
|
93
|
-
/**
|
|
94
|
-
* A description of a route, which path or pattern to match against, and a
|
|
95
|
-
* render() callback used to render a match to the outlet.
|
|
96
|
-
*/
|
|
97
73
|
export type RouteConfig = PathRouteConfig | URLPatternRouteConfig;
|
|
98
74
|
/**
|
|
99
75
|
* A reactive controller that performs location-based routing using a
|
|
@@ -101,13 +77,14 @@ export type RouteConfig = PathRouteConfig | URLPatternRouteConfig;
|
|
|
101
77
|
*/
|
|
102
78
|
export declare class Routes implements ReactiveController {
|
|
103
79
|
private readonly _host;
|
|
80
|
+
/**
|
|
81
|
+
* The installed routes, in precedence order. Mutable, but mutating it starts
|
|
82
|
+
* no route transition; call `goto()` if a different route now matches.
|
|
83
|
+
*/
|
|
104
84
|
routes: Array<RouteConfig>;
|
|
105
85
|
/**
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
* pathname minus its leading slash, and is handed to child controllers as
|
|
109
|
-
* their tail. A nested controller's own pathname is a tail, with no leading
|
|
110
|
-
* slash; the fallback accepts that too.
|
|
86
|
+
* Matched when no route in {@link routes} does. Behaves like `/*`, so
|
|
87
|
+
* `params[0]` is the whole pathname minus any leading slash.
|
|
111
88
|
*/
|
|
112
89
|
fallback?: BaseRouteConfig;
|
|
113
90
|
private readonly _childRoutes;
|
|
@@ -116,23 +93,13 @@ export declare class Routes implements ReactiveController {
|
|
|
116
93
|
private _gotoSeq;
|
|
117
94
|
private _currentPathname;
|
|
118
95
|
private _currentTail;
|
|
119
|
-
/**
|
|
120
|
-
* The search and hash of the current location.
|
|
121
|
-
*
|
|
122
|
-
* Ambient rather than tailed: only the pathname nests, so a child controller
|
|
123
|
-
* is handed the parent's tail as its pathname but the *same* search and
|
|
124
|
-
* hash. There is no meaningful way to split a fragment across a route tree.
|
|
125
|
-
*/
|
|
96
|
+
/** Ambient, not tailed: only the pathname nests. */
|
|
126
97
|
private _currentSearch;
|
|
127
98
|
private _currentHash;
|
|
128
99
|
private _currentRoute;
|
|
129
100
|
private _currentParams;
|
|
130
|
-
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* It's critical to call this immediately in hostDisconnected so that this
|
|
134
|
-
* controller instance doesn't receive a tail match meant for another route.
|
|
135
|
-
*/
|
|
101
|
+
/** Must run in hostDisconnected, or this controller can receive another
|
|
102
|
+
* route's tail match. */
|
|
136
103
|
private _onDisconnect;
|
|
137
104
|
constructor(host: ReactiveControllerHost & HTMLElement, routes: Array<RouteConfig>, options?: {
|
|
138
105
|
fallback?: BaseRouteConfig;
|
|
@@ -143,17 +110,30 @@ export declare class Routes implements ReactiveController {
|
|
|
143
110
|
*/
|
|
144
111
|
link(pathname?: string): string;
|
|
145
112
|
/**
|
|
146
|
-
*
|
|
113
|
+
* A URL for the route named `name`, with `params` substituted into its
|
|
114
|
+
* pattern.
|
|
147
115
|
*
|
|
148
|
-
*
|
|
149
|
-
*
|
|
150
|
-
*
|
|
116
|
+
* Lets a component say which route it means instead of where that route
|
|
117
|
+
* currently lives, so moving a route subtree does not silently break every
|
|
118
|
+
* hardcoded link inside it.
|
|
151
119
|
*
|
|
152
|
-
*
|
|
153
|
-
*
|
|
154
|
-
*
|
|
155
|
-
*
|
|
156
|
-
*
|
|
120
|
+
* Resolution covers the mounted controller tree: this controller, its
|
|
121
|
+
* ancestors, and any descendant that has rendered. A route in a branch that
|
|
122
|
+
* has not mounted yet is not addressable, because the mapping from a parent
|
|
123
|
+
* route to its child controller only exists once that parent has rendered.
|
|
124
|
+
* An unknown or ambiguous name throws rather than producing a wrong URL.
|
|
125
|
+
*/
|
|
126
|
+
linkTo(name: string, params?: {
|
|
127
|
+
[key: string]: string | undefined;
|
|
128
|
+
}): string;
|
|
129
|
+
/**
|
|
130
|
+
* Navigates this controller to `path`, which may carry a search and hash.
|
|
131
|
+
* Navigates child routes but not parent ones, so it is not yet a general page
|
|
132
|
+
* navigation API.
|
|
133
|
+
*
|
|
134
|
+
* `options.signal` makes the navigation abandonable. `enter()` is awaited, so
|
|
135
|
+
* a second `goto()` can finish while the first is still resolving; without a
|
|
136
|
+
* signal the slower one commits last onto a route the URL has left.
|
|
157
137
|
*/
|
|
158
138
|
goto(path: string, options?: {
|
|
159
139
|
signal?: AbortSignal;
|
|
@@ -169,74 +149,38 @@ export declare class Routes implements ReactiveController {
|
|
|
169
149
|
[key: string]: string | undefined;
|
|
170
150
|
};
|
|
171
151
|
/**
|
|
172
|
-
* Hands a tail match to a child
|
|
173
|
-
*
|
|
174
|
-
* input cannot be silent on one and an uncaught global throw on the other.
|
|
175
|
-
*
|
|
176
|
-
* A child with no route for the new tail is the expected case, not an error —
|
|
177
|
-
* the outgoing branch mid-swap, or a deep link to a path the child cannot
|
|
178
|
-
* render. Filtered structurally rather than by swallowing every rejection, so
|
|
179
|
-
* a genuine `enter()` rejection still surfaces the way it does upstream.
|
|
180
|
-
* Skipping must still supersede: `goto()` is where the counter is bumped, so
|
|
181
|
-
* returning without it would leave an in-flight child navigation current,
|
|
182
|
-
* free to commit over a URL that has moved on. A parent route with no tail
|
|
183
|
-
* at all is the same case: nothing to route, but still something to stand
|
|
184
|
-
* down.
|
|
152
|
+
* Hands a tail match to a child. Shared by `goto()`'s propagation loop and the
|
|
153
|
+
* late-mount path, so identical input behaves identically on both.
|
|
185
154
|
*
|
|
186
|
-
*
|
|
187
|
-
*
|
|
188
|
-
*
|
|
189
|
-
* to correct it, leaving the nested outlet stuck — reachable, because a
|
|
190
|
-
* hash-only navigation aborts the outstanding one without producing a
|
|
191
|
-
* replacement. Supersession is the counter's job.
|
|
155
|
+
* A child with no route for the tail is expected, not an error: the outgoing
|
|
156
|
+
* branch mid-swap, or a deep link it cannot render. Filtered structurally so
|
|
157
|
+
* a genuine `enter()` rejection still surfaces. Skipping must still supersede.
|
|
192
158
|
*/
|
|
193
159
|
private _routeChild;
|
|
194
|
-
/**
|
|
195
|
-
* Invalidate any in-flight `goto()` on this controller without starting a
|
|
196
|
-
* new one. Same-class access, so `_gotoSeq` stays private to `Routes`.
|
|
197
|
-
*/
|
|
160
|
+
/** Invalidate any in-flight `goto()` here without starting a new one. */
|
|
198
161
|
private _supersede;
|
|
199
162
|
/**
|
|
200
|
-
* True when this controller
|
|
201
|
-
*
|
|
202
|
-
*
|
|
203
|
-
* `Router` gates interception of fragment-only navigation on this. Left
|
|
204
|
-
* ungated, a pathname-only app would have every in-page anchor swallowed and
|
|
205
|
-
* re-rendered instead of scrolled; gated, such an app behaves exactly as it
|
|
206
|
-
* did before hash routes existed.
|
|
207
|
-
*
|
|
208
|
-
* Walks children because a nested controller may route on the hash while the
|
|
209
|
-
* top-level `Router` does not — and only the top-level one sees the
|
|
210
|
-
* navigate event.
|
|
163
|
+
* True when this controller or any below it constrains the hash. `Router`
|
|
164
|
+
* gates fragment-only interception on this, so a pathname-only app keeps
|
|
165
|
+
* native scrolling. Walks children because only the root sees the event.
|
|
211
166
|
*/
|
|
212
167
|
protected _constrainsHash(seen?: Set<Routes>): boolean;
|
|
213
168
|
/**
|
|
214
|
-
* True when this controller can render `path`
|
|
215
|
-
*
|
|
216
|
-
*
|
|
217
|
-
* `Router` gates interception on this: intercepting a path we cannot render
|
|
218
|
-
* commits the URL and then throws out of `goto()`, leaving the address bar
|
|
219
|
-
* moved and the outlet stale. Letting the browser handle it instead means a
|
|
220
|
-
* server-rendered page, an export endpoint, or a GET form still works.
|
|
169
|
+
* True when this controller can render `path`. `Router` gates interception on
|
|
170
|
+
* this: intercepting what we cannot render commits the URL and then throws,
|
|
171
|
+
* leaving the address bar moved and the outlet stale.
|
|
221
172
|
*/
|
|
222
173
|
hasRouteFor(path: string): boolean;
|
|
223
174
|
/**
|
|
224
|
-
*
|
|
225
|
-
*
|
|
226
|
-
*
|
|
227
|
-
* One `exec()` per candidate rather than `test()` to select and `exec()` to
|
|
228
|
-
* extract: that ran the winning pattern twice, and every caller that wants a
|
|
229
|
-
* route wants its params too.
|
|
175
|
+
* The first route matching `location`, or the fallback's match. One `exec()`
|
|
176
|
+
* per candidate; selecting with `test()` first ran the winner twice.
|
|
230
177
|
*/
|
|
231
178
|
private _match;
|
|
232
179
|
hostConnected(): void;
|
|
233
180
|
hostDisconnected(): void;
|
|
234
181
|
private _onRoutesConnected;
|
|
235
182
|
}
|
|
236
|
-
/**
|
|
237
|
-
* This event is fired from Routes controllers when their host is connected to
|
|
238
|
-
* announce the child route and potentially connect to a parent routes controller.
|
|
239
|
-
*/
|
|
183
|
+
/** Announces a Routes controller to its parent when the host connects. */
|
|
240
184
|
export declare class RoutesConnectedEvent extends Event {
|
|
241
185
|
static readonly eventName = "lit-routes-connected";
|
|
242
186
|
readonly routes: Routes;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAC,kBAAkB,EAAE,sBAAsB,EAAC,MAAM,KAAK,CAAC;AAEpE,MAAM,WAAW,eAAe;IAC9B,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE;QAAC,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAA;KAAC,KAAK,OAAO,CAAC;IAClE,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE;QACf,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;KACnC,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC;CAClC;AAED;;;GAGG;AACH,MAAM,WAAW,eAAgB,SAAQ,eAAe;IACtD,IAAI,EAAE,MAAM,CAAC;CACd;AAED
|
|
1
|
+
{"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../src/routes.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAC,kBAAkB,EAAE,sBAAsB,EAAC,MAAM,KAAK,CAAC;AAEpE,MAAM,WAAW,eAAe;IAC9B,I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