@brightspace-ui/labs 2.43.1 → 2.44.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.
package/package.json CHANGED
@@ -114,5 +114,5 @@
114
114
  "resize-observer-polyfill": "^1",
115
115
  "webvtt-parser": "^2"
116
116
  },
117
- "version": "2.43.1"
117
+ "version": "2.44.1"
118
118
  }
@@ -227,6 +227,17 @@ describe('Page Routing', () => {
227
227
  });
228
228
  ```
229
229
 
230
+ ## Router Navigation Hooks
231
+
232
+ If you want to execute code that doesn't belong to a web component before or after navigation, you can use the RouterHooks class. This is helpful for data models that care about changes in navigation, like a module responsible for timing page loads.
233
+
234
+ ```
235
+ import { RouterHooks } from '@brightspace-ui/labs/utilities/lit-router';
236
+
237
+ RouterHooks.registerPreNavigate((context) => { console.log('this runs before we navigate', context);});
238
+ RouterHooks.registerPostNavigate((context) => { console.log('post', context);});
239
+ ```
240
+
230
241
  ## Known Issues
231
242
 
232
243
  ### Route order inversion issue
@@ -1,2 +1,2 @@
1
- export { ContextReactor, redirect, navigate, registerRoutes, RouterTesting } from './router.js';
1
+ export { ContextReactor, redirect, navigate, registerRoutes, RouterTesting, RouterHooks } from './router.js';
2
2
  export { RouteReactor } from './RouteReactor.js';
@@ -85,6 +85,37 @@ const _registerRoutes = routes => {
85
85
  });
86
86
  };
87
87
 
88
+ export class RouterHooks {
89
+ static registerPreNavigate = (callback) => {
90
+ RouterHooks.preNavigate.push(callback);
91
+ };
92
+
93
+ static registerPostNavigate = (callback) => {
94
+ RouterHooks.postNavigate.push(callback);
95
+ };
96
+
97
+ static reset() {
98
+ this.preNavigate = [];
99
+ this.postNavigate = [];
100
+ };
101
+ }
102
+
103
+ RouterHooks.reset();
104
+
105
+ const _registerPreNavigateHooks = () => {
106
+ activePage('*', (context, next) => {
107
+ RouterHooks.preNavigate.forEach(h => h(context));
108
+ next();
109
+ });
110
+ };
111
+
112
+ const _registerPostNavigateHooks = () => {
113
+ activePage('*', (context, next) => {
114
+ RouterHooks.postNavigate.forEach(h => h(context));
115
+ next();
116
+ });
117
+ };
118
+
88
119
  const configure = options => {
89
120
  if (options && options.customPage) activePage = page.create();
90
121
  if (options && options.basePath) activePage.base(options.basePath);
@@ -110,7 +141,9 @@ export const registerRoutes = (routes, options) => {
110
141
  });
111
142
  next();
112
143
  });
144
+ _registerPreNavigateHooks();
113
145
  _registerRoutes(routes);
146
+ _registerPostNavigateHooks();
114
147
  _storeCtx();
115
148
  };
116
149
 
@@ -175,6 +208,7 @@ export const RouterTesting = {
175
208
  activePage = page.create();
176
209
  hasRegistered = false;
177
210
  ContextReactor.reset();
211
+ RouterHooks.reset();
178
212
  },
179
213
 
180
214
  restart: () => {