@amazon-devices/react-navigation__routers 7.0.1759135970 → 8.0.9000000000

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.
Files changed (73) hide show
  1. package/SystemBundles/21657b0b773752468bbe6711df32d141e12a1ad993b6abbe39ad0de460714386.bundle +1279 -0
  2. package/SystemBundles/21657b0b773752468bbe6711df32d141e12a1ad993b6abbe39ad0de460714386.map +372 -0
  3. package/SystemModules/amzn__react-navigation__routers-8.modules.txt +9 -0
  4. package/SystemModules/amzn__react-navigation__routers-8.private.modules.txt +1 -0
  5. package/SystemModules/keplerscript-system-bundle-config.json +4 -4
  6. package/kepler-compatibility.json +2 -2
  7. package/lib/module/BaseRouter.js +6 -4
  8. package/lib/module/BaseRouter.js.map +1 -1
  9. package/lib/module/CommonActions.js +48 -7
  10. package/lib/module/CommonActions.js.map +1 -1
  11. package/lib/module/DrawerRouter.js +6 -6
  12. package/lib/module/DrawerRouter.js.map +1 -1
  13. package/lib/module/StackRouter.js +240 -77
  14. package/lib/module/StackRouter.js.map +1 -1
  15. package/lib/module/TabRouter.js +117 -34
  16. package/lib/module/TabRouter.js.map +1 -1
  17. package/lib/module/createParamsFromAction.js +16 -0
  18. package/lib/module/createParamsFromAction.js.map +1 -0
  19. package/lib/module/createRouteFromAction.js +21 -0
  20. package/lib/module/createRouteFromAction.js.map +1 -0
  21. package/lib/module/index.js +8 -6
  22. package/lib/module/index.js.map +1 -1
  23. package/lib/module/package.json +1 -0
  24. package/lib/module/types.js +2 -0
  25. package/lib/module/types.js.map +1 -1
  26. package/lib/typescript/package.json +1 -0
  27. package/lib/typescript/src/BaseRouter.d.ts +23 -20
  28. package/lib/typescript/src/BaseRouter.d.ts.map +1 -1
  29. package/lib/typescript/src/CommonActions.d.ts +66 -18
  30. package/lib/typescript/src/CommonActions.d.ts.map +1 -1
  31. package/lib/typescript/src/DrawerRouter.d.ts +18 -6
  32. package/lib/typescript/src/DrawerRouter.d.ts.map +1 -1
  33. package/lib/typescript/src/StackRouter.d.ts +67 -11
  34. package/lib/typescript/src/StackRouter.d.ts.map +1 -1
  35. package/lib/typescript/src/TabRouter.d.ts +24 -4
  36. package/lib/typescript/src/TabRouter.d.ts.map +1 -1
  37. package/lib/typescript/src/createParamsFromAction.d.ts +13 -0
  38. package/lib/typescript/src/createParamsFromAction.d.ts.map +1 -0
  39. package/lib/typescript/src/createRouteFromAction.d.ts +17 -0
  40. package/lib/typescript/src/createRouteFromAction.d.ts.map +1 -0
  41. package/lib/typescript/src/index.d.ts +4 -4
  42. package/lib/typescript/src/index.d.ts.map +1 -1
  43. package/lib/typescript/src/types.d.ts +1 -2
  44. package/lib/typescript/src/types.d.ts.map +1 -1
  45. package/package.json +1 -1
  46. package/src/BaseRouter.tsx +11 -6
  47. package/src/CommonActions.tsx +147 -56
  48. package/src/DrawerRouter.tsx +15 -13
  49. package/src/StackRouter.tsx +417 -203
  50. package/src/TabRouter.tsx +192 -53
  51. package/src/createParamsFromAction.tsx +22 -0
  52. package/src/createRouteFromAction.tsx +24 -0
  53. package/src/index.tsx +4 -4
  54. package/src/types.tsx +5 -5
  55. package/SystemBundles/2c2b2e72cf9d128296ac83ba53082313ac31f6e9fd98b8e87fe0af40e15fbec1.bundle +0 -3515
  56. package/SystemBundles/2c2b2e72cf9d128296ac83ba53082313ac31f6e9fd98b8e87fe0af40e15fbec1.map +0 -1
  57. package/SystemModules/amzn__react-navigation__routers-7.modules.txt +0 -21
  58. package/SystemModules/amzn__react-navigation__routers-7.private.modules.txt +0 -13
  59. package/THIRD_PARTY_NOTICES.txt +0 -24
  60. package/lib/commonjs/BaseRouter.js +0 -61
  61. package/lib/commonjs/BaseRouter.js.map +0 -1
  62. package/lib/commonjs/CommonActions.js +0 -53
  63. package/lib/commonjs/CommonActions.js.map +0 -1
  64. package/lib/commonjs/DrawerRouter.js +0 -154
  65. package/lib/commonjs/DrawerRouter.js.map +0 -1
  66. package/lib/commonjs/StackRouter.js +0 -310
  67. package/lib/commonjs/StackRouter.js.map +0 -1
  68. package/lib/commonjs/TabRouter.js +0 -253
  69. package/lib/commonjs/TabRouter.js.map +0 -1
  70. package/lib/commonjs/index.js +0 -80
  71. package/lib/commonjs/index.js.map +0 -1
  72. package/lib/commonjs/types.js +0 -6
  73. package/lib/commonjs/types.js.map +0 -1
