@amazon-devices/react-navigation__routers 2.0.1758683737

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 (60) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +17 -0
  3. package/SystemBundles/b690027feea4132f8720e462805cba84c12cd0e25f99e321a1a2c8b794832a91.bundle +3515 -0
  4. package/SystemBundles/b690027feea4132f8720e462805cba84c12cd0e25f99e321a1a2c8b794832a91.map +1 -0
  5. package/SystemModules/amzn__react-navigation__routers-2.modules.txt +21 -0
  6. package/SystemModules/amzn__react-navigation__routers-2.private.modules.txt +13 -0
  7. package/SystemModules/keplerscript-system-bundle-config.json +7 -0
  8. package/THIRD_PARTY_NOTICES.txt +24 -0
  9. package/kepler-compatibility.json +8 -0
  10. package/kepler-transformed-package-info.json +3 -0
  11. package/lib/commonjs/BaseRouter.js +61 -0
  12. package/lib/commonjs/BaseRouter.js.map +1 -0
  13. package/lib/commonjs/CommonActions.js +53 -0
  14. package/lib/commonjs/CommonActions.js.map +1 -0
  15. package/lib/commonjs/DrawerRouter.js +154 -0
  16. package/lib/commonjs/DrawerRouter.js.map +1 -0
  17. package/lib/commonjs/StackRouter.js +310 -0
  18. package/lib/commonjs/StackRouter.js.map +1 -0
  19. package/lib/commonjs/TabRouter.js +253 -0
  20. package/lib/commonjs/TabRouter.js.map +1 -0
  21. package/lib/commonjs/index.js +80 -0
  22. package/lib/commonjs/index.js.map +1 -0
  23. package/lib/commonjs/types.js +6 -0
  24. package/lib/commonjs/types.js.map +1 -0
  25. package/lib/module/BaseRouter.js +55 -0
  26. package/lib/module/BaseRouter.js.map +1 -0
  27. package/lib/module/CommonActions.js +44 -0
  28. package/lib/module/CommonActions.js.map +1 -0
  29. package/lib/module/DrawerRouter.js +145 -0
  30. package/lib/module/DrawerRouter.js.map +1 -0
  31. package/lib/module/StackRouter.js +302 -0
  32. package/lib/module/StackRouter.js.map +1 -0
  33. package/lib/module/TabRouter.js +245 -0
  34. package/lib/module/TabRouter.js.map +1 -0
  35. package/lib/module/index.js +8 -0
  36. package/lib/module/index.js.map +1 -0
  37. package/lib/module/types.js +2 -0
  38. package/lib/module/types.js.map +1 -0
  39. package/lib/typescript/src/BaseRouter.d.ts +27 -0
  40. package/lib/typescript/src/BaseRouter.d.ts.map +1 -0
  41. package/lib/typescript/src/CommonActions.d.ts +56 -0
  42. package/lib/typescript/src/CommonActions.d.ts.map +1 -0
  43. package/lib/typescript/src/DrawerRouter.d.ts +53 -0
  44. package/lib/typescript/src/DrawerRouter.d.ts.map +1 -0
  45. package/lib/typescript/src/StackRouter.d.ts +69 -0
  46. package/lib/typescript/src/StackRouter.d.ts.map +1 -0
  47. package/lib/typescript/src/TabRouter.d.ts +41 -0
  48. package/lib/typescript/src/TabRouter.d.ts.map +1 -0
  49. package/lib/typescript/src/index.d.ts +11 -0
  50. package/lib/typescript/src/index.d.ts.map +1 -0
  51. package/lib/typescript/src/types.d.ts +177 -0
  52. package/lib/typescript/src/types.d.ts.map +1 -0
  53. package/package.json +1 -0
  54. package/src/BaseRouter.tsx +81 -0
  55. package/src/CommonActions.tsx +89 -0
  56. package/src/DrawerRouter.tsx +247 -0
  57. package/src/StackRouter.tsx +495 -0
  58. package/src/TabRouter.tsx +407 -0
  59. package/src/index.tsx +28 -0
  60. package/src/types.tsx +228 -0
