@react-navigation/core 8.0.0-alpha.19 → 8.0.0-alpha.20

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 (144) hide show
  1. package/lib/module/BaseNavigationContainer.js +6 -2
  2. package/lib/module/BaseNavigationContainer.js.map +1 -1
  3. package/lib/module/StaticNavigation.js +72 -61
  4. package/lib/module/StaticNavigation.js.map +1 -1
  5. package/lib/module/findFocusedRoute.js +7 -4
  6. package/lib/module/findFocusedRoute.js.map +1 -1
  7. package/lib/module/getActionFromState.js +25 -12
  8. package/lib/module/getActionFromState.js.map +1 -1
  9. package/lib/module/getPathFromState.js +24 -9
  10. package/lib/module/getPathFromState.js.map +1 -1
  11. package/lib/module/getPatternParts.js +3 -0
  12. package/lib/module/getPatternParts.js.map +1 -1
  13. package/lib/module/getStateFromPath.js +164 -78
  14. package/lib/module/getStateFromPath.js.map +1 -1
  15. package/lib/module/index.js +1 -1
  16. package/lib/module/index.js.map +1 -1
  17. package/lib/module/types.js +8 -0
  18. package/lib/module/types.js.map +1 -1
  19. package/lib/module/useDescriptors.js +13 -6
  20. package/lib/module/useDescriptors.js.map +1 -1
  21. package/lib/module/useFocusEvents.js +5 -1
  22. package/lib/module/useFocusEvents.js.map +1 -1
  23. package/lib/module/useNavigation.js.map +1 -1
  24. package/lib/module/useNavigationBuilder.js +18 -5
  25. package/lib/module/useNavigationBuilder.js.map +1 -1
  26. package/lib/module/useNavigationCache.js +1 -1
  27. package/lib/module/useNavigationCache.js.map +1 -1
  28. package/lib/module/useNavigationState.js.map +1 -1
  29. package/lib/module/useOnAction.js +1 -1
  30. package/lib/module/useOnAction.js.map +1 -1
  31. package/lib/module/useRoute.js.map +1 -1
  32. package/lib/module/validatePathConfig.js +2 -1
  33. package/lib/module/validatePathConfig.js.map +1 -1
  34. package/lib/typescript/src/BaseNavigationContainer.d.ts +2 -2
  35. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  36. package/lib/typescript/src/EnsureSingleNavigator.d.ts +1 -1
  37. package/lib/typescript/src/EnsureSingleNavigator.d.ts.map +1 -1
  38. package/lib/typescript/src/Group.d.ts +1 -1
  39. package/lib/typescript/src/Group.d.ts.map +1 -1
  40. package/lib/typescript/src/NavigationBuilderContext.d.ts +1 -1
  41. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  42. package/lib/typescript/src/NavigationContainerRefContext.d.ts +1 -1
  43. package/lib/typescript/src/NavigationContainerRefContext.d.ts.map +1 -1
  44. package/lib/typescript/src/NavigationHelpersContext.d.ts +1 -1
  45. package/lib/typescript/src/NavigationHelpersContext.d.ts.map +1 -1
  46. package/lib/typescript/src/NavigationIndependentTree.d.ts +1 -1
  47. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  48. package/lib/typescript/src/NavigationProvider.d.ts +6 -6
  49. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  50. package/lib/typescript/src/PreventRemoveProvider.d.ts +1 -1
  51. package/lib/typescript/src/PreventRemoveProvider.d.ts.map +1 -1
  52. package/lib/typescript/src/SceneView.d.ts +3 -2
  53. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  54. package/lib/typescript/src/Screen.d.ts +1 -1
  55. package/lib/typescript/src/Screen.d.ts.map +1 -1
  56. package/lib/typescript/src/StaticNavigation.d.ts +68 -73
  57. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  58. package/lib/typescript/src/createNavigationContainerRef.d.ts +1 -1
  59. package/lib/typescript/src/createNavigationContainerRef.d.ts.map +1 -1
  60. package/lib/typescript/src/createNavigatorFactory.d.ts +3 -3
  61. package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
  62. package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
  63. package/lib/typescript/src/getActionFromState.d.ts +1 -1
  64. package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
  65. package/lib/typescript/src/getPathFromState.d.ts +1 -1
  66. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  67. package/lib/typescript/src/getPatternParts.d.ts +1 -0
  68. package/lib/typescript/src/getPatternParts.d.ts.map +1 -1
  69. package/lib/typescript/src/getStateFromPath.d.ts +2 -2
  70. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  71. package/lib/typescript/src/index.d.ts +34 -33
  72. package/lib/typescript/src/index.d.ts.map +1 -1
  73. package/lib/typescript/src/theming/ThemeContext.d.ts +1 -1
  74. package/lib/typescript/src/theming/ThemeContext.d.ts.map +1 -1
  75. package/lib/typescript/src/theming/ThemeProvider.d.ts +2 -2
  76. package/lib/typescript/src/theming/ThemeProvider.d.ts.map +1 -1
  77. package/lib/typescript/src/theming/useTheme.d.ts +1 -1
  78. package/lib/typescript/src/theming/useTheme.d.ts.map +1 -1
  79. package/lib/typescript/src/types.d.ts +72 -37
  80. package/lib/typescript/src/types.d.ts.map +1 -1
  81. package/lib/typescript/src/useChildListeners.d.ts +3 -3
  82. package/lib/typescript/src/useChildListeners.d.ts.map +1 -1
  83. package/lib/typescript/src/useComponent.d.ts +1 -1
  84. package/lib/typescript/src/useComponent.d.ts.map +1 -1
  85. package/lib/typescript/src/useDescriptors.d.ts +5 -5
  86. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  87. package/lib/typescript/src/useEventEmitter.d.ts +1 -1
  88. package/lib/typescript/src/useEventEmitter.d.ts.map +1 -1
  89. package/lib/typescript/src/useFocusEvents.d.ts +2 -2
  90. package/lib/typescript/src/useFocusEvents.d.ts.map +1 -1
  91. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +2 -2
  92. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts.map +1 -1
  93. package/lib/typescript/src/useKeyedChildListeners.d.ts +3 -3
  94. package/lib/typescript/src/useKeyedChildListeners.d.ts.map +1 -1
  95. package/lib/typescript/src/useNavigation.d.ts +5 -3
  96. package/lib/typescript/src/useNavigation.d.ts.map +1 -1
  97. package/lib/typescript/src/useNavigationBuilder.d.ts +7 -7
  98. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  99. package/lib/typescript/src/useNavigationCache.d.ts +3 -3
  100. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  101. package/lib/typescript/src/useNavigationContainerRef.d.ts +1 -1
  102. package/lib/typescript/src/useNavigationContainerRef.d.ts.map +1 -1
  103. package/lib/typescript/src/useNavigationHelpers.d.ts +4 -4
  104. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  105. package/lib/typescript/src/useNavigationState.d.ts +2 -2
  106. package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
  107. package/lib/typescript/src/useOnAction.d.ts +3 -3
  108. package/lib/typescript/src/useOnAction.d.ts.map +1 -1
  109. package/lib/typescript/src/useOnGetState.d.ts +1 -1
  110. package/lib/typescript/src/useOnGetState.d.ts.map +1 -1
  111. package/lib/typescript/src/useOnPreventRemove.d.ts +3 -3
  112. package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
  113. package/lib/typescript/src/useOptionsGetters.d.ts +1 -1
  114. package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
  115. package/lib/typescript/src/usePreventRemoveContext.d.ts +1 -1
  116. package/lib/typescript/src/useRoute.d.ts +2 -2
  117. package/lib/typescript/src/useRoute.d.ts.map +1 -1
  118. package/lib/typescript/src/useRouteCache.d.ts +2 -2
  119. package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
  120. package/lib/typescript/src/useStateForPath.d.ts +1 -1
  121. package/lib/typescript/src/useStateForPath.d.ts.map +1 -1
  122. package/lib/typescript/src/utilities.d.ts +4 -3
  123. package/lib/typescript/src/utilities.d.ts.map +1 -1
  124. package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
  125. package/package.json +18 -17
  126. package/src/BaseNavigationContainer.tsx +7 -2
  127. package/src/StaticNavigation.tsx +220 -248
  128. package/src/findFocusedRoute.tsx +10 -5
  129. package/src/getActionFromState.tsx +43 -30
  130. package/src/getPathFromState.tsx +40 -20
  131. package/src/getPatternParts.tsx +8 -0
  132. package/src/getStateFromPath.tsx +244 -67
  133. package/src/index.tsx +39 -1
  134. package/src/types.tsx +167 -122
  135. package/src/useDescriptors.tsx +22 -10
  136. package/src/useFocusEvents.tsx +7 -1
  137. package/src/useNavigation.tsx +8 -4
  138. package/src/useNavigationBuilder.tsx +22 -7
  139. package/src/useNavigationCache.tsx +4 -4
  140. package/src/useNavigationState.tsx +3 -3
  141. package/src/useOnAction.tsx +1 -1
  142. package/src/useRoute.tsx +4 -2
  143. package/src/utilities.tsx +17 -11
  144. package/src/validatePathConfig.tsx +1 -0
