@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.
- package/lib/module/BaseNavigationContainer.js +6 -2
- package/lib/module/BaseNavigationContainer.js.map +1 -1
- package/lib/module/StaticNavigation.js +72 -61
- package/lib/module/StaticNavigation.js.map +1 -1
- package/lib/module/findFocusedRoute.js +7 -4
- package/lib/module/findFocusedRoute.js.map +1 -1
- package/lib/module/getActionFromState.js +25 -12
- package/lib/module/getActionFromState.js.map +1 -1
- package/lib/module/getPathFromState.js +24 -9
- package/lib/module/getPathFromState.js.map +1 -1
- package/lib/module/getPatternParts.js +3 -0
- package/lib/module/getPatternParts.js.map +1 -1
- package/lib/module/getStateFromPath.js +164 -78
- package/lib/module/getStateFromPath.js.map +1 -1
- package/lib/module/index.js +1 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/types.js +8 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/useDescriptors.js +13 -6
- package/lib/module/useDescriptors.js.map +1 -1
- package/lib/module/useFocusEvents.js +5 -1
- package/lib/module/useFocusEvents.js.map +1 -1
- package/lib/module/useNavigation.js.map +1 -1
- package/lib/module/useNavigationBuilder.js +18 -5
- package/lib/module/useNavigationBuilder.js.map +1 -1
- package/lib/module/useNavigationCache.js +1 -1
- package/lib/module/useNavigationCache.js.map +1 -1
- package/lib/module/useNavigationState.js.map +1 -1
- package/lib/module/useOnAction.js +1 -1
- package/lib/module/useOnAction.js.map +1 -1
- package/lib/module/useRoute.js.map +1 -1
- package/lib/module/validatePathConfig.js +2 -1
- package/lib/module/validatePathConfig.js.map +1 -1
- package/lib/typescript/src/BaseNavigationContainer.d.ts +2 -2
- package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
- package/lib/typescript/src/EnsureSingleNavigator.d.ts +1 -1
- package/lib/typescript/src/EnsureSingleNavigator.d.ts.map +1 -1
- package/lib/typescript/src/Group.d.ts +1 -1
- package/lib/typescript/src/Group.d.ts.map +1 -1
- package/lib/typescript/src/NavigationBuilderContext.d.ts +1 -1
- package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
- package/lib/typescript/src/NavigationContainerRefContext.d.ts +1 -1
- package/lib/typescript/src/NavigationContainerRefContext.d.ts.map +1 -1
- package/lib/typescript/src/NavigationHelpersContext.d.ts +1 -1
- package/lib/typescript/src/NavigationHelpersContext.d.ts.map +1 -1
- package/lib/typescript/src/NavigationIndependentTree.d.ts +1 -1
- package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
- package/lib/typescript/src/NavigationProvider.d.ts +6 -6
- package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
- package/lib/typescript/src/PreventRemoveProvider.d.ts +1 -1
- package/lib/typescript/src/PreventRemoveProvider.d.ts.map +1 -1
- package/lib/typescript/src/SceneView.d.ts +3 -2
- package/lib/typescript/src/SceneView.d.ts.map +1 -1
- package/lib/typescript/src/Screen.d.ts +1 -1
- package/lib/typescript/src/Screen.d.ts.map +1 -1
- package/lib/typescript/src/StaticNavigation.d.ts +68 -73
- package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
- package/lib/typescript/src/createNavigationContainerRef.d.ts +1 -1
- package/lib/typescript/src/createNavigationContainerRef.d.ts.map +1 -1
- package/lib/typescript/src/createNavigatorFactory.d.ts +3 -3
- package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
- package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
- package/lib/typescript/src/getActionFromState.d.ts +1 -1
- package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
- package/lib/typescript/src/getPathFromState.d.ts +1 -1
- package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
- package/lib/typescript/src/getPatternParts.d.ts +1 -0
- package/lib/typescript/src/getPatternParts.d.ts.map +1 -1
- package/lib/typescript/src/getStateFromPath.d.ts +2 -2
- package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +34 -33
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/theming/ThemeContext.d.ts +1 -1
- package/lib/typescript/src/theming/ThemeContext.d.ts.map +1 -1
- package/lib/typescript/src/theming/ThemeProvider.d.ts +2 -2
- package/lib/typescript/src/theming/ThemeProvider.d.ts.map +1 -1
- package/lib/typescript/src/theming/useTheme.d.ts +1 -1
- package/lib/typescript/src/theming/useTheme.d.ts.map +1 -1
- package/lib/typescript/src/types.d.ts +72 -37
- package/lib/typescript/src/types.d.ts.map +1 -1
- package/lib/typescript/src/useChildListeners.d.ts +3 -3
- package/lib/typescript/src/useChildListeners.d.ts.map +1 -1
- package/lib/typescript/src/useComponent.d.ts +1 -1
- package/lib/typescript/src/useComponent.d.ts.map +1 -1
- package/lib/typescript/src/useDescriptors.d.ts +5 -5
- package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
- package/lib/typescript/src/useEventEmitter.d.ts +1 -1
- package/lib/typescript/src/useEventEmitter.d.ts.map +1 -1
- package/lib/typescript/src/useFocusEvents.d.ts +2 -2
- package/lib/typescript/src/useFocusEvents.d.ts.map +1 -1
- package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +2 -2
- package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts.map +1 -1
- package/lib/typescript/src/useKeyedChildListeners.d.ts +3 -3
- package/lib/typescript/src/useKeyedChildListeners.d.ts.map +1 -1
- package/lib/typescript/src/useNavigation.d.ts +5 -3
- package/lib/typescript/src/useNavigation.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationBuilder.d.ts +7 -7
- package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationCache.d.ts +3 -3
- package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationContainerRef.d.ts +1 -1
- package/lib/typescript/src/useNavigationContainerRef.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationHelpers.d.ts +4 -4
- package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
- package/lib/typescript/src/useNavigationState.d.ts +2 -2
- package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
- package/lib/typescript/src/useOnAction.d.ts +3 -3
- package/lib/typescript/src/useOnAction.d.ts.map +1 -1
- package/lib/typescript/src/useOnGetState.d.ts +1 -1
- package/lib/typescript/src/useOnGetState.d.ts.map +1 -1
- package/lib/typescript/src/useOnPreventRemove.d.ts +3 -3
- package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
- package/lib/typescript/src/useOptionsGetters.d.ts +1 -1
- package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
- package/lib/typescript/src/usePreventRemoveContext.d.ts +1 -1
- package/lib/typescript/src/useRoute.d.ts +2 -2
- package/lib/typescript/src/useRoute.d.ts.map +1 -1
- package/lib/typescript/src/useRouteCache.d.ts +2 -2
- package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
- package/lib/typescript/src/useStateForPath.d.ts +1 -1
- package/lib/typescript/src/useStateForPath.d.ts.map +1 -1
- package/lib/typescript/src/utilities.d.ts +4 -3
- package/lib/typescript/src/utilities.d.ts.map +1 -1
- package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
- package/package.json +18 -17
- package/src/BaseNavigationContainer.tsx +7 -2
- package/src/StaticNavigation.tsx +220 -248
- package/src/findFocusedRoute.tsx +10 -5
- package/src/getActionFromState.tsx +43 -30
- package/src/getPathFromState.tsx +40 -20
- package/src/getPatternParts.tsx +8 -0
- package/src/getStateFromPath.tsx +244 -67
- package/src/index.tsx +39 -1
- package/src/types.tsx +167 -122
- package/src/useDescriptors.tsx +22 -10
- package/src/useFocusEvents.tsx +7 -1
- package/src/useNavigation.tsx +8 -4
- package/src/useNavigationBuilder.tsx +22 -7
- package/src/useNavigationCache.tsx +4 -4
- package/src/useNavigationState.tsx +3 -3
- package/src/useOnAction.tsx +1 -1
- package/src/useRoute.tsx +4 -2
- package/src/utilities.tsx +17 -11
- package/src/validatePathConfig.tsx +1 -0
package/src/findFocusedRoute.tsx
CHANGED
|
@@ -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
|
|
16
|
-
|
|
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
|
-
|
|
23
|
+
current = route.state;
|
|
24
|
+
}
|
|
20
25
|
|
|
21
|
-
return
|
|
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 &&
|
|
71
|
+
(routes.length === 1 && firstRoute.key == null) ||
|
|
66
72
|
(routes.length === 2 &&
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
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
|
-
|
|
81
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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 (
|
|
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
|
|
124
|
-
|
|
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 &&
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
|
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 {
|
package/src/getPathFromState.tsx
CHANGED
|
@@ -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
|
-
(
|
|
73
|
+
(config?.screens &&
|
|
72
74
|
route.params &&
|
|
73
75
|
(('screen' in route.params &&
|
|
74
76
|
typeof route.params.screen === 'string' &&
|
|
75
|
-
|
|
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
|
-
|
|
157
|
-
|
|
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 (
|
|
173
|
-
|
|
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 =
|
|
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 (!
|
|
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
|
|
237
|
-
|
|
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 (
|
|
241
|
-
route = nextRoute
|
|
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]
|
|
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
|
package/src/getPatternParts.tsx
CHANGED
|
@@ -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
|
+
}
|