@@ -0,0 +1,495 @@
1
+ import { nanoid } from 'nanoid/non-secure';
2
+
3
+ import BaseRouter from './BaseRouter';
4
+ import type {
5
+ CommonNavigationAction,
6
+ DefaultRouterOptions,
7
+ NavigationState,
8
+ ParamListBase,
9
+ Route,
10
+ Router,
11
+ } from './types';
12
+
13
+ export type StackActionType =
14
+ | {
15
+ type: 'REPLACE';
16
+ payload: { name: string; key?: string | undefined; params?: object };
17
+ source?: string;
18
+ target?: string;
19
+ }
20
+ | {
21
+ type: 'PUSH';
22
+ payload: { name: string; params?: object };
23
+ source?: string;
24
+ target?: string;
25
+ }
26
+ | {
27
+ type: 'POP';
28
+ payload: { count: number };
29
+ source?: string;
30
+ target?: string;
31
+ }
32
+ | {
33
+ type: 'POP_TO_TOP';
34
+ source?: string;
35
+ target?: string;
36
+ };
37
+
38
+ export type StackRouterOptions = DefaultRouterOptions;
39
+
40
+ export type StackNavigationState<ParamList extends ParamListBase> =
41
+ NavigationState<ParamList> & {
42
+ /**
43
+ * Type of the router, in this case, it's stack.
44
+ */
45
+ type: 'stack';
46
+ };
47
+
48
+ export type StackActionHelpers<ParamList extends ParamListBase> = {
49
+ /**
50
+ * Replace the current route with a new one.
51
+ *
52
+ * @param name Route name of the new route.
53
+ * @param [params] Params object for the new route.
54
+ */
55
+ replace<RouteName extends keyof ParamList>(
56
+ ...args: undefined extends ParamList[RouteName]
57
+ ? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
58
+ : [screen: RouteName, params: ParamList[RouteName]]
59
+ ): void;
60
+
61
+ /**
62
+ * Push a new screen onto the stack.
63
+ *
64
+ * @param name Name of the route for the tab.
65
+ * @param [params] Params object for the route.
66
+ */
67
+ push<RouteName extends keyof ParamList>(
68
+ ...args: undefined extends ParamList[RouteName]
69
+ ? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
70
+ : [screen: RouteName, params: ParamList[RouteName]]
71
+ ): void;
72
+
73
+ /**
74
+ * Pop a screen from the stack.
75
+ */
76
+ pop(count?: number): void;
77
+
78
+ /**
79
+ * Pop to the first route in the stack, dismissing all other screens.
80
+ */
81
+ popToTop(): void;
82
+ };
83
+
84
+ export const StackActions = {
85
+ replace(name: string, params?: object): StackActionType {
86
+ return { type: 'REPLACE', payload: { name, params } };
87
+ },
88
+ push(name: string, params?: object): StackActionType {
89
+ return { type: 'PUSH', payload: { name, params } };
90
+ },
91
+ pop(count: number = 1): StackActionType {
92
+ return { type: 'POP', payload: { count } };
93
+ },
94
+ popToTop(): StackActionType {
95
+ return { type: 'POP_TO_TOP' };
96
+ },
97
+ };
98
+
99
+ export default function StackRouter(options: StackRouterOptions) {
100
+ const router: Router<
101
+ StackNavigationState<ParamListBase>,
102
+ CommonNavigationAction | StackActionType
103
+ > = {
104
+ ...BaseRouter,
105
+
106
+ type: 'stack',
107
+
108
+ getInitialState({ routeNames, routeParamList }) {
109
+ const initialRouteName =
110
+ options.initialRouteName !== undefined &&
111
+ routeNames.includes(options.initialRouteName)
112
+ ? options.initialRouteName
113
+ : routeNames[0];
114
+
115
+ return {
116
+ stale: false,
117
+ type: 'stack',
118
+ key: `stack-${nanoid()}`,
119
+ index: 0,
120
+ routeNames,
121
+ routes: [
122
+ {
123
+ key: `${initialRouteName}-${nanoid()}`,
124
+ name: initialRouteName,
125
+ params: routeParamList[initialRouteName],
126
+ },
127
+ ],
128
+ };
129
+ },
130
+
131
+ getRehydratedState(partialState, { routeNames, routeParamList }) {
132
+ let state = partialState;
133
+
134
+ if (state.stale === false) {
135
+ return state;
136
+ }
137
+
138
+ const routes = state.routes
139
+ .filter((route) => routeNames.includes(route.name))
140
+ .map(
141
+ (route) =>
142
+ ({
143
+ ...route,
144
+ key: route.key || `${route.name}-${nanoid()}`,
145
+ params:
146
+ routeParamList[route.name] !== undefined
147
+ ? {
148
+ ...routeParamList[route.name],
149
+ ...route.params,
150
+ }
151
+ : route.params,
152
+ } as Route<string>)
153
+ );
154
+
155
+ if (routes.length === 0) {
156
+ const initialRouteName =
157
+ options.initialRouteName !== undefined
158
+ ? options.initialRouteName
159
+ : routeNames[0];
160
+
161
+ routes.push({
162
+ key: `${initialRouteName}-${nanoid()}`,
163
+ name: initialRouteName,
164
+ params: routeParamList[initialRouteName],
165
+ });
166
+ }
167
+
168
+ return {
169
+ stale: false,
170
+ type: 'stack',
171
+ key: `stack-${nanoid()}`,
172
+ index: routes.length - 1,
173
+ routeNames,
174
+ routes,
175
+ };
176
+ },
177
+
178
+ getStateForRouteNamesChange(
179
+ state,
180
+ { routeNames, routeParamList, routeKeyChanges }
181
+ ) {
182
+ const routes = state.routes.filter(
183
+ (route) =>
184
+ routeNames.includes(route.name) &&
185
+ !routeKeyChanges.includes(route.name)
186
+ );
187
+
188
+ if (routes.length === 0) {
189
+ const initialRouteName =
190
+ options.initialRouteName !== undefined &&
191
+ routeNames.includes(options.initialRouteName)
192
+ ? options.initialRouteName
193
+ : routeNames[0];
194
+
195
+ routes.push({
196
+ key: `${initialRouteName}-${nanoid()}`,
197
+ name: initialRouteName,
198
+ params: routeParamList[initialRouteName],
199
+ });
200
+ }
201
+
202
+ return {
203
+ ...state,
204
+ routeNames,
205
+ routes,
206
+ index: Math.min(state.index, routes.length - 1),
207
+ };
208
+ },
209
+
210
+ getStateForRouteFocus(state, key) {
211
+ const index = state.routes.findIndex((r) => r.key === key);
212
+
213
+ if (index === -1 || index === state.index) {
214
+ return state;
215
+ }
216
+
217
+ return {
218
+ ...state,
219
+ index,
220
+ routes: state.routes.slice(0, index + 1),
221
+ };
222
+ },
223
+
224
+ getStateForAction(state, action, options) {
225
+ const { routeParamList } = options;
226
+
227
+ switch (action.type) {
228
+ case 'REPLACE': {
229
+ const index =
230
+ action.target === state.key && action.source
231
+ ? state.routes.findIndex((r) => r.key === action.source)
232
+ : state.index;
233
+
234
+ if (index === -1) {
235
+ return null;
236
+ }
237
+
238
+ const { name, key, params } = action.payload;
239
+
240
+ if (!state.routeNames.includes(name)) {
241
+ return null;
242
+ }
243
+
244
+ return {
245
+ ...state,
246
+ routes: state.routes.map((route, i) =>
247
+ i === index
248
+ ? {
249
+ key: key !== undefined ? key : `${name}-${nanoid()}`,
250
+ name,
251
+ params:
252
+ routeParamList[name] !== undefined
253
+ ? {
254
+ ...routeParamList[name],
255
+ ...params,
256
+ }
257
+ : params,
258
+ }
259
+ : route
260
+ ),
261
+ };
262
+ }
263
+
264
+ case 'PUSH':
265
+ if (state.routeNames.includes(action.payload.name)) {
266
+ const getId = options.routeGetIdList[action.payload.name];
267
+ const id = getId?.({ params: action.payload.params });
268
+
269
+ const route = id
270
+ ? state.routes.find(
271
+ (route) =>
272
+ route.name === action.payload.name &&
273
+ id === getId?.({ params: route.params })
274
+ )
275
+ : undefined;
276
+
277
+ let routes: Route<string>[];
278
+
279
+ if (route) {
280
+ routes = state.routes.filter((r) => r.key !== route.key);
281
+ routes.push({
282
+ ...route,
283
+ params:
284
+ routeParamList[action.payload.name] !== undefined
285
+ ? {
286
+ ...routeParamList[action.payload.name],
287
+ ...action.payload.params,
288
+ }
289
+ : action.payload.params,
290
+ });
291
+ } else {
292
+ routes = [
293
+ ...state.routes,
294
+ {
295
+ key: `${action.payload.name}-${nanoid()}`,
296
+ name: action.payload.name,
297
+ params:
298
+ routeParamList[action.payload.name] !== undefined
299
+ ? {
300
+ ...routeParamList[action.payload.name],
301
+ ...action.payload.params,
302
+ }
303
+ : action.payload.params,
304
+ },
305
+ ];
306
+ }
307
+
308
+ return {
309
+ ...state,
310
+ index: routes.length - 1,
311
+ routes,
312
+ };
313
+ }
314
+
315
+ return null;
316
+
317
+ case 'POP': {
318
+ const index =
319
+ action.target === state.key && action.source
320
+ ? state.routes.findIndex((r) => r.key === action.source)
321
+ : state.index;
322
+
323
+ if (index > 0) {
324
+ const count = Math.max(index - action.payload.count + 1, 1);
325
+ const routes = state.routes
326
+ .slice(0, count)
327
+ .concat(state.routes.slice(index + 1));
328
+
329
+ return {
330
+ ...state,
331
+ index: routes.length - 1,
332
+ routes,
333
+ };
334
+ }
335
+
336
+ return null;
337
+ }
338
+
339
+ case 'POP_TO_TOP':
340
+ return router.getStateForAction(
341
+ state,
342
+ {
343
+ type: 'POP',
344
+ payload: { count: state.routes.length - 1 },
345
+ },
346
+ options
347
+ );
348
+
349
+ case 'NAVIGATE':
350
+ if (
351
+ action.payload.name !== undefined &&
352
+ !state.routeNames.includes(action.payload.name)
353
+ ) {
354
+ return null;
355
+ }
356
+
357
+ if (action.payload.key || action.payload.name) {
358
+ // If the route already exists, navigate to that
359
+ let index = -1;
360
+
361
+ const getId =
362
+ // `getId` and `key` can't be used together
363
+ action.payload.key === undefined &&
364
+ action.payload.name !== undefined
365
+ ? options.routeGetIdList[action.payload.name]
366
+ : undefined;
367
+ const id = getId?.({ params: action.payload.params });
368
+
369
+ if (id) {
370
+ index = state.routes.findIndex(
371
+ (route) =>
372
+ route.name === action.payload.name &&
373
+ id === getId?.({ params: route.params })
374
+ );
375
+ } else if (
376
+ (state.routes[state.index].name === action.payload.name &&
377
+ action.payload.key === undefined) ||
378
+ state.routes[state.index].key === action.payload.key
379
+ ) {
380
+ index = state.index;
381
+ } else {
382
+ for (let i = state.routes.length - 1; i >= 0; i--) {
383
+ if (
384
+ (state.routes[i].name === action.payload.name &&
385
+ action.payload.key === undefined) ||
386
+ state.routes[i].key === action.payload.key
387
+ ) {
388
+ index = i;
389
+ break;
390
+ }
391
+ }
392
+ }
393
+
394
+ if (
395
+ index === -1 &&
396
+ action.payload.key &&
397
+ action.payload.name === undefined
398
+ ) {
399
+ return null;
400
+ }
401
+
402
+ if (index === -1 && action.payload.name !== undefined) {
403
+ const routes = [
404
+ ...state.routes,
405
+ {
406
+ key:
407
+ action.payload.key ?? `${action.payload.name}-${nanoid()}`,
408
+ name: action.payload.name,
409
+ path: action.payload.path,
410
+ params:
411
+ routeParamList[action.payload.name] !== undefined
412
+ ? {
413
+ ...routeParamList[action.payload.name],
414
+ ...action.payload.params,
415
+ }
416
+ : action.payload.params,
417
+ },
418
+ ];
419
+
420
+ return {
421
+ ...state,
422
+ routes,
423
+ index: routes.length - 1,
424
+ };
425
+ }
426
+
427
+ const route = state.routes[index];
428
+
429
+ let params;
430
+
431
+ if (action.payload.merge) {
432
+ params =
433
+ action.payload.params !== undefined ||
434
+ routeParamList[route.name] !== undefined
435
+ ? {
436
+ ...routeParamList[route.name],
437
+ ...route.params,
438
+ ...action.payload.params,
439
+ }
440
+ : route.params;
441
+ } else {
442
+ params =
443
+ routeParamList[route.name] !== undefined
444
+ ? {
445
+ ...routeParamList[route.name],
446
+ ...action.payload.params,
447
+ }
448
+ : action.payload.params;
449
+ }
450
+
451
+ return {
452
+ ...state,
453
+ index,
454
+ routes: [
455
+ ...state.routes.slice(0, index),
456
+ params !== route.params ||
457
+ (action.payload.path && action.payload.path !== route.path)
458
+ ? {
459
+ ...route,
460
+ path: action.payload.path ?? route.path,
461
+ params,
462
+ }
463
+ : state.routes[index],
464
+ ],
465
+ };
466
+ }
467
+
468
+ return null;
469
+
470
+ case 'GO_BACK':
471
+ if (state.index > 0) {
472
+ return router.getStateForAction(
473
+ state,
474
+ {
475
+ type: 'POP',
476
+ payload: { count: 1 },
477
+ target: action.target,
478
+ source: action.source,
479
+ },
480
+ options
481
+ );
482
+ }
483
+
484
+ return null;
485
+
486
+ default:
487
+ return BaseRouter.getStateForAction(state, action);
488
+ }
489
+ },
490
+
491
+ actionCreators: StackActions,
492
+ };
493
+
494
+ return router;
495
+ }