@brightspace-ui/labs 2.33.0 → 2.33.2
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/package.json
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Router
|
|
2
2
|
|
|
3
|
-
A Lit wrapper around the [Page.js Router](https://visionmedia.github.io/page.js/).
|
|
3
|
+
A Lit wrapper around the [Page.js Router](https://visionmedia.github.io/page.js/), providing an easy way to define routes, lazy load the view, and react to changes to the route.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Route Registration
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
### Route Registration
|
|
10
|
-
|
|
11
|
-
Registering routes defines the routing table used when determining the view to render.
|
|
7
|
+
To define the routing table that gets used when determining which view to render, `registerRoutes` should be executed once -- typically by the root of the application.
|
|
12
8
|
|
|
13
9
|
When the URL matches a particular route's pattern, the `view` function is called and returns a Lit `html` literal to render.
|
|
14
10
|
|
|
@@ -17,36 +13,29 @@ import { registerRoutes } from '@brightspace-ui/labs/utilities/lit-router';
|
|
|
17
13
|
|
|
18
14
|
registerRoutes([
|
|
19
15
|
{
|
|
20
|
-
pattern: '/
|
|
16
|
+
pattern: '/my-app',
|
|
21
17
|
loader: () => import('./home.js'),
|
|
22
|
-
view: () => html`<
|
|
23
|
-
},
|
|
24
|
-
{
|
|
25
|
-
pattern: '/example/:id',
|
|
26
|
-
view: ctx => html`<test-id id=${ctx.params.id}></test-id>`
|
|
18
|
+
view: () => html`<my-app-home></my-app-home>`
|
|
27
19
|
},
|
|
28
20
|
{
|
|
29
|
-
pattern: '/
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
${ctx.search.name}
|
|
33
|
-
</test-foo>
|
|
34
|
-
`
|
|
21
|
+
pattern: '/my-app/user/:id',
|
|
22
|
+
loader: () => import('./user.js'),
|
|
23
|
+
view: ctx => html`<my-app-user id="${ctx.params.id}"></my-app-user>`
|
|
35
24
|
}
|
|
36
25
|
], options);
|
|
37
26
|
```
|
|
38
27
|
|
|
39
|
-
|
|
28
|
+
### Route Properties
|
|
40
29
|
|
|
41
30
|
Each route has the following properties:
|
|
42
31
|
- `pattern` (required): The Page.js route pattern on which to match
|
|
43
|
-
- `loader` (optional): Allows for lazy-loading dependencies (e.g. importing view files) before rendering the view; must return a Promise
|
|
32
|
+
- `loader` (optional): Allows for lazy-loading dependencies (e.g. importing view files) before rendering the view; must return a `Promise`
|
|
44
33
|
- `view` (optional): Function that returns a Lit `html` literal to render
|
|
45
34
|
- `to` (optional): String indicating a redirect path, using Page.js `redirect(fromPath, toPath)`
|
|
46
35
|
|
|
47
|
-
|
|
36
|
+
### View Context
|
|
48
37
|
|
|
49
|
-
The view is provided a context object containing:
|
|
38
|
+
The view delegate is provided a context object containing:
|
|
50
39
|
- `params`: URL segment parameters (e.g. `:id`)
|
|
51
40
|
- `search`: search query values
|
|
52
41
|
- `options`: object passed by the entry-point
|
|
@@ -59,20 +48,20 @@ The view is provided a context object containing:
|
|
|
59
48
|
Example:
|
|
60
49
|
```js
|
|
61
50
|
{
|
|
62
|
-
pattern: '/user/:id/:page' // search: ?semester=1
|
|
51
|
+
pattern: '/my-app/user/:id/:page' // search: ?semester=1
|
|
63
52
|
view: ctx => html`
|
|
64
|
-
<user
|
|
65
|
-
id
|
|
66
|
-
page
|
|
67
|
-
semester
|
|
68
|
-
</user
|
|
53
|
+
<my-app-user
|
|
54
|
+
id="${ctx.options.id}"
|
|
55
|
+
page="${ctx.params.page}"
|
|
56
|
+
semester="${ctx.search.semester}">
|
|
57
|
+
</my-app-user>
|
|
69
58
|
`
|
|
70
59
|
}
|
|
71
60
|
```
|
|
72
61
|
|
|
73
|
-
|
|
62
|
+
### Options
|
|
74
63
|
|
|
75
|
-
Options are the second parameter passed to `registerRoutes`. The two tables below
|
|
64
|
+
Options are the second parameter passed to `registerRoutes`. The two tables below encompass all the attributes that the options object can use.
|
|
76
65
|
|
|
77
66
|
Page.js options:
|
|
78
67
|
|
|
@@ -139,9 +128,9 @@ export const loader () => [
|
|
|
139
128
|
]
|
|
140
129
|
```
|
|
141
130
|
|
|
142
|
-
|
|
131
|
+
## Rendering the Active view using RouteReactor
|
|
143
132
|
|
|
144
|
-
The `RouteReactor` is a [Reactive Controller](https://lit.dev/docs/composition/controllers/) responsible for re-rendering the
|
|
133
|
+
The `RouteReactor` is a [Reactive Controller](https://lit.dev/docs/composition/controllers/) responsible for rendering (and re-rendering) the active view whenever the route changes.
|
|
145
134
|
|
|
146
135
|
```js
|
|
147
136
|
import { RouteReactor } from '@brightspace-ui/labs/utilities/lit-router';
|
|
@@ -183,38 +172,40 @@ class FooBar extends LitElement {
|
|
|
183
172
|
}
|
|
184
173
|
```
|
|
185
174
|
|
|
186
|
-
|
|
175
|
+
## Navigating and Redirecting
|
|
187
176
|
|
|
188
|
-
|
|
177
|
+
Page.js will hook into any `<a>` tags and handle the navigation automatically.
|
|
189
178
|
|
|
190
|
-
|
|
179
|
+
To navigate manually and add a new browser history entry, use `navigate(path)`:
|
|
191
180
|
|
|
192
181
|
```js
|
|
193
182
|
import { navigate } from '@brightspace-ui/labs/utilities/lit-router';
|
|
194
183
|
|
|
195
|
-
navigate('/');
|
|
184
|
+
navigate('/my-app/new-place');
|
|
196
185
|
```
|
|
197
186
|
|
|
198
|
-
To
|
|
187
|
+
To redirect to a page and replace the previous browser history item the new one, use `redirect(path)`:
|
|
199
188
|
|
|
200
189
|
```js
|
|
201
190
|
import { redirect } from '@brightspace-ui/labs/utilities/lit-router';
|
|
202
191
|
|
|
203
|
-
redirect('/');
|
|
192
|
+
redirect('/my-app/new-place');
|
|
204
193
|
```
|
|
205
194
|
|
|
206
|
-
|
|
195
|
+
## Testing Routing in Your Application
|
|
207
196
|
|
|
208
197
|
For testing page routing in your application, this template is recommended:
|
|
209
198
|
|
|
210
199
|
```js
|
|
200
|
+
import { RouterTesting } from '@brightspace-ui/labs/utilities/lit-router';
|
|
201
|
+
|
|
211
202
|
describe('Page Routing', () => {
|
|
212
203
|
|
|
213
204
|
beforeEach(async () => {
|
|
214
205
|
// Initialize the routes here or import a file
|
|
215
206
|
// that calls registerRoutes and expose a way to recall it
|
|
216
207
|
initRouter();
|
|
217
|
-
entryPoint = await fixture(html`<!-- Your
|
|
208
|
+
entryPoint = await fixture(html`<!-- Your RouteReactor component here -->`);
|
|
218
209
|
navigate('/'); // Reset tests back to the index, clears the url
|
|
219
210
|
});
|
|
220
211
|
|
|
@@ -259,39 +250,3 @@ registerRoutes([
|
|
|
259
250
|
```
|
|
260
251
|
|
|
261
252
|
Note: The reason this is an opt-in fix is that a lot of existing implementations of lit-router have worked around this issue by putting the wildcard routes at the start, which would break if the issue were fixed for everyone automatically.
|
|
262
|
-
|
|
263
|
-
## Developing and Contributing
|
|
264
|
-
|
|
265
|
-
After cloning the repo, run `npm install` to install dependencies.
|
|
266
|
-
|
|
267
|
-
### Testing
|
|
268
|
-
|
|
269
|
-
To run the full suite of tests:
|
|
270
|
-
|
|
271
|
-
```shell
|
|
272
|
-
npm test
|
|
273
|
-
```
|
|
274
|
-
|
|
275
|
-
Alternatively, tests can be selectively run:
|
|
276
|
-
|
|
277
|
-
```shell
|
|
278
|
-
# eslint
|
|
279
|
-
npm run lint
|
|
280
|
-
|
|
281
|
-
# unit tests
|
|
282
|
-
npm run test:unit
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
### Running the demos
|
|
286
|
-
|
|
287
|
-
To start a [@web/dev-server](https://modern-web.dev/docs/dev-server/overview/) that hosts the demo pages and tests:
|
|
288
|
-
|
|
289
|
-
```shell
|
|
290
|
-
npm start
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
### Versioning and Releasing
|
|
294
|
-
|
|
295
|
-
This repo is configured to use `semantic-release`. Commits prefixed with `fix:` and `feat:` will trigger patch and minor releases when merged to `main`.
|
|
296
|
-
|
|
297
|
-
To learn how to create major releases and release from maintenance branches, refer to the [semantic-release GitHub Action](https://github.com/BrightspaceUI/actions/tree/main/semantic-release) documentation.
|
|
@@ -38,10 +38,11 @@ const _handleRouteView = (context, next, r) => {
|
|
|
38
38
|
};
|
|
39
39
|
|
|
40
40
|
const _handleRouteLoader = r => (context, next) => {
|
|
41
|
+
const disableRouteOrderFix = _lastOptions?.disableRouteOrderFix ?? false;
|
|
41
42
|
const enableRouteOrderFix = _lastOptions?.enableRouteOrderFix ?? false;
|
|
42
43
|
|
|
43
44
|
// Skip further pattern matches if the route has already been handled
|
|
44
|
-
if (enableRouteOrderFix && context.handled) {
|
|
45
|
+
if (!disableRouteOrderFix && enableRouteOrderFix && context.handled) {
|
|
45
46
|
next();
|
|
46
47
|
return;
|
|
47
48
|
}
|
|
@@ -51,7 +52,7 @@ const _handleRouteLoader = r => (context, next) => {
|
|
|
51
52
|
_handleRouteView(context, next, r);
|
|
52
53
|
});
|
|
53
54
|
} else if (r.pattern && r.to) {
|
|
54
|
-
if (enableRouteOrderFix) {
|
|
55
|
+
if (!disableRouteOrderFix && enableRouteOrderFix) {
|
|
55
56
|
activePage.redirect(r.to);
|
|
56
57
|
} else {
|
|
57
58
|
activePage.redirect(r.pattern, r.to);
|
|
@@ -93,7 +94,7 @@ export const registerRoutes = (routes, options) => {
|
|
|
93
94
|
if (hasRegistered) throw new Error('May not construct multiple routers.');
|
|
94
95
|
hasRegistered = true;
|
|
95
96
|
|
|
96
|
-
if (!options?.enableRouteOrderFix) console.warn('lit-router: The enableRouteOrderFix option is not enabled. This may cause issues with route handling. See here for details: https://github.com/
|
|
97
|
+
if (!options?.enableRouteOrderFix) console.warn('lit-router: The enableRouteOrderFix option is not enabled. This may cause issues with route handling. See here for details: https://github.com/BrightspaceUI/labs/tree/main/src/utilities/router#route-order-inversion-issue');
|
|
97
98
|
|
|
98
99
|
configure(options);
|
|
99
100
|
|