@react-navigation/core 8.0.0-alpha.2 → 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 +20 -9
- package/lib/module/BaseNavigationContainer.js.map +1 -1
- package/lib/module/ConsumedParamsContext.js +5 -0
- package/lib/module/ConsumedParamsContext.js.map +1 -0
- package/lib/module/NavigationBuilderContext.js +1 -0
- package/lib/module/NavigationBuilderContext.js.map +1 -1
- package/lib/module/NavigationIndependentTree.js +8 -4
- package/lib/module/NavigationIndependentTree.js.map +1 -1
- package/lib/module/NavigationProvider.js +14 -3
- package/lib/module/NavigationProvider.js.map +1 -1
- package/lib/module/NavigationStateContext.js.map +1 -1
- package/lib/module/PreventRemoveProvider.js +3 -3
- package/lib/module/PreventRemoveProvider.js.map +1 -1
- package/lib/module/SceneView.js +46 -56
- package/lib/module/SceneView.js.map +1 -1
- package/lib/module/StaticNavigation.js +166 -46
- package/lib/module/StaticNavigation.js.map +1 -1
- package/lib/module/checkSerializable.js +11 -4
- package/lib/module/checkSerializable.js.map +1 -1
- package/lib/module/createNavigatorFactory.js +27 -5
- package/lib/module/createNavigatorFactory.js.map +1 -1
- package/lib/module/findFocusedRoute.js +7 -4
- package/lib/module/findFocusedRoute.js.map +1 -1
- package/lib/module/getActionFromState.js +40 -14
- package/lib/module/getActionFromState.js.map +1 -1
- package/lib/module/getPathFromState.js +48 -11
- 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 +353 -166
- package/lib/module/getStateFromPath.js.map +1 -1
- package/lib/module/getStateFromRouteParams.js +24 -0
- package/lib/module/getStateFromRouteParams.js.map +1 -0
- package/lib/module/index.js +3 -4
- package/lib/module/index.js.map +1 -1
- package/lib/module/theming/useTheme.js +1 -1
- package/lib/module/theming/useTheme.js.map +1 -1
- package/lib/module/types.js +15 -0
- package/lib/module/types.js.map +1 -1
- package/lib/module/useDescriptors.js +18 -9
- package/lib/module/useDescriptors.js.map +1 -1
- package/lib/module/useEventEmitter.js +43 -31
- package/lib/module/useEventEmitter.js.map +1 -1
- package/lib/module/useFocusEvents.js +6 -2
- package/lib/module/useFocusEvents.js.map +1 -1
- package/lib/module/useFocusedListenersChildrenAdapter.js +2 -2
- package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
- package/lib/module/useIsFocused.js +7 -12
- package/lib/module/useIsFocused.js.map +1 -1
- package/lib/module/useNavigation.js.map +1 -1
- package/lib/module/useNavigationBuilder.js +144 -86
- package/lib/module/useNavigationBuilder.js.map +1 -1
- package/lib/module/useNavigationCache.js +3 -3
- package/lib/module/useNavigationCache.js.map +1 -1
- package/lib/module/useNavigationHelpers.js +1 -1
- package/lib/module/useNavigationHelpers.js.map +1 -1
- package/lib/module/useNavigationIndependentTree.js +1 -1
- package/lib/module/useNavigationIndependentTree.js.map +1 -1
- package/lib/module/useNavigationState.js +6 -4
- package/lib/module/useNavigationState.js.map +1 -1
- package/lib/module/useOnAction.js +3 -3
- package/lib/module/useOnAction.js.map +1 -1
- package/lib/module/useOnGetState.js +6 -3
- package/lib/module/useOnGetState.js.map +1 -1
- package/lib/module/useOnPreventRemove.js +3 -3
- package/lib/module/useOnPreventRemove.js.map +1 -1
- package/lib/module/useOnRouteFocus.js +1 -1
- package/lib/module/useOnRouteFocus.js.map +1 -1
- package/lib/module/useOptionsGetters.js +7 -4
- package/lib/module/useOptionsGetters.js.map +1 -1
- package/lib/module/usePreventRemove.js +1 -1
- package/lib/module/usePreventRemove.js.map +1 -1
- package/lib/module/usePreventRemoveContext.js +1 -1
- package/lib/module/usePreventRemoveContext.js.map +1 -1
- package/lib/module/useRegisterNavigator.js +2 -2
- package/lib/module/useRegisterNavigator.js.map +1 -1
- package/lib/module/useRoute.js.map +1 -1
- package/lib/module/useRouteCache.js +2 -1
- package/lib/module/useRouteCache.js.map +1 -1
- package/lib/module/useScheduleUpdate.js +1 -1
- package/lib/module/useScheduleUpdate.js.map +1 -1
- package/lib/module/useStateForPath.js +1 -1
- package/lib/module/useStateForPath.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 +6 -3
- package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
- package/lib/typescript/src/ConsumedParamsContext.d.ts +8 -0
- package/lib/typescript/src/ConsumedParamsContext.d.ts.map +1 -0
- 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 +12 -6
- 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/NavigationFocusedRouteStateContext.d.ts +4 -4
- package/lib/typescript/src/NavigationFocusedRouteStateContext.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 +10 -10
- package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
- package/lib/typescript/src/NavigationStateContext.d.ts +3 -3
- package/lib/typescript/src/NavigationStateContext.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/StaticContainer.d.ts +1 -1
- package/lib/typescript/src/StaticContainer.d.ts.map +1 -1
- package/lib/typescript/src/StaticNavigation.d.ts +123 -92
- package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
- package/lib/typescript/src/checkSerializable.d.ts +5 -3
- package/lib/typescript/src/checkSerializable.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 +17 -5
- package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
- package/lib/typescript/src/findFocusedRoute.d.ts +3 -3
- package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
- package/lib/typescript/src/getActionFromState.d.ts +4 -4
- package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
- package/lib/typescript/src/getPathFromState.d.ts +3 -3
- 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 +5 -5
- package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
- package/lib/typescript/src/getStateFromRouteParams.d.ts +3 -0
- package/lib/typescript/src/getStateFromRouteParams.d.ts.map +1 -0
- 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 +215 -131
- 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 +7 -7
- 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/useIsFocused.d.ts +3 -0
- package/lib/typescript/src/useIsFocused.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 +24 -24
- 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 +19 -19
- 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 +9 -9
- 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/useOnRouteFocus.d.ts +6 -6
- package/lib/typescript/src/useOnRouteFocus.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 +4 -4
- package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
- package/lib/typescript/src/useScheduleUpdate.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 +45 -5
- package/lib/typescript/src/utilities.d.ts.map +1 -1
- package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
- package/package.json +23 -20
- package/src/BaseNavigationContainer.tsx +337 -326
- package/src/ConsumedParamsContext.tsx +10 -0
- package/src/NavigationBuilderContext.tsx +14 -8
- package/src/NavigationFocusedRouteStateContext.tsx +4 -4
- package/src/NavigationIndependentTree.tsx +8 -5
- package/src/NavigationProvider.tsx +17 -3
- package/src/NavigationStateContext.tsx +5 -6
- package/src/PreventRemoveProvider.tsx +3 -3
- package/src/SceneView.tsx +58 -56
- package/src/StaticNavigation.tsx +488 -250
- package/src/checkSerializable.tsx +22 -12
- package/src/createNavigatorFactory.tsx +83 -8
- package/src/findFocusedRoute.tsx +13 -8
- package/src/getActionFromState.tsx +68 -39
- package/src/getPathFromState.tsx +91 -27
- package/src/getPatternParts.tsx +8 -0
- package/src/getStateFromPath.tsx +555 -180
- package/src/getStateFromRouteParams.tsx +60 -0
- package/src/index.tsx +47 -5
- package/src/theming/useTheme.tsx +1 -1
- package/src/types.tsx +541 -338
- package/src/useDescriptors.tsx +27 -12
- package/src/useEventEmitter.tsx +45 -36
- package/src/useFocusEvents.tsx +8 -2
- package/src/useFocusedListenersChildrenAdapter.tsx +2 -2
- package/src/useIsFocused.tsx +14 -21
- package/src/useNavigation.tsx +8 -4
- package/src/useNavigationBuilder.tsx +220 -132
- package/src/useNavigationCache.tsx +6 -6
- package/src/useNavigationHelpers.tsx +1 -1
- package/src/useNavigationIndependentTree.tsx +1 -1
- package/src/useNavigationState.tsx +11 -7
- package/src/useOnAction.tsx +10 -10
- package/src/useOnGetState.tsx +5 -3
- package/src/useOnPreventRemove.tsx +3 -3
- package/src/useOnRouteFocus.tsx +10 -12
- package/src/useOptionsGetters.tsx +7 -4
- package/src/usePreventRemove.tsx +1 -1
- package/src/usePreventRemoveContext.tsx +1 -1
- package/src/useRegisterNavigator.tsx +2 -2
- package/src/useRoute.tsx +4 -2
- package/src/useRouteCache.tsx +2 -1
- package/src/useScheduleUpdate.tsx +1 -3
- package/src/useStateForPath.tsx +1 -1
- package/src/utilities.tsx +101 -5
- package/src/validatePathConfig.tsx +1 -0
- package/lib/module/CurrentRenderContext.js +0 -10
- package/lib/module/CurrentRenderContext.js.map +0 -1
- package/lib/module/useCurrentRender.js +0 -19
- package/lib/module/useCurrentRender.js.map +0 -1
- package/lib/typescript/src/CurrentRenderContext.d.ts +0 -9
- package/lib/typescript/src/CurrentRenderContext.d.ts.map +0 -1
- package/lib/typescript/src/useCurrentRender.d.ts +0 -14
- package/lib/typescript/src/useCurrentRender.d.ts.map +0 -1
- package/src/CurrentRenderContext.tsx +0 -9
- package/src/useCurrentRender.tsx +0 -31
package/src/useDescriptors.tsx
CHANGED
|
@@ -113,17 +113,18 @@ export function useDescriptors<
|
|
|
113
113
|
router,
|
|
114
114
|
emitter,
|
|
115
115
|
}: Options<State, ScreenOptions, EventMap>) {
|
|
116
|
-
const theme = React.
|
|
117
|
-
const [options, setOptions] = React.useState<
|
|
118
|
-
|
|
119
|
-
);
|
|
116
|
+
const theme = React.use(ThemeContext);
|
|
117
|
+
const [options, setOptions] = React.useState<
|
|
118
|
+
Record<string, Partial<ScreenOptions> | undefined>
|
|
119
|
+
>({});
|
|
120
120
|
const {
|
|
121
121
|
onDispatchAction,
|
|
122
|
+
onEmitEvent,
|
|
122
123
|
onOptionsChange,
|
|
123
124
|
scheduleUpdate,
|
|
124
125
|
flushUpdates,
|
|
125
126
|
stackRef,
|
|
126
|
-
} = React.
|
|
127
|
+
} = React.use(NavigationBuilderContext);
|
|
127
128
|
|
|
128
129
|
const context = React.useMemo(
|
|
129
130
|
() => ({
|
|
@@ -133,6 +134,7 @@ export function useDescriptors<
|
|
|
133
134
|
addKeyedListener,
|
|
134
135
|
onRouteFocus,
|
|
135
136
|
onDispatchAction,
|
|
137
|
+
onEmitEvent,
|
|
136
138
|
onOptionsChange,
|
|
137
139
|
scheduleUpdate,
|
|
138
140
|
flushUpdates,
|
|
@@ -145,6 +147,7 @@ export function useDescriptors<
|
|
|
145
147
|
addKeyedListener,
|
|
146
148
|
onRouteFocus,
|
|
147
149
|
onDispatchAction,
|
|
150
|
+
onEmitEvent,
|
|
148
151
|
onOptionsChange,
|
|
149
152
|
scheduleUpdate,
|
|
150
153
|
flushUpdates,
|
|
@@ -177,20 +180,20 @@ export function useDescriptors<
|
|
|
177
180
|
ScreenOptions,
|
|
178
181
|
EventMap
|
|
179
182
|
>,
|
|
180
|
-
overrides:
|
|
183
|
+
overrides: Partial<ScreenOptions> | undefined
|
|
181
184
|
) => {
|
|
182
185
|
const config = screens[route.name];
|
|
183
|
-
const screen = config
|
|
186
|
+
const screen = config?.props;
|
|
184
187
|
|
|
185
188
|
const optionsList = [
|
|
186
189
|
// The default `screenOptions` passed to the navigator
|
|
187
190
|
screenOptions,
|
|
188
191
|
// The `screenOptions` props passed to `Group` elements
|
|
189
|
-
...((config
|
|
192
|
+
...((config?.options
|
|
190
193
|
? config.options.filter(Boolean)
|
|
191
194
|
: []) as ScreenOptionsOrCallback<ScreenOptions>[]),
|
|
192
195
|
// The `options` prop passed to `Screen` elements,
|
|
193
|
-
screen
|
|
196
|
+
screen?.options,
|
|
194
197
|
// The options set via `navigation.setOptions`
|
|
195
198
|
overrides,
|
|
196
199
|
];
|
|
@@ -219,7 +222,11 @@ export function useDescriptors<
|
|
|
219
222
|
routeState: NavigationState | PartialState<NavigationState> | undefined
|
|
220
223
|
) => {
|
|
221
224
|
const config = screens[route.name];
|
|
222
|
-
const screen = config
|
|
225
|
+
const screen = config?.props;
|
|
226
|
+
|
|
227
|
+
if (screen == null) {
|
|
228
|
+
throw new Error(`Couldn't find a screen named '${route.name}'.`);
|
|
229
|
+
}
|
|
223
230
|
|
|
224
231
|
const clearOptions = () =>
|
|
225
232
|
setOptions((o) => {
|
|
@@ -236,7 +243,7 @@ export function useDescriptors<
|
|
|
236
243
|
// The `layout` prop passed to `Screen` elements,
|
|
237
244
|
screen.layout ??
|
|
238
245
|
// The `screenLayout` props passed to `Group` elements
|
|
239
|
-
config
|
|
246
|
+
config?.layout ??
|
|
240
247
|
// The default `screenLayout` passed to the navigator
|
|
241
248
|
screenLayout;
|
|
242
249
|
|
|
@@ -291,7 +298,15 @@ export function useDescriptors<
|
|
|
291
298
|
>
|
|
292
299
|
>((acc, route, i) => {
|
|
293
300
|
const navigation = navigations[route.key];
|
|
294
|
-
const
|
|
301
|
+
const overrides = options[route.key];
|
|
302
|
+
|
|
303
|
+
if (navigation == null) {
|
|
304
|
+
throw new Error(
|
|
305
|
+
`Couldn't find a navigation object for route '${route.key}'.`
|
|
306
|
+
);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const customOptions = getOptions(route, navigation, overrides);
|
|
295
310
|
const element = render(route, navigation, customOptions, routes[i]?.state);
|
|
296
311
|
|
|
297
312
|
acc[route.key] = {
|
package/src/useEventEmitter.tsx
CHANGED
|
@@ -59,58 +59,67 @@ export function useEventEmitter<T extends Record<string, any>>(
|
|
|
59
59
|
target?: string;
|
|
60
60
|
canPreventDefault?: boolean;
|
|
61
61
|
}) => {
|
|
62
|
-
const items = listeners.current[type]
|
|
62
|
+
const items = listeners.current[type];
|
|
63
63
|
|
|
64
64
|
// Copy the current list of callbacks in case they are mutated during execution
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
)
|
|
69
|
-
|
|
65
|
+
let callbacks: Set<(e: any) => void> | undefined;
|
|
66
|
+
|
|
67
|
+
if (items) {
|
|
68
|
+
if (target !== undefined) {
|
|
69
|
+
const targetSet = items[target];
|
|
70
|
+
|
|
71
|
+
if (targetSet && targetSet.size) {
|
|
72
|
+
callbacks = new Set(targetSet);
|
|
73
|
+
}
|
|
74
|
+
} else {
|
|
75
|
+
for (const key in items) {
|
|
76
|
+
const set = items[key];
|
|
77
|
+
|
|
78
|
+
if (set && set.size) {
|
|
79
|
+
callbacks ??= new Set();
|
|
80
|
+
|
|
81
|
+
for (const cb of set) {
|
|
82
|
+
callbacks.add(cb);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
70
88
|
|
|
71
|
-
const
|
|
72
|
-
|
|
73
|
-
return type;
|
|
74
|
-
},
|
|
89
|
+
const descriptors: PropertyDescriptorMap = {
|
|
90
|
+
type: { enumerable: true, value: type },
|
|
75
91
|
};
|
|
76
92
|
|
|
77
93
|
if (target !== undefined) {
|
|
78
|
-
|
|
79
|
-
enumerable: true,
|
|
80
|
-
get() {
|
|
81
|
-
return target;
|
|
82
|
-
},
|
|
83
|
-
});
|
|
94
|
+
descriptors.target = { enumerable: true, value: target };
|
|
84
95
|
}
|
|
85
96
|
|
|
86
97
|
if (data !== undefined) {
|
|
87
|
-
|
|
88
|
-
enumerable: true,
|
|
89
|
-
get() {
|
|
90
|
-
return data;
|
|
91
|
-
},
|
|
92
|
-
});
|
|
98
|
+
descriptors.data = { enumerable: true, value: data };
|
|
93
99
|
}
|
|
94
100
|
|
|
101
|
+
let defaultPrevented = false;
|
|
102
|
+
|
|
95
103
|
if (canPreventDefault) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
enumerable: true,
|
|
101
|
-
get() {
|
|
102
|
-
return defaultPrevented;
|
|
103
|
-
},
|
|
104
|
+
descriptors.defaultPrevented = {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
get() {
|
|
107
|
+
return defaultPrevented;
|
|
104
108
|
},
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
109
|
+
};
|
|
110
|
+
descriptors.preventDefault = {
|
|
111
|
+
enumerable: true,
|
|
112
|
+
value() {
|
|
113
|
+
defaultPrevented = true;
|
|
110
114
|
},
|
|
111
|
-
}
|
|
115
|
+
};
|
|
112
116
|
}
|
|
113
117
|
|
|
118
|
+
const event: EventArg<any, any, any> = Object.defineProperties(
|
|
119
|
+
{} as EventArg<any, any, any>,
|
|
120
|
+
descriptors
|
|
121
|
+
);
|
|
122
|
+
|
|
114
123
|
listenRef.current?.(event);
|
|
115
124
|
|
|
116
125
|
callbacks?.forEach((cb) => cb(event));
|
package/src/useFocusEvents.tsx
CHANGED
|
@@ -17,10 +17,16 @@ export function useFocusEvents<State extends NavigationState>({
|
|
|
17
17
|
state,
|
|
18
18
|
emitter,
|
|
19
19
|
}: Options<State>) {
|
|
20
|
-
const navigation = React.
|
|
20
|
+
const navigation = React.use(NavigationContext);
|
|
21
21
|
const lastFocusedKeyRef = React.useRef<string | undefined>(undefined);
|
|
22
22
|
|
|
23
|
-
const
|
|
23
|
+
const currentFocusedRoute = state.routes[state.index];
|
|
24
|
+
|
|
25
|
+
if (currentFocusedRoute == null) {
|
|
26
|
+
throw new Error(`Couldn't find a route at index ${state.index}.`);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const currentFocusedKey = currentFocusedRoute.key;
|
|
24
30
|
|
|
25
31
|
// When the parent screen changes its focus state, we also need to change child's focus
|
|
26
32
|
// Coz the child screen can't be focused if the parent screen is out of focus
|
|
@@ -20,7 +20,7 @@ export function useFocusedListenersChildrenAdapter({
|
|
|
20
20
|
navigation,
|
|
21
21
|
focusedListeners,
|
|
22
22
|
}: Options) {
|
|
23
|
-
const { addListener } = React.
|
|
23
|
+
const { addListener } = React.use(NavigationBuilderContext);
|
|
24
24
|
|
|
25
25
|
const listener = React.useCallback(
|
|
26
26
|
(callback: FocusedNavigationCallback<any>) => {
|
|
@@ -41,7 +41,7 @@ export function useFocusedListenersChildrenAdapter({
|
|
|
41
41
|
[focusedListeners, navigation]
|
|
42
42
|
);
|
|
43
43
|
|
|
44
|
-
React.
|
|
44
|
+
React.useInsertionEffect(
|
|
45
45
|
() => addListener?.('focus', listener),
|
|
46
46
|
[addListener, listener]
|
|
47
47
|
);
|
package/src/useIsFocused.tsx
CHANGED
|
@@ -1,32 +1,25 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export const FocusedRouteKeyContext = React.createContext<string | undefined>(
|
|
4
|
+
undefined
|
|
5
|
+
);
|
|
6
|
+
|
|
7
|
+
export const IsFocusedContext = React.createContext<boolean | undefined>(
|
|
8
|
+
undefined
|
|
9
|
+
);
|
|
4
10
|
|
|
5
11
|
/**
|
|
6
12
|
* Hook to get the current focus state of the screen. Returns a `true` if screen is focused, otherwise `false`.
|
|
7
13
|
* This can be used if a component needs to render something based on the focus state.
|
|
8
14
|
*/
|
|
9
15
|
export function useIsFocused(): boolean {
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const subscribe = React.useCallback(
|
|
13
|
-
(callback: () => void) => {
|
|
14
|
-
const unsubscribeFocus = navigation.addListener('focus', callback);
|
|
15
|
-
const unsubscribeBlur = navigation.addListener('blur', callback);
|
|
16
|
-
|
|
17
|
-
return () => {
|
|
18
|
-
unsubscribeFocus();
|
|
19
|
-
unsubscribeBlur();
|
|
20
|
-
};
|
|
21
|
-
},
|
|
22
|
-
[navigation]
|
|
23
|
-
);
|
|
16
|
+
const isFocused = React.use(IsFocusedContext);
|
|
24
17
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
18
|
+
if (isFocused === undefined) {
|
|
19
|
+
throw new Error(
|
|
20
|
+
"Couldn't determine focus state. Is your component inside a screen in a navigator?"
|
|
21
|
+
);
|
|
22
|
+
}
|
|
30
23
|
|
|
31
|
-
return
|
|
24
|
+
return isFocused;
|
|
32
25
|
}
|
package/src/useNavigation.tsx
CHANGED
|
@@ -13,6 +13,8 @@ import type {
|
|
|
13
13
|
RootParamList,
|
|
14
14
|
} from './types';
|
|
15
15
|
|
|
16
|
+
type RootNavigation = GenericNavigation<RootParamList>;
|
|
17
|
+
|
|
16
18
|
/**
|
|
17
19
|
* Hook to access the navigation prop of the parent screen anywhere.
|
|
18
20
|
*
|
|
@@ -21,15 +23,17 @@ import type {
|
|
|
21
23
|
*
|
|
22
24
|
* @returns Navigation prop of the parent screen.
|
|
23
25
|
*/
|
|
24
|
-
export function useNavigation():
|
|
26
|
+
export function useNavigation(): RootNavigation;
|
|
25
27
|
export function useNavigation<
|
|
26
28
|
const Navigator = RootNavigator,
|
|
27
29
|
>(): NavigationListForNavigator<Navigator>[keyof NavigationListForNavigator<Navigator>];
|
|
28
30
|
export function useNavigation<
|
|
29
31
|
const Navigator = RootNavigator,
|
|
30
|
-
const RouteName extends
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
const RouteName extends string = string,
|
|
33
|
+
>(
|
|
34
|
+
name: RouteName & keyof NavigationListForNested<Navigator>
|
|
35
|
+
): NavigationListForNested<Navigator>[RouteName &
|
|
36
|
+
keyof NavigationListForNested<Navigator>];
|
|
33
37
|
export function useNavigation(name?: string): unknown {
|
|
34
38
|
const root = React.use(NavigationContainerRefContext);
|
|
35
39
|
const navigation = React.use(NavigationContext);
|