@@ -12,11 +12,16 @@ type Result =
12
12
  export function findFocusedRoute(state: InitialState): Result {
13
13
  let current: InitialState | undefined = state;
14
14
 
15
- while (current?.routes[current.index ?? 0].state != null) {
16
- current = current.routes[current.index ?? 0].state;
17
- }
15
+ while (current != null) {
16
+ const route: InitialState['routes'][number] | undefined =
17
+ current.routes[current.index ?? 0];
18
+
19
+ if (route?.state == null) {
20
+ return route;
21
+ }
18
22
 
19
- const route = current?.routes[current?.index ?? 0];
23
+ current = route.state;
24
+ }
20
25
 
21
- return route;
26
+ return undefined;
22
27
  }
@@ -2,9 +2,7 @@ import type {
2
2
  CommonActions,
3
3
  NavigationState,
4
4
  ParamListBase,
5
- PartialRoute,
6
5
  PartialState,
7
- Route,
8
6
  } from '@react-navigation/routers';
9
7
 
10
8
  import type { NavigatorScreenParams, PathConfig, PathConfigMap } from './types';
@@ -60,13 +58,21 @@ export function getActionFromState(
60
58
  return undefined;
61
59
  }
62
60
 
61
+ const firstRoute = routes[0];
62
+
63
+ if (firstRoute == null) {
64
+ return undefined;
65
+ }
66
+
67
+ const secondRoute = routes[1];
68
+
63
69
  if (
64
70
  !(
65
- (routes.length === 1 && routes[0].key === undefined) ||
71
+ (routes.length === 1 && firstRoute.key == null) ||
66
72
  (routes.length === 2 &&
67
- routes[0].key === undefined &&
68
- routes[0].name === normalizedConfig?.initialRouteName &&
69
- routes[1].key === undefined)
73
+ firstRoute.key == null &&
74
+ firstRoute.name === normalizedConfig?.initialRouteName &&
75
+ secondRoute?.key == null)
70
76
  )
