@esmx/router 3.0.0-rc.83 → 3.0.0-rc.87

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/dist/options.mjs CHANGED
@@ -38,6 +38,7 @@ export function parsedOptions(options = {}) {
38
38
  rootStyle: options.rootStyle || false,
39
39
  root: options.root || "",
40
40
  context: options.context || {},
41
+ data: options.data || {},
41
42
  req: options.req || null,
42
43
  res: options.res || null,
43
44
  layer: options.layer || false,
package/dist/router.d.ts CHANGED
@@ -13,6 +13,7 @@ export declare class Router {
13
13
  readonly transition: RouteTransition;
14
14
  get route(): Route;
15
15
  get context(): Record<string | symbol, unknown>;
16
+ get data(): Record<string | symbol, unknown>;
16
17
  get root(): string | HTMLElement;
17
18
  get mode(): RouterMode;
18
19
  get base(): URL;
package/dist/router.mjs CHANGED
@@ -44,6 +44,9 @@ export class Router {
44
44
  get context() {
45
45
  return this.parsedOptions.context;
46
46
  }
47
+ get data() {
48
+ return this.parsedOptions.data;
49
+ }
47
50
  get root() {
48
51
  return this.parsedOptions.root;
49
52
  }
@@ -257,24 +260,44 @@ export class Router {
257
260
  },
258
261
  layer: true
259
262
  });
260
- await router.replace(toInput);
261
- router.afterEach((to, from) => {
262
- if (layerOptions.shouldClose && ![
263
+ const initRoute = await router.replace(toInput);
264
+ router.afterEach(async (to, from) => {
265
+ if (![
263
266
  RouteType.pushWindow,
264
267
  RouteType.replaceWindow,
265
268
  RouteType.replace,
266
269
  RouteType.restartApp,
267
270
  RouteType.pushLayer
268
- ].includes(to.type)) {
269
- const result = layerOptions.shouldClose(to, from, router);
270
- if (result === true) {
271
- router.destroy();
272
- promiseResolve({
273
- type: "push",
274
- route: to
275
- });
271
+ ].includes(to.type))
272
+ return;
273
+ let keepAlive = false;
274
+ if (layerOptions.keepAlive === "exact") {
275
+ keepAlive = to.path === initRoute.path;
276
+ } else if (layerOptions.keepAlive === "include") {
277
+ keepAlive = to.path.startsWith(initRoute.path);
278
+ } else if (typeof layerOptions.keepAlive === "function") {
279
+ keepAlive = await layerOptions.keepAlive(to, from, router);
280
+ } else {
281
+ if (layerOptions.shouldClose) {
282
+ console.warn(
283
+ "[esmx-router] RouteLayerOptions.shouldClose is deprecated. Use keepAlive instead. Note: shouldClose returns true to close, keepAlive returns true to keep alive."
284
+ );
285
+ keepAlive = !await layerOptions.shouldClose(
286
+ to,
287
+ from,
288
+ router
289
+ );
290
+ } else {
291
+ keepAlive = to.path === initRoute.path;
276
292
  }
277
293
  }
294
+ if (!keepAlive) {
295
+ router.destroy();
296
+ promiseResolve({
297
+ type: "push",
298
+ route: to
299
+ });
300
+ }
278
301
  });
279
302
  if (layerOptions.push) {
280
303
  router.navigation.pushHistoryState(
package/dist/types.d.ts CHANGED
@@ -115,6 +115,27 @@ export interface RouteLayerOptions {
115
115
  */
116
116
  zIndex?: number;
117
117
  /**
118
+ * Hook function to determine layer keep-alive behavior before route closure
119
+ * Determines whether the layer should remain open during navigation
120
+ * - 'exact': Keep layer alive only when navigating to the exact initial path (default)
121
+ * - 'include': Keep layer alive when navigating to paths that start with the initial path
122
+ * - function: Custom logic to determine if the layer should be kept alive
123
+ * @default 'exact'
124
+ * @example
125
+ * ```typescript
126
+ * // Default behavior - keep only when navigating back to initial path
127
+ * keepAlive: 'exact'
128
+ *
129
+ * // Keep layer alive for all sub-paths (paths starting with the initial path)
130
+ * keepAlive: 'include'
131
+ *
132
+ * // Custom logic
133
+ * keepAlive: (to, from, router) => to.query.keepLayer === 'true'
134
+ * ```
135
+ */
136
+ keepAlive?: 'exact' | 'include' | RouteVerifyHook;
137
+ /**
138
+ * @deprecated Use keepAlive instead
118
139
  * Verification hook function before route closure
119
140
  * @returns Return true to allow closure, false to prevent closure
120
141
  */
@@ -186,6 +207,7 @@ export interface RouterOptions {
186
207
  */
187
208
  root?: string | HTMLElement;
188
209
  context?: Record<string | symbol, unknown>;
210
+ data?: Record<string | symbol, unknown>;
189
211
  routes?: RouteConfig[];
190
212
  mode?: RouterMode;
191
213
  /** Optional in browser, but required on server side */
package/package.json CHANGED
@@ -39,7 +39,7 @@
39
39
  "unbuild": "3.6.1",
40
40
  "vitest": "3.2.4"
41
41
  },
42
- "version": "3.0.0-rc.83",
42
+ "version": "3.0.0-rc.87",
43
43
  "type": "module",
44
44
  "private": false,
45
45
  "exports": {
@@ -58,5 +58,5 @@
58
58
  "template",
59
59
  "public"
60
60
  ],
61
- "gitHead": "9fda727dc0f62de0f2bfd4bd29c2b3028fd69137"
61
+ "gitHead": "1f79a73438c2193fd06ebaa3dc6a4ec420e6b026"
62
62
  }
package/src/options.ts CHANGED
@@ -66,6 +66,7 @@ export function parsedOptions(
66
66
  rootStyle: options.rootStyle || false,
67
67
  root: options.root || '',
68
68
  context: options.context || {},
69
+ data: options.data || {},
69
70
  req: options.req || null,
70
71
  res: options.res || null,
71
72
  layer: options.layer || false,
package/src/router.ts CHANGED
@@ -43,6 +43,9 @@ export class Router {
43
43
  public get context() {
44
44
  return this.parsedOptions.context;
45
45
  }
46
+ public get data() {
47
+ return this.parsedOptions.data;
48
+ }
46
49
 
47
50
  public get root() {
48
51
  return this.parsedOptions.root;
@@ -289,11 +292,10 @@ export class Router {
289
292
  },
290
293
  layer: true
291
294
  });
292
- await router.replace(toInput);
295
+ const initRoute = await router.replace(toInput);
293
296
 
294
- router.afterEach((to, from) => {
297
+ router.afterEach(async (to, from) => {
295
298
  if (
296
- layerOptions.shouldClose &&
297
299
  ![
298
300
  RouteType.pushWindow,
299
301
  RouteType.replaceWindow,
@@ -301,16 +303,37 @@ export class Router {
301
303
  RouteType.restartApp,
302
304
  RouteType.pushLayer
303
305
  ].includes(to.type)
304
- ) {
305
- const result = layerOptions.shouldClose(to, from, router);
306
- if (result === true) {
307
- router.destroy();
308
- promiseResolve({
309
- type: 'push',
310
- route: to
311
- });
306
+ )
307
+ return;
308
+ let keepAlive = false;
309
+ if (layerOptions.keepAlive === 'exact') {
310
+ keepAlive = to.path === initRoute.path;
311
+ } else if (layerOptions.keepAlive === 'include') {
312
+ keepAlive = to.path.startsWith(initRoute.path);
313
+ } else if (typeof layerOptions.keepAlive === 'function') {
314
+ keepAlive = await layerOptions.keepAlive(to, from, router);
315
+ } else {
316
+ if (layerOptions.shouldClose) {
317
+ console.warn(
318
+ '[esmx-router] RouteLayerOptions.shouldClose is deprecated. Use keepAlive instead. ' +
319
+ 'Note: shouldClose returns true to close, keepAlive returns true to keep alive.'
320
+ );
321
+ keepAlive = !(await layerOptions.shouldClose(
322
+ to,
323
+ from,
324
+ router
325
+ ));
326
+ } else {
327
+ keepAlive = to.path === initRoute.path;
312
328
  }
313
329
  }
330
+ if (!keepAlive) {
331
+ router.destroy();
332
+ promiseResolve({
333
+ type: 'push',
334
+ route: to
335
+ });
336
+ }
314
337
  });
315
338
  if (layerOptions.push) {
316
339
  router.navigation.pushHistoryState(
package/src/types.ts CHANGED
@@ -177,6 +177,27 @@ export interface RouteLayerOptions {
177
177
  */
178
178
  zIndex?: number;
179
179
  /**
180
+ * Hook function to determine layer keep-alive behavior before route closure
181
+ * Determines whether the layer should remain open during navigation
182
+ * - 'exact': Keep layer alive only when navigating to the exact initial path (default)
183
+ * - 'include': Keep layer alive when navigating to paths that start with the initial path
184
+ * - function: Custom logic to determine if the layer should be kept alive
185
+ * @default 'exact'
186
+ * @example
187
+ * ```typescript
188
+ * // Default behavior - keep only when navigating back to initial path
189
+ * keepAlive: 'exact'
190
+ *
191
+ * // Keep layer alive for all sub-paths (paths starting with the initial path)
192
+ * keepAlive: 'include'
193
+ *
194
+ * // Custom logic
195
+ * keepAlive: (to, from, router) => to.query.keepLayer === 'true'
196
+ * ```
197
+ */
198
+ keepAlive?: 'exact' | 'include' | RouteVerifyHook;
199
+ /**
200
+ * @deprecated Use keepAlive instead
180
201
  * Verification hook function before route closure
181
202
  * @returns Return true to allow closure, false to prevent closure
182
203
  */
@@ -258,6 +279,7 @@ export interface RouterOptions {
258
279
  */
259
280
  root?: string | HTMLElement;
260
281
  context?: Record<string | symbol, unknown>;
282
+ data?: Record<string | symbol, unknown>;
261
283
  routes?: RouteConfig[];
262
284
  mode?: RouterMode;
263
285
  /** Optional in browser, but required on server side */