package/src/TabRouter.tsx CHANGED
@@ -1,6 +1,7 @@
1
1
  import { nanoid } from 'nanoid/non-secure';
2
2
 
3
- import BaseRouter from './BaseRouter';
3
+ import { BaseRouter } from './BaseRouter';
4
+ import { createParamsFromAction } from './createParamsFromAction';
4
5
  import type {
5
6
  CommonNavigationAction,
6
7
  DefaultRouterOptions,
@@ -19,13 +20,23 @@ export type TabActionType = {
19
20
  };
20
21
 
21
22
  export type BackBehavior =
22
- | 'initialRoute'
23
23
  | 'firstRoute'
24
- | 'history'
24
+ | 'initialRoute'
25
25
  | 'order'
26
+ | 'history'
27
+ | 'fullHistory'
26
28
  | 'none';
27
29
 
28
30
  export type TabRouterOptions = DefaultRouterOptions & {
31
+ /**
32
+ * Control how going back should behave
33
+ * - `firstRoute` - return to the first defined route
34
+ * - `initialRoute` - return to the route from `initialRouteName`
35
+ * - `order` - return to the route defined before the focused route
36
+ * - `history` - return to last visited route; if the same route is visited multiple times, the older entries are dropped from the history
37
+ * - `fullHistory` - return to last visited route; doesn't drop duplicate entries unlike `history` - matches behavior of web pages
38
+ * - `none` - do not handle going back
39
+ */
29
40
  backBehavior?: BackBehavior;
30
41
  };
31
42
 
@@ -40,7 +51,11 @@ export type TabNavigationState<ParamList extends ParamListBase> = Omit<
40
51
  /**
41
52
  * List of previously visited route keys.
42
53
  */
43
- history: { type: 'route'; key: string }[];
54
+ history: { type: 'route'; key: string; params?: object | undefined }[];
55
+ /**
56
+ * List of routes' key, which are supposed to be preloaded before navigating to.
57
+ */
58
+ preloadedRouteKeys: string[];
44
59
  };
45
60
 
