@amazon-devices/react-navigation__routers 8.0.9000000000 → 8.0.9000000002

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.
@@ -1,85 +0,0 @@
1
- "use strict";
2
-
3
- export function goBack() {
4
- return {
5
- type: 'GO_BACK'
6
- };
7
- }
8
- export function navigate(...args) {
9
- if (typeof args[0] === 'string') {
10
- const [name, params, options] = args;
11
- if (typeof options === 'boolean') {
12
- console.warn(`Passing a boolean as the third argument to 'navigate' is deprecated. Pass '{ merge: true }' instead.`);
13
- }
14
- return {
15
- type: 'NAVIGATE',
16
- payload: {
17
- name,
18
- params,
19
- merge: typeof options === 'boolean' ? options : options?.merge,
20
- pop: options?.pop
21
- }
22
- };
23
- } else {
24
- const payload = args[0] || {};
25
- if (!('name' in payload)) {
26
- throw new Error('You need to specify a name when calling navigate with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigate for usage.');
27
- }
28
- return {
29
- type: 'NAVIGATE',
30
- payload
31
- };
32
- }
33
- }
34
- export function navigateDeprecated(...args) {
35
- if (typeof args[0] === 'string') {
36
- return {
37
- type: 'NAVIGATE_DEPRECATED',
38
- payload: {
39
- name: args[0],
40
- params: args[1]
41
- }
42
- };
43
- } else {
44
- const payload = args[0] || {};
45
- if (!('name' in payload)) {
46
- throw new Error('You need to specify a name when calling navigateDeprecated with an object as the argument. See https://reactnavigation.org/docs/navigation-actions#navigatelegacy for usage.');
47
- }
48
- return {
49
- type: 'NAVIGATE_DEPRECATED',
50
- payload
51
- };
52
- }
53
- }
54
- export function reset(state) {
55
- return {
56
- type: 'RESET',
57
- payload: state
58
- };
59
- }
60
- export function setParams(params) {
61
- return {
62
- type: 'SET_PARAMS',
63
- payload: {
64
- params
65
- }
66
- };
67
- }
68
- export function replaceParams(params) {
69
- return {
70
- type: 'REPLACE_PARAMS',
71
- payload: {
72
- params
73
- }
74
- };
75
- }
76
- export function preload(name, params) {
77
- return {
78
- type: 'PRELOAD',
79
- payload: {
80
- name,
81
- params
82
- }
83
- };
84
- }
85
- //# sourceMappingURL=CommonActions.js.map
@@ -1,145 +0,0 @@
1
- "use strict";
2
-
3
- import { nanoid } from 'nanoid/non-secure';
4
- import { TabActions, TabRouter } from "./TabRouter.js";
5
- export const DrawerActions = {
6
- ...TabActions,
7
- openDrawer() {
8
- return {
9
- type: 'OPEN_DRAWER'
10
- };
11
- },
12
- closeDrawer() {
13
- return {
14
- type: 'CLOSE_DRAWER'
15
- };
16
- },
17
- toggleDrawer() {
18
- return {
19
- type: 'TOGGLE_DRAWER'
20
- };
21
- }
22
- };
23
- export function DrawerRouter({
24
- defaultStatus = 'closed',
25
- ...rest
26
- }) {
27
- const router = TabRouter(rest);
28
- const isDrawerInHistory = state => Boolean(state.history?.some(it => it.type === 'drawer'));
29
- const addDrawerToHistory = state => {
30
- if (isDrawerInHistory(state)) {
31
- return state;
32
- }
33
- return {
34
- ...state,
35
- history: [...state.history, {
36
- type: 'drawer',
37
- status: defaultStatus === 'open' ? 'closed' : 'open'
38
- }]
39
- };
40
- };
41
- const removeDrawerFromHistory = state => {
42
- if (!isDrawerInHistory(state)) {
43
- return state;
44
- }
45
- return {
46
- ...state,
47
- history: state.history.filter(it => it.type !== 'drawer')
48
- };
49
- };
50
- const openDrawer = state => {
51
- if (defaultStatus === 'open') {
52
- return removeDrawerFromHistory(state);
53
- }
54
- return addDrawerToHistory(state);
55
- };
56
- const closeDrawer = state => {
57
- if (defaultStatus === 'open') {
58
- return addDrawerToHistory(state);
59
- }
60
- return removeDrawerFromHistory(state);
61
- };
62
- return {
63
- ...router,
64
- type: 'drawer',
65
- getInitialState({
66
- routeNames,
67
- routeParamList,
68
- routeGetIdList
69
- }) {
70
- const state = router.getInitialState({
71
- routeNames,
72
- routeParamList,
73
- routeGetIdList
74
- });
75
- return {
76
- ...state,
77
- default: defaultStatus,
78
- stale: false,
79
- type: 'drawer',
80
- key: `drawer-${nanoid()}`
81
- };
82
- },
83
- getRehydratedState(partialState, {
84
- routeNames,
85
- routeParamList,
86
- routeGetIdList
87
- }) {
88
- if (partialState.stale === false) {
89
- return partialState;
90
- }
91
- let state = router.getRehydratedState(partialState, {
92
- routeNames,
93
- routeParamList,
94
- routeGetIdList
95
- });
96
- if (isDrawerInHistory(partialState)) {
97
- // Re-sync the drawer entry in history to correct it if it was wrong
98
- state = removeDrawerFromHistory(state);
99
- state = addDrawerToHistory(state);
100
- }
101
- return {
102
- ...state,
103
- default: defaultStatus,
104
- type: 'drawer',
105
- key: `drawer-${nanoid()}`
106
- };
107
- },
108
- getStateForRouteFocus(state, key) {
109
- const result = router.getStateForRouteFocus(state, key);
110
- return closeDrawer(result);
111
- },
112
- getStateForAction(state, action, options) {
113
- switch (action.type) {
114
- case 'OPEN_DRAWER':
115
- return openDrawer(state);
116
- case 'CLOSE_DRAWER':
117
- return closeDrawer(state);
118
- case 'TOGGLE_DRAWER':
119
- if (isDrawerInHistory(state)) {
120
- return removeDrawerFromHistory(state);
121
- }
122
- return addDrawerToHistory(state);
123
- case 'JUMP_TO':
124
- case 'NAVIGATE':
125
- case 'NAVIGATE_DEPRECATED':
126
- {
127
- const result = router.getStateForAction(state, action, options);
128
- if (result != null && result.index !== state.index) {
129
- return closeDrawer(result);
130
- }
131
- return result;
132
- }
133
- case 'GO_BACK':
134
- if (isDrawerInHistory(state)) {
135
- return removeDrawerFromHistory(state);
136
- }
137
- return router.getStateForAction(state, action, options);
138
- default:
139
- return router.getStateForAction(state, action, options);
140
- }
141
- },
142
- actionCreators: DrawerActions
143
- };
144
- }
145
- //# sourceMappingURL=DrawerRouter.js.map
@@ -1,465 +0,0 @@
1
- "use strict";
2
-
3
- import { nanoid } from 'nanoid/non-secure';
4
- import { BaseRouter } from "./BaseRouter.js";
5
- import { createParamsFromAction } from "./createParamsFromAction.js";
6
- import { createRouteFromAction } from "./createRouteFromAction.js";
7
- export const StackActions = {
8
- replace(name, params) {
9
- return {
10
- type: 'REPLACE',
11
- payload: {
12
- name,
13
- params
14
- }
15
- };
16
- },
17
- push(name, params) {
18
- return {
19
- type: 'PUSH',
20
- payload: {
21
- name,
22
- params
23
- }
24
- };
25
- },
26
- pop(count = 1) {
27
- return {
28
- type: 'POP',
29
- payload: {
30
- count
31
- }
32
- };
33
- },
34
- popToTop() {
35
- return {
36
- type: 'POP_TO_TOP'
37
- };
38
- },
39
- popTo(name, params, options) {
40
- if (typeof options === 'boolean') {
41
- console.warn(`Passing a boolean as the third argument to 'popTo' is deprecated. Pass '{ merge: true }' instead.`);
42
- }
43
- return {
44
- type: 'POP_TO',
45
- payload: {
46
- name,
47
- params,
48
- merge: typeof options === 'boolean' ? options : options?.merge
49
- }
50
- };
51
- }
52
- };
53
- export function StackRouter(options) {
54
- const router = {
55
- ...BaseRouter,
56
- type: 'stack',
57
- getInitialState({
58
- routeNames,
59
- routeParamList
60
- }) {
61
- const initialRouteName = options.initialRouteName !== undefined && routeNames.includes(options.initialRouteName) ? options.initialRouteName : routeNames[0];
62
- return {
63
- stale: false,
64
- type: 'stack',
65
- key: `stack-${nanoid()}`,
66
- index: 0,
67
- routeNames,
68
- preloadedRoutes: [],
69
- routes: [{
70
- key: `${initialRouteName}-${nanoid()}`,
71
- name: initialRouteName,
72
- params: routeParamList[initialRouteName]
73
- }]
74
- };
75
- },
76
- getRehydratedState(partialState, {
77
- routeNames,
78
- routeParamList
79
- }) {
80
- const state = partialState;
81
- if (state.stale === false) {
82
- return state;
83
- }
84
- const routes = state.routes.filter(route => routeNames.includes(route.name)).map(route => ({
85
- ...route,
86
- key: route.key || `${route.name}-${nanoid()}`,
87
- params: routeParamList[route.name] !== undefined ? {
88
- ...routeParamList[route.name],
89
- ...route.params
90
- } : route.params
91
- }));
92
- const preloadedRoutes = state.preloadedRoutes?.filter(route => routeNames.includes(route.name)).map(route => ({
93
- ...route,
94
- key: route.key || `${route.name}-${nanoid()}`,
95
- params: routeParamList[route.name] !== undefined ? {
96
- ...routeParamList[route.name],
97
- ...route.params
98
- } : route.params
99
- })) ?? [];
100
- if (routes.length === 0) {
101
- const initialRouteName = options.initialRouteName !== undefined ? options.initialRouteName : routeNames[0];
102
- routes.push({
103
- key: `${initialRouteName}-${nanoid()}`,
104
- name: initialRouteName,
105
- params: routeParamList[initialRouteName]
106
- });
107
- }
108
- return {
109
- stale: false,
110
- type: 'stack',
111
- key: `stack-${nanoid()}`,
112
- index: routes.length - 1,
113
- routeNames,
114
- routes,
115
- preloadedRoutes
116
- };
117
- },
118
- getStateForRouteNamesChange(state, {
119
- routeNames,
120
- routeParamList,
121
- routeKeyChanges
122
- }) {
123
- const routes = state.routes.filter(route => routeNames.includes(route.name) && !routeKeyChanges.includes(route.name));
124
- if (routes.length === 0) {
125
- const initialRouteName = options.initialRouteName !== undefined && routeNames.includes(options.initialRouteName) ? options.initialRouteName : routeNames[0];
126
- routes.push({
127
- key: `${initialRouteName}-${nanoid()}`,
128
- name: initialRouteName,
129
- params: routeParamList[initialRouteName]
130
- });
131
- }
132
- return {
133
- ...state,
134
- routeNames,
135
- routes,
136
- index: Math.min(state.index, routes.length - 1)
137
- };
138
- },
139
- getStateForRouteFocus(state, key) {
140
- const index = state.routes.findIndex(r => r.key === key);
141
- if (index === -1 || index === state.index) {
142
- return state;
143
- }
144
- return {
145
- ...state,
146
- index,
147
- routes: state.routes.slice(0, index + 1)
148
- };
149
- },
150
- getStateForAction(state, action, options) {
151
- const {
152
- routeParamList
153
- } = options;
154
- switch (action.type) {
155
- case 'REPLACE':
156
- {
157
- const currentIndex = action.target === state.key && action.source ? state.routes.findIndex(r => r.key === action.source) : state.index;
158
- if (currentIndex === -1) {
159
- return null;
160
- }
161
- if (!state.routeNames.includes(action.payload.name)) {
162
- return null;
163
- }
164
- const getId = options.routeGetIdList[action.payload.name];
165
- const id = getId?.({
166
- params: action.payload.params
167
- });
168
-
169
- // Re-use preloaded route if available
170
- let route = state.preloadedRoutes.find(route => route.name === action.payload.name && id === getId?.({
171
- params: route.params
172
- }));
173
- if (!route) {
174
- route = createRouteFromAction({
175
- action,
176
- routeParamList
177
- });
178
- }
179
- return {
180
- ...state,
181
- routes: state.routes.map((r, i) => i === currentIndex ? route : r),
182
- preloadedRoutes: state.preloadedRoutes.filter(r => r.key !== route.key)
183
- };
184
- }
185
- case 'PUSH':
186
- case 'NAVIGATE':
187
- {
188
- if (!state.routeNames.includes(action.payload.name)) {
189
- return null;
190
- }
191
- const getId = options.routeGetIdList[action.payload.name];
192
- const id = getId?.({
193
- params: action.payload.params
194
- });
195
- let route;
196
- if (id !== undefined) {
197
- route = state.routes.find(route => route.name === action.payload.name && id === getId?.({
198
- params: route.params
199
- }));
200
- } else {
201
- const currentRoute = state.routes[state.index];
202
-
203
- // If the route matches the current one, then navigate to it
204
- if (action.type === 'NAVIGATE' && action.payload.name === currentRoute.name) {
205
- route = currentRoute;
206
- }
207
- }
208
- if (!route) {
209
- route = state.preloadedRoutes.find(route => route.name === action.payload.name && id === getId?.({
210
- params: route.params
211
- }));
212
- }
213
- let params;
214
- if (action.type === 'NAVIGATE' && action.payload.merge && route) {
215
- params = action.payload.params !== undefined || routeParamList[action.payload.name] !== undefined ? {
216
- ...routeParamList[action.payload.name],
217
- ...route.params,
218
- ...action.payload.params
219
- } : route.params;
220
- } else {
221
- params = createParamsFromAction({
222
- action,
223
- routeParamList
224
- });
225
- }
226
- let routes;
227
- if (route) {
228
- const routeKey = route.key;
229
- routes = state.routes.filter(r => r.key !== routeKey);
230
- routes.push({
231
- ...route,
232
- path: action.type === 'NAVIGATE' && action.payload.path !== undefined ? action.payload.path : route.path,
233
- params
234
- });
235
- } else {
236
- routes = [...state.routes, {
237
- key: `${action.payload.name}-${nanoid()}`,
238
- name: action.payload.name,
239
- path: action.type === 'NAVIGATE' ? action.payload.path : undefined,
240
- params
241
- }];
242
- }
243
- return {
244
- ...state,
245
- index: routes.length - 1,
246
- preloadedRoutes: state.preloadedRoutes.filter(route => routes[routes.length - 1].key !== route.key),
247
- routes
248
- };
249
- }
250
- case 'NAVIGATE_DEPRECATED':
251
- {
252
- if (!state.routeNames.includes(action.payload.name)) {
253
- return null;
254
- }
255
- if (state.preloadedRoutes.find(route => route.name === action.payload.name && id === getId?.({
256
- params: route.params
257
- }))) {
258
- return null;
259
- }
260
-
261
- // If the route already exists, navigate to that
262
- let index = -1;
263
- const getId = options.routeGetIdList[action.payload.name];
264
- const id = getId?.({
265
- params: action.payload.params
266
- });
267
- if (id !== undefined) {
268
- index = state.routes.findIndex(route => route.name === action.payload.name && id === getId?.({
269
- params: route.params
270
- }));
271
- } else if (state.routes[state.index].name === action.payload.name) {
272
- index = state.index;
273
- } else {
274
- index = state.routes.findLastIndex(route => route.name === action.payload.name);
275
- }
276
- if (index === -1) {
277
- const routes = [...state.routes, createRouteFromAction({
278
- action,
279
- routeParamList
280
- })];
281
- return {
282
- ...state,
283
- routes,
284
- index: routes.length - 1
285
- };
286
- }
287
- const route = state.routes[index];
288
- let params;
289
- if (action.payload.merge) {
290
- params = action.payload.params !== undefined || routeParamList[route.name] !== undefined ? {
291
- ...routeParamList[route.name],
292
- ...route.params,
293
- ...action.payload.params
294
- } : route.params;
295
- } else {
296
- params = createParamsFromAction({
297
- action,
298
- routeParamList
299
- });
300
- }
301
- return {
302
- ...state,
303
- index,
304
- routes: [...state.routes.slice(0, index), params !== route.params ? {
305
- ...route,
306
- params
307
- } : state.routes[index]]
308
- };
309
- }
310
- case 'POP':
311
- {
312
- const currentIndex = action.target === state.key && action.source ? state.routes.findIndex(r => r.key === action.source) : state.index;
313
- if (currentIndex > 0) {
314
- const count = Math.max(currentIndex - action.payload.count + 1, 1);
315
- const routes = state.routes.slice(0, count).concat(state.routes.slice(currentIndex + 1));
316
- return {
317
- ...state,
318
- index: routes.length - 1,
319
- routes
320
- };
321
- }
322
- return null;
323
- }
324
- case 'POP_TO_TOP':
325
- return router.getStateForAction(state, {
326
- type: 'POP',
327
- payload: {
328
- count: state.routes.length - 1
329
- }
330
- }, options);
331
- case 'POP_TO':
332
- {
333
- const currentIndex = action.target === state.key && action.source ? state.routes.findLastIndex(r => r.key === action.source) : state.index;
334
- if (currentIndex === -1) {
335
- return null;
336
- }
337
- if (!state.routeNames.includes(action.payload.name)) {
338
- return null;
339
- }
340
-
341
- // If the route already exists, navigate to it
342
- let index = -1;
343
- const getId = options.routeGetIdList[action.payload.name];
344
- const id = getId?.({
345
- params: action.payload.params
346
- });
347
- if (id !== undefined) {
348
- index = state.routes.findIndex(route => route.name === action.payload.name && id === getId?.({
349
- params: route.params
350
- }));
351
- } else if (state.routes[currentIndex].name === action.payload.name) {
352
- index = currentIndex;
353
- } else {
354
- for (let i = currentIndex; i >= 0; i--) {
355
- if (state.routes[i].name === action.payload.name) {
356
- index = i;
357
- break;
358
- }
359
- }
360
- }
361
-
362
- // If the route doesn't exist, remove the current route and add the new one
363
- if (index === -1) {
364
- // Re-use preloaded route if available
365
- let route = state.preloadedRoutes.find(route => route.name === action.payload.name && id === getId?.({
366
- params: route.params
367
- }));
368
- if (!route) {
369
- route = createRouteFromAction({
370
- action,
371
- routeParamList
372
- });
373
- }
374
- const routes = state.routes.slice(0, currentIndex).concat(route);
375
- return {
376
- ...state,
377
- index: routes.length - 1,
378
- routes,
379
- preloadedRoutes: state.preloadedRoutes.filter(r => r.key !== route.key)
380
- };
381
- }
382
- const route = state.routes[index];
383
- let params;
384
- if (action.payload.merge) {
385
- params = action.payload.params !== undefined || routeParamList[route.name] !== undefined ? {
386
- ...routeParamList[route.name],
387
- ...route.params,
388
- ...action.payload.params
389
- } : route.params;
390
- } else {
391
- params = createParamsFromAction({
392
- action,
393
- routeParamList
394
- });
395
- }
396
- return {
397
- ...state,
398
- index,
399
- routes: [...state.routes.slice(0, index), params !== route.params ? {
400
- ...route,
401
- params
402
- } : state.routes[index]]
403
- };
404
- }
405
- case 'GO_BACK':
406
- if (state.index > 0) {
407
- return router.getStateForAction(state, {
408
- type: 'POP',
409
- payload: {
410
- count: 1
411
- },
412
- target: action.target,
413
- source: action.source
414
- }, options);
415
- }
416
- return null;
417
- case 'PRELOAD':
418
- {
419
- const getId = options.routeGetIdList[action.payload.name];
420
- const id = getId?.({
421
- params: action.payload.params
422
- });
423
- let route;
424
- if (id !== undefined) {
425
- route = state.routes.find(route => route.name === action.payload.name && id === getId?.({
426
- params: route.params
427
- }));
428
- }
429
- if (route) {
430
- return {
431
- ...state,
432
- routes: state.routes.map(r => {
433
- if (r.key !== route?.key) {
434
- return r;
435
- }
436
- return {
437
- ...r,
438
- params: createParamsFromAction({
439
- action,
440
- routeParamList
441
- })
442
- };
443
- })
444
- };
445
- } else {
446
- return {
447
- ...state,
448
- preloadedRoutes: state.preloadedRoutes.filter(r => r.name !== action.payload.name || id !== getId?.({
449
- params: r.params
450
- })).concat(createRouteFromAction({
451
- action,
452
- routeParamList
453
- }))
454
- };
455
- }
456
- }
457
- default:
458
- return BaseRouter.getStateForAction(state, action);
459
- }
460
- },
461
- actionCreators: StackActions
462
- };
463
- return router;
464
- }
465
- //# sourceMappingURL=StackRouter.js.map