@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,249 +0,0 @@
1
- import { nanoid } from 'nanoid/non-secure';
2
-
3
- import {
4
- type TabActionHelpers,
5
- TabActions,
6
- type TabActionType,
7
- type TabNavigationState,
8
- TabRouter,
9
- type TabRouterOptions,
10
- } from './TabRouter';
11
- import type {
12
- CommonNavigationAction,
13
- ParamListBase,
14
- PartialState,
15
- Router,
16
- } from './types';
17
- export type DrawerStatus = 'open' | 'closed';
18
-
19
- export type DrawerActionType =
20
- | TabActionType
21
- | {
22
- type: 'OPEN_DRAWER' | 'CLOSE_DRAWER' | 'TOGGLE_DRAWER';
23
- source?: string;
24
- target?: string;
25
- };
26
-
27
- export type DrawerRouterOptions = TabRouterOptions & {
28
- defaultStatus?: DrawerStatus;
29
- };
30
-
31
- export type DrawerNavigationState<ParamList extends ParamListBase> = Omit<
32
- TabNavigationState<ParamList>,
33
- 'type' | 'history'
34
- > & {
35
- /**
36
- * Type of the router, in this case, it's drawer.
37
- */
38
- type: 'drawer';
39
- /**
40
- * Default status of the drawer.
41
- */
42
- default: DrawerStatus;
43
- /**
44
- * List of previously visited route keys and drawer open status.
45
- */
46
- history: (
47
- | { type: 'route'; key: string }
48
- | { type: 'drawer'; status: DrawerStatus }
49
- )[];
50
- };
51
-
52
- export type DrawerActionHelpers<ParamList extends ParamListBase> =
53
- TabActionHelpers<ParamList> & {
54
- /**
55
- * Open the drawer sidebar.
56
- */
57
- openDrawer(): void;
58
-
59
- /**
60
- * Close the drawer sidebar.
61
- */
62
- closeDrawer(): void;
63
-
64
- /**
65
- * Open the drawer sidebar if closed, or close if opened.
66
- */
67
- toggleDrawer(): void;
68
- };
69
-
70
- export const DrawerActions = {
71
- ...TabActions,
72
- openDrawer() {
73
- return { type: 'OPEN_DRAWER' } as const satisfies DrawerActionType;
74
- },
75
- closeDrawer() {
76
- return { type: 'CLOSE_DRAWER' } as const satisfies DrawerActionType;
77
- },
78
- toggleDrawer() {
79
- return { type: 'TOGGLE_DRAWER' } as const satisfies DrawerActionType;
80
- },
81
- };
82
-
83
- export function DrawerRouter({
84
- defaultStatus = 'closed',
85
- ...rest
86
- }: DrawerRouterOptions): Router<
87
- DrawerNavigationState<ParamListBase>,
88
- DrawerActionType | CommonNavigationAction
89
- > {
90
- const router = TabRouter(rest) as unknown as Router<
91
- DrawerNavigationState<ParamListBase>,
92
- TabActionType | CommonNavigationAction
93
- >;
94
-
95
- const isDrawerInHistory = (
96
- state:
97
- | DrawerNavigationState<ParamListBase>
98
- | PartialState<DrawerNavigationState<ParamListBase>>
99
- ) => Boolean(state.history?.some((it) => it.type === 'drawer'));
100
-
101
- const addDrawerToHistory = (
102
- state: DrawerNavigationState<ParamListBase>
103
- ): DrawerNavigationState<ParamListBase> => {
104
- if (isDrawerInHistory(state)) {
105
- return state;
106
- }
107
-
108
- return {
109
- ...state,
110
- history: [
111
- ...state.history,
112
- {
113
- type: 'drawer',
114
- status: defaultStatus === 'open' ? 'closed' : 'open',
115
- },
116
- ],
117
- };
118
- };
119
-
120
- const removeDrawerFromHistory = (
121
- state: DrawerNavigationState<ParamListBase>
122
- ): DrawerNavigationState<ParamListBase> => {
123
- if (!isDrawerInHistory(state)) {
124
- return state;
125
- }
126
-
127
- return {
128
- ...state,
129
- history: state.history.filter((it) => it.type !== 'drawer'),
130
- };
131
- };
132
-
133
- const openDrawer = (
134
- state: DrawerNavigationState<ParamListBase>
135
- ): DrawerNavigationState<ParamListBase> => {
136
- if (defaultStatus === 'open') {
137
- return removeDrawerFromHistory(state);
138
- }
139
-
140
- return addDrawerToHistory(state);
141
- };
142
-
143
- const closeDrawer = (
144
- state: DrawerNavigationState<ParamListBase>
145
- ): DrawerNavigationState<ParamListBase> => {
146
- if (defaultStatus === 'open') {
147
- return addDrawerToHistory(state);
148
- }
149
-
150
- return removeDrawerFromHistory(state);
151
- };
152
-
153
- return {
154
- ...router,
155
-
156
- type: 'drawer',
157
-
158
- getInitialState({ routeNames, routeParamList, routeGetIdList }) {
159
- const state = router.getInitialState({
160
- routeNames,
161
- routeParamList,
162
- routeGetIdList,
163
- });
164
-
165
- return {
166
- ...state,
167
- default: defaultStatus,
168
- stale: false,
169
- type: 'drawer',
170
- key: `drawer-${nanoid()}`,
171
- };
172
- },
173
-
174
- getRehydratedState(
175
- partialState,
176
- { routeNames, routeParamList, routeGetIdList }
177
- ) {
178
- if (partialState.stale === false) {
179
- return partialState;
180
- }
181
-
182
- let state = router.getRehydratedState(partialState, {
183
- routeNames,
184
- routeParamList,
185
- routeGetIdList,
186
- });
187
-
188
- if (isDrawerInHistory(partialState)) {
189
- // Re-sync the drawer entry in history to correct it if it was wrong
190
- state = removeDrawerFromHistory(state);
191
- state = addDrawerToHistory(state);
192
- }
193
-
194
- return {
195
- ...state,
196
- default: defaultStatus,
197
- type: 'drawer',
198
- key: `drawer-${nanoid()}`,
199
- };
200
- },
201
-
202
- getStateForRouteFocus(state, key) {
203
- const result = router.getStateForRouteFocus(state, key);
204
-
205
- return closeDrawer(result);
206
- },
207
-
208
- getStateForAction(state, action, options) {
209
- switch (action.type) {
210
- case 'OPEN_DRAWER':
211
- return openDrawer(state);
212
-
213
- case 'CLOSE_DRAWER':
214
- return closeDrawer(state);
215
-
216
- case 'TOGGLE_DRAWER':
217
- if (isDrawerInHistory(state)) {
218
- return removeDrawerFromHistory(state);
219
- }
220
-
221
- return addDrawerToHistory(state);
222
-
223
- case 'JUMP_TO':
224
- case 'NAVIGATE':
225
- case 'NAVIGATE_DEPRECATED': {
226
- const result = router.getStateForAction(state, action, options);
227
-
228
- if (result != null && result.index !== state.index) {
229
- return closeDrawer(result as DrawerNavigationState<ParamListBase>);
230
- }
231
-
232
- return result;
233
- }
234
-
235
- case 'GO_BACK':
236
- if (isDrawerInHistory(state)) {
237
- return removeDrawerFromHistory(state);
238
- }
239
-
240
- return router.getStateForAction(state, action, options);
241
-
242
- default:
243
- return router.getStateForAction(state, action, options);
244
- }
245
- },
246
-
247
- actionCreators: DrawerActions,
248
- };
249
- }