71
77
  ) {
72
78
  return {
@@ -77,20 +83,20 @@ export function getActionFromState(
77
83
 
78
84
  const route = state.routes[state.index ?? state.routes.length - 1];
79
85
 
80
- let current: PartialState<NavigationState> | undefined = route?.state;
81
- let config: ConfigItem | undefined = normalizedConfig?.screens?.[route?.name];
86
+ if (route == null) {
87
+ return undefined;
88
+ }
89
+
90
+ let current: PartialState<NavigationState> | undefined = route.state;
91
+ let config: ConfigItem | undefined = normalizedConfig?.screens?.[route.name];
82
92
  let params = { ...route.params } as NavigatorScreenParams<ParamListBase>;
83
93
 
84
- const payload:
85
- | {
86
- name: string;
87
- params: NavigatorScreenParams<ParamListBase>;
88
- path?: string | undefined;
89
- pop?: boolean | undefined;
90
- }
91
- | undefined = route
92
- ? { name: route.name, path: route.path, params }
93
- : undefined;
94
+ const payload: {
95
+ name: string;
96
+ params: NavigatorScreenParams<ParamListBase>;
97
+ path?: string | undefined;
98
+ pop?: boolean | undefined;
99
+ } = { name: route.name, path: route.path, params };
94
100
 
95
101
  // If the screen contains a navigator, pop other screens to navigate to it
96
102
  // This avoid pushing multiple instances of navigators onto a stack
@@ -106,7 +112,7 @@ export function getActionFromState(
106
112
  // There are 2 ways we can detect if a screen contains a navigator:
107
113
  // - The route contains nested state in `route.state`
108
114
  // - Nested screens are defined in the config
109
- if (payload && config?.screens && Object.keys(config.screens).length) {
115
+ if (config?.screens && Object.keys(config.screens).length) {
110
116
  payload.pop = true;
111
117
  }
112
118
 
@@ -120,8 +126,19 @@ export function getActionFromState(
120
126
  ? current.routes.slice(0, current.index + 1)
121
127
  : current.routes;
122
128
 
123
- const route: Route<string> | PartialRoute<Route<string>> =
124
- routes[routes.length - 1];
129
+ const route = routes[routes.length - 1];
130
+
131
+ if (route == null) {
132
+ return undefined;
133
+ }
134
+
135
+ const firstRoute = routes[0];
136
+
137
+ if (firstRoute == null) {
138
+ return undefined;
139
+ }
140
+
141
+ const secondRoute = routes[1];
125
142
 
126
143
  // Explicitly set to override existing value when merging params
127
144
  Object.assign(params, {
@@ -131,14 +148,14 @@ export function getActionFromState(
131
148
  state: undefined,
132
149
  });
133
150
 
134
- if (routes.length === 1 && routes[0].key === undefined) {
151
+ if (routes.length === 1 && firstRoute.key == null) {
135
152
  params.initial = true;
136
153
  params.screen = route.name;
137
154
  } else if (
138
155
  routes.length === 2 &&
139
- routes[0].key === undefined &&
140
- routes[0].name === config?.initialRouteName &&
141
- routes[1].key === undefined
156
+ firstRoute.key == null &&
157
+ firstRoute.name === config?.initialRouteName &&
158
+ secondRoute?.key == null
142
159
  ) {
143
160
  params.initial = false;
144
161
  params.screen = route.name;
@@ -164,14 +181,10 @@ export function getActionFromState(
164
181
  }
165
182
  }
166
183
 
167
- if (payload?.params.screen || payload?.params.state) {
184
+ if (payload.params.screen || payload.params.state) {
168
185
  payload.pop = true;
169
186
  }
170
187
 
171
- if (!payload) {
172
- return;
173
- }
174
-
175
188
  // Try to construct payload for a `NAVIGATE` action from the state
176
189
  // This lets us preserve the navigation state and not lose it
177
190
  return {
@@ -1,8 +1,4 @@
1
- import type {
2
- NavigationState,
3
- PartialState,
4
- Route,
5
- } from '@react-navigation/routers';
1
+ import type { NavigationState, PartialState } from '@react-navigation/routers';
6
2
  import queryString from 'query-string';
7
3
 
8
4
  import { getPatternParts, type PatternPart } from './getPatternParts';
@@ -34,6 +30,10 @@ const getActiveRoute = (
34
30
  ? state.routes[state.index]
35
31
  : state.routes[state.routes.length - 1];
36
32
 
33
+ if (route == null) {
34
+ throw new Error(`Couldn't find the active route.`);
35
+ }
36
+
37
37
  if (route.state) {
38
38
  return getActiveRoute(route.state);
39
39
  }
@@ -66,13 +66,15 @@ const getTransformedState = (
66
66
  ): Omit<PartialState<NavigationState>, 'stale'> => {
67
67
  const routes = state.routes.map(
68
68
  (route): Omit<PartialState<NavigationState>, 'stale'>['routes'][number] => {
69
+ const config = configs?.[route.name];
70
+
69
71
  if (
70
72
  route.state ||
71
- (configs?.[route.name]?.screens &&
73
+ (config?.screens &&
72
74
  route.params &&
73
75
  (('screen' in route.params &&
74
76
  typeof route.params.screen === 'string' &&
75
- configs[route.name].screens?.[route.params.screen]) ||
77
+ config.screens[route.params.screen]) ||
76
78
  'state' in route.params))
77
79
  ) {
78
80
  const nestedState: State | undefined =
@@ -152,10 +154,16 @@ export function getPathFromState<ParamList extends {}>(
152
154
  const allParams: Record<string, string> = {};
153
155
 
154
156
  while (current) {
155
- let index = typeof current.index === 'number' ? current.index : 0;
156
- let route = current.routes[index] as Route<string> & {
157
- state?: State;
158
- };
157
+ let index: number = typeof current.index === 'number' ? current.index : 0;
158
+
159
+ const initialRoute: State['routes'][number] | undefined =
160
+ current.routes[index];
161
+
162
+ if (initialRoute == null) {
163
+ throw new Error(`Couldn't find a route at index ${index}.`);
164
+ }
165
+
166
+ let route: State['routes'][number] = initialRoute;
159
167
 
160
168
  let parts: PatternPart[] | undefined;
161
169
 
@@ -169,13 +177,19 @@ export function getPathFromState<ParamList extends {}>(
169
177
 
170
178
  let hasNext = true;
171
179
 
172
- while (route.name in currentOptions && hasNext) {
173
- parts = currentOptions[route.name].parts;
180
+ while (hasNext) {
181
+ const config = currentOptions[route.name];
182
+
183
+ if (config == null) {
184
+ break;
185
+ }
186
+
187
+ parts = config.parts;
174
188
 
175
189
  nestedRouteNames.push(route.name);
176
190
 
177
191
  if (route.params) {
178
- const options = currentOptions[route.name];
192
+ const options = config;
179
193
 
180
194
  const currentParams = Object.fromEntries(
181
195
  Object.entries(route.params)
@@ -225,7 +239,7 @@ export function getPathFromState<ParamList extends {}>(
225
239
  }
226
240
 
227
241
  // If there is no `screens` property or no nested state, we return pattern
228
- if (!currentOptions[route.name].screens || route.state === undefined) {
242
+ if (!config.screens || route.state == null) {
229
243
  hasNext = false;
230
244
  } else {
231
245
  index =
@@ -233,12 +247,18 @@ export function getPathFromState<ParamList extends {}>(
233
247
  ? route.state.index
234
248
  : route.state.routes.length - 1;
235
249
 
236
- const nextRoute = route.state.routes[index];
237
- const nestedConfig = currentOptions[route.name].screens;
250
+ const nextRoute: State['routes'][number] | undefined =
251
+ route.state.routes[index];
252
+
253
+ if (nextRoute == null) {
254
+ throw new Error(`Couldn't find a route at index ${index}.`);
255
+ }
256
+
257
+ const nestedConfig = config.screens;
238
258
 
239
259
  // if there is config for next route name, we go deeper
240
- if (nestedConfig && nextRoute.name in nestedConfig) {
241
- route = nextRoute as Route<string> & { state?: State };
260
+ if (nextRoute.name in nestedConfig) {
261
+ route = nextRoute;
242
262
  currentOptions = nestedConfig;
243
263
  } else {
244
264
  // If not, there is no sense in going deeper in config
@@ -247,7 +267,7 @@ export function getPathFromState<ParamList extends {}>(
247
267
  }
248
268
  }
249
269
 
250
- if (currentOptions[route.name] !== undefined) {
270
+ if (currentOptions[route.name] != null) {
251
271
  path += parts
252
272
  ?.map(({ segment, param, optional }) => {
253
273
  // We don't know what to show for wildcard patterns
@@ -124,3 +124,11 @@ export function getPatternParts(path: string): PatternPart[] {
124
124
 
125
125
  return parts;
126
126
  }
127
+
128
+ export function combinePatternParts<T extends PatternPart>(
129
+ parentParts: T[],
130
+ parts: T[],
131
+ exact = false
132
+ ): T[] {
133
+ return exact ? parts : [...parentParts, ...parts];
134
+ }