46
61
  export type TabActionHelpers<ParamList extends ParamListBase> = {
@@ -50,18 +65,23 @@ export type TabActionHelpers<ParamList extends ParamListBase> = {
50
65
  * @param name Name of the route for the tab.
51
66
  * @param [params] Params object for the route.
52
67
  */
53
- jumpTo<RouteName extends Extract<keyof ParamList, string>>(
54
- ...args: undefined extends ParamList[RouteName]
55
- ? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
56
- : [screen: RouteName, params: ParamList[RouteName]]
68
+ jumpTo<RouteName extends keyof ParamList>(
69
+ ...args: RouteName extends unknown
70
+ ? undefined extends ParamList[RouteName]
71
+ ? [screen: RouteName, params?: ParamList[RouteName]]
72
+ : [screen: RouteName, params: ParamList[RouteName]]
73
+ : never
57
74
  ): void;
58
75
  };
59
76
 
60
77
  const TYPE_ROUTE = 'route' as const;
61
78
 
62
79
  export const TabActions = {
63
- jumpTo(name: string, params?: object): TabActionType {
64
- return { type: 'JUMP_TO', payload: { name, params } };
80
+ jumpTo(name: string, params?: object) {
81
+ return {
82
+ type: 'JUMP_TO',
83
+ payload: { name, params },
84
+ } as const satisfies TabActionType;
65
85
  },
66
86
  };
67
87
 
@@ -71,13 +91,22 @@ const getRouteHistory = (
71
91
  backBehavior: BackBehavior,
72
92
  initialRouteName: string | undefined
73
93
  ) => {
74
- const history = [{ type: TYPE_ROUTE, key: routes[index].key }];
94
+ const history = [
95
+ {
96
+ type: TYPE_ROUTE,
97
+ key: routes[index].key,
98
+ },
99
+ ];
100
+
75
101
  let initialRouteIndex;
76
102
 
77
103
  switch (backBehavior) {
78
104
  case 'order':
79
105
  for (let i = index; i > 0; i--) {
80
- history.unshift({ type: TYPE_ROUTE, key: routes[i - 1].key });
106
+ history.unshift({
107
+ type: TYPE_ROUTE,
108
+ key: routes[i - 1].key,
109
+ });
81
110
  }
82
111
  break;
83
112
  case 'firstRoute':
@@ -102,6 +131,7 @@ const getRouteHistory = (
102
131
  }
103
132
  break;
104
133
  case 'history':
134
+ case 'fullHistory':
105
135
  // The history will fill up on navigation
106
136
  break;
107
137
  }
@@ -115,14 +145,37 @@ const changeIndex = (
115
145
  backBehavior: BackBehavior,
116
146
  initialRouteName: string | undefined
117
147
  ) => {
118
- let history;
148
+ let history = state.history;
149
+
150
+ if (backBehavior === 'history' || backBehavior === 'fullHistory') {
151
+ const currentRoute = state.routes[index];
119
152
 
120
- if (backBehavior === 'history') {
121
- const currentKey = state.routes[index].key;
153
+ if (backBehavior === 'history') {
154
+ // Remove the existing key from the history to de-duplicate it
155
+ history = history.filter((it) =>
156
+ it.type === 'route' ? it.key !== currentRoute.key : false
157
+ );
158
+ } else if (backBehavior === 'fullHistory') {
159
+ const lastHistoryRouteItemIndex = history.findLastIndex(
160
+ (item) => item.type === 'route'
161
+ );
162
+
163
+ if (currentRoute.key === history[lastHistoryRouteItemIndex]?.key) {
164
+ // For full-history, only remove if it matches the last route
165
+ // Useful for drawer, if current route was in history, then drawer state changed
166
+ // Then we only need to move the route to the front
167
+ history = [
168
+ ...history.slice(0, lastHistoryRouteItemIndex),
169
+ ...history.slice(lastHistoryRouteItemIndex + 1),
170
+ ];
171
+ }
172
+ }
122
173
 
123
- history = state.history
124
- .filter((it) => (it.type === 'route' ? it.key !== currentKey : false))
125
- .concat({ type: TYPE_ROUTE, key: currentKey });
174
+ history = history.concat({
175
+ type: TYPE_ROUTE,
176
+ key: currentRoute.key,
177
+ params: backBehavior === 'fullHistory' ? currentRoute.params : undefined,
178
+ });
126
179
  } else {
127
180
  history = getRouteHistory(
128
181
  state.routes,
@@ -139,7 +192,7 @@ const changeIndex = (
139
192
  };
140
193
  };
141
194
 
142
- export default function TabRouter({
195
+ export function TabRouter({
143
196
  initialRouteName,
144
197
  backBehavior = 'firstRoute',
145
198
  }: TabRouterOptions) {
@@ -178,11 +231,12 @@ export default function TabRouter({
178
231
  routeNames,
179
232
  history,
180
233
  routes,
234
+ preloadedRouteKeys: [],
181
235
  };
182
236
  },
183
237
 
184
238
  getRehydratedState(partialState, { routeNames, routeParamList }) {
185
- let state = partialState;
239
+ const state = partialState;
186
240
 
187
241
  if (state.stale === false) {
188
242
  return state;
@@ -207,8 +261,8 @@ export default function TabRouter({
207
261
  ...(route ? route.params : undefined),
208
262
  }
209
263
  : route
210
- ? route.params
211
- : undefined,
264
+ ? route.params
265
+ : undefined,
212
266
  } as Route<string>;
213
267
  });
214
268
 
@@ -217,9 +271,10 @@ export default function TabRouter({
217
271
  routes.length - 1
218
272
  );
219
273
 
274
+ const routeKeys = routes.map((route) => route.key);
275
+
220
276
  const history =
221
- state.history?.filter((it) => routes.find((r) => r.key === it.key)) ??
222
- [];
277
+ state.history?.filter((it) => routeKeys.includes(it.key)) ?? [];
223
278
 
224
279
  return changeIndex(
225
280
  {
@@ -230,6 +285,10 @@ export default function TabRouter({
230
285
  routeNames,
231
286
  history,
232
287
  routes,
288
+ preloadedRouteKeys:
289
+ state.preloadedRouteKeys?.filter((key) =>
290
+ routeKeys.includes(key)
291
+ ) ?? [],
233
292
  },
234
293
  index,
235
294
  backBehavior,
@@ -293,28 +352,21 @@ export default function TabRouter({
293
352
  getStateForAction(state, action, { routeParamList, routeGetIdList }) {
294
353
  switch (action.type) {
295
354
  case 'JUMP_TO':
296
- case 'NAVIGATE': {
297
- let index = -1;
298
-
299
- if (action.type === 'NAVIGATE' && action.payload.key) {
300
- index = state.routes.findIndex(
301
- (route) => route.key === action.payload.key
302
- );
303
- } else {
304
- index = state.routes.findIndex(
305
- (route) => route.name === action.payload.name
306
- );
307
- }
355
+ case 'NAVIGATE':
356
+ case 'NAVIGATE_DEPRECATED': {
357
+ const index = state.routes.findIndex(
358
+ (route) => route.name === action.payload.name
359
+ );
308
360
 
309
361
  if (index === -1) {
310
362
  return null;
311
363
  }
312
364
 
313
- return changeIndex(
365
+ const updatedState = changeIndex(
314
366
  {
315
367
  ...state,
316
- routes: state.routes.map((route, i) => {
317
- if (i !== index) {
368
+ routes: state.routes.map((route) => {
369
+ if (route.name !== action.payload.name) {
318
370
  return route;
319
371
  }
320
372
 
@@ -331,7 +383,8 @@ export default function TabRouter({
331
383
  let params;
332
384
 
333
385
  if (
334
- action.type === 'NAVIGATE' &&
386
+ (action.type === 'NAVIGATE' ||
387
+ action.type === 'NAVIGATE_DEPRECATED') &&
335
388
  action.payload.merge &&
336
389
  currentId === nextId
337
390
  ) {
@@ -345,13 +398,7 @@ export default function TabRouter({
345
398
  }
346
399
  : route.params;
347
400
  } else {
348
- params =
349
- routeParamList[route.name] !== undefined
350
- ? {
351
- ...routeParamList[route.name],
352
- ...action.payload.params,
353
- }
354
- : action.payload.params;
401
+ params = createParamsFromAction({ action, routeParamList });
355
402
  }
356
403
 
357
404
  const path =
@@ -368,6 +415,46 @@ export default function TabRouter({
368
415
  backBehavior,
369
416
  initialRouteName
370
417
  );
418
+
419
+ return {
420
+ ...updatedState,
421
+ preloadedRouteKeys: updatedState.preloadedRouteKeys.filter(
422
+ (key) => key !== state.routes[updatedState.index].key
423
+ ),
424
+ };
425
+ }
426
+
427
+ case 'SET_PARAMS':
428
+ case 'REPLACE_PARAMS': {
429
+ const nextState = BaseRouter.getStateForAction(state, action);
430
+
431
+ if (nextState !== null) {
432
+ const index = nextState.index;
433
+
434
+ if (index != null) {
435
+ const focusedRoute = nextState.routes[index];
436
+ const historyItemIndex = state.history.findLastIndex(
437
+ (item) => item.key === focusedRoute.key
438
+ );
439
+
440
+ let updatedHistory = state.history;
441
+
442
+ if (historyItemIndex !== -1) {
443
+ updatedHistory = [...state.history];
444
+ updatedHistory[historyItemIndex] = {
445
+ ...updatedHistory[historyItemIndex],
446
+ params: focusedRoute.params,
447
+ };
448
+ }
449
+
450
+ return {
451
+ ...nextState,
452
+ history: updatedHistory,
453
+ };
454
+ }
455
+ }
456
+
457
+ return nextState;
371
458
  }
372
459
 
373
460
  case 'GO_BACK': {
@@ -375,8 +462,9 @@ export default function TabRouter({
375
462
  return null;
376
463
  }
377
464
 
378
- const previousKey = state.history[state.history.length - 2].key;
379
- const index = state.routes.findIndex(
465
+ const previousHistoryItem = state.history[state.history.length - 2];
466
+ const previousKey = previousHistoryItem?.key;
467
+ const index = state.routes.findLastIndex(
380
468
  (route) => route.key === previousKey
381
469
  );
382
470
 
@@ -384,22 +472,73 @@ export default function TabRouter({
384
472
  return null;
385
473
  }
386
474
 
475
+ let routes = state.routes;
476
+
477
+ if (
478
+ backBehavior === 'fullHistory' &&
479
+ routes[index].params !== previousHistoryItem.params
480
+ ) {
481
+ routes = [...state.routes];
482
+ routes[index] = {
483
+ ...routes[index],
484
+ params: previousHistoryItem.params,
485
+ };
486
+ }
487
+
387
488
  return {
388
489
  ...state,
490
+ routes,
491
+ preloadedRouteKeys: state.preloadedRouteKeys.filter(
492
+ (key) => key !== state.routes[index].key
493
+ ),
389
494
  history: state.history.slice(0, -1),
390
495
  index,
391
496
  };
392
497
  }
393
498
 
499
+ case 'PRELOAD': {
500
+ const routeIndex = state.routes.findIndex(
501
+ (route) => route.name === action.payload.name
502
+ );
503
+
504
+ if (routeIndex === -1) {
505
+ return null;
506
+ }
507
+
508
+ const route = state.routes[routeIndex];
509
+
510
+ const getId = routeGetIdList[route.name];
511
+
512
+ const currentId = getId?.({ params: route.params });
513
+ const nextId = getId?.({ params: action.payload.params });
514
+
515
+ const key =
516
+ currentId === nextId ? route.key : `${route.name}-${nanoid()}`;
517
+
518
+ const params = createParamsFromAction({ action, routeParamList });
519
+ const newRoute =
520
+ params !== route.params ? { ...route, key, params } : route;
521
+
522
+ return {
523
+ ...state,
524
+ preloadedRouteKeys: state.preloadedRouteKeys
525
+ .filter((key) => key !== route.key)
526
+ .concat(newRoute.key),
527
+ routes: state.routes.map((route, index) =>
528
+ index === routeIndex ? newRoute : route
529
+ ),
530
+ history:
531
+ key === route.key
532
+ ? state.history
533
+ : state.history.filter((record) => record.key !== route.key),
534
+ };
535
+ }
536
+
394
537
  default:
395
538
  return BaseRouter.getStateForAction(state, action);
396
539
  }
397
540
  },
398
541
 
399
- shouldActionChangeFocus(action) {
400
- return action.type === 'NAVIGATE';
401
- },
402
-
403
542
  actionCreators: TabActions,
404
543
  };
405
544
 
@@ -0,0 +1,22 @@
1
+ import type { ParamListBase } from './types';
2
+
3
+ type Options = {
4
+ action: {
5
+ payload: {
6
+ name: string;
7
+ params?: object;
8
+ };
9
+ };
10
+ routeParamList: ParamListBase;
11
+ };
12
+
13
+ export function createParamsFromAction({ action, routeParamList }: Options) {
14
+ const { name, params } = action.payload;
15
+
16
+ return routeParamList[name] !== undefined
17
+ ? {
18
+ ...routeParamList[name],
19
+ ...params,
20
+ }
21
+ : params;
22
+ }
@@ -0,0 +1,24 @@
1
+ import { nanoid } from 'nanoid/non-secure';
2
+
3
+ import { createParamsFromAction } from './createParamsFromAction';
4
+ import type { ParamListBase } from './types';
5
+
6
+ type Options = {
7
+ action: {
8
+ payload: {
9
+ name: string;
10
+ params?: object;
11
+ };
12
+ };
13
+ routeParamList: ParamListBase;
14
+ };
15
+
16
+ export function createRouteFromAction({ action, routeParamList }: Options) {
17
+ const { name } = action.payload;
18
+
19
+ return {
20
+ key: `${name}-${nanoid()}`,
21
+ name,
22
+ params: createParamsFromAction({ action, routeParamList }),
23
+ };
24
+ }
package/src/index.tsx CHANGED
@@ -2,7 +2,7 @@ import * as CommonActions from './CommonActions';
2
2
 
3
3
  export { CommonActions };
4
4
 
5
- export { default as BaseRouter } from './BaseRouter';
5
+ export { BaseRouter } from './BaseRouter';
6
6
  export type {
7
7
  DrawerActionHelpers,
8
8
  DrawerActionType,
@@ -10,19 +10,19 @@ export type {
10
10
  DrawerRouterOptions,
11
11
  DrawerStatus,
12
12
  } from './DrawerRouter';
13
- export { DrawerActions, default as DrawerRouter } from './DrawerRouter';
13
+ export { DrawerActions, DrawerRouter } from './DrawerRouter';
14
14
  export type {
15
15
  StackActionHelpers,
16
16
  StackActionType,
17
17
  StackNavigationState,
18
18
  StackRouterOptions,
19
19
  } from './StackRouter';
20
- export { StackActions, default as StackRouter } from './StackRouter';
20
+ export { StackActions, StackRouter } from './StackRouter';
21
21
  export type {
22
22
  TabActionHelpers,
23
23
  TabActionType,
24
24
  TabNavigationState,
25
25
  TabRouterOptions,
26
26
  } from './TabRouter';
27
- export { TabActions, default as TabRouter } from './TabRouter';
27
+ export { TabActions, TabRouter } from './TabRouter';
28
28
  export * from './types';
package/src/types.tsx CHANGED
@@ -2,9 +2,9 @@ import type * as CommonActions from './CommonActions';
2
2
 
3
3
  export type CommonNavigationAction = CommonActions.Action;
4
4
 
5
- type NavigationRoute<
5
+ export type NavigationRoute<
6
6
  ParamList extends ParamListBase,
7
- RouteName extends keyof ParamList
7
+ RouteName extends keyof ParamList,
8
8
  > = Route<Extract<RouteName, string>, ParamList[RouteName]> & {
9
9
  state?: NavigationState | PartialState<NavigationState>;
10
10
  };
@@ -64,7 +64,7 @@ export type PartialState<State extends NavigationState> = Partial<
64
64
 
65
65
  export type Route<
66
66
  RouteName extends string,
67
- Params extends object | undefined = object | undefined
67
+ Params extends object | undefined = object | undefined,
68
68
  > = Readonly<{
69
69
  /**
70
70
  * Unique key for the route.
@@ -130,7 +130,7 @@ export type DefaultRouterOptions<RouteName extends string = string> = {
130
130
  export type RouterFactory<
131
131
  State extends NavigationState,
132
132
  Action extends NavigationAction,
133
- RouterOptions extends DefaultRouterOptions
133
+ RouterOptions extends DefaultRouterOptions,
134
134
  > = (options: RouterOptions) => Router<State, Action>;
135
135
 
136
136
  export type RouterConfigOptions = {
@@ -145,7 +145,7 @@ export type RouterConfigOptions = {
145
145
 
146
146
  export type Router<
147
147
  State extends NavigationState,
148
- Action extends NavigationAction
148
+ Action extends NavigationAction,
149
149
  > = {
150
150
  /**
151
151
  * Type of the router. Should match the `type` property in state.