@react-navigation/core 6.0.2
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/LICENSE +21 -0
- package/README.md +38 -0
- package/lib/commonjs/BaseNavigationContainer.js +393 -0
- package/lib/commonjs/BaseNavigationContainer.js.map +1 -0
- package/lib/commonjs/CurrentRenderContext.js +21 -0
- package/lib/commonjs/CurrentRenderContext.js.map +1 -0
- package/lib/commonjs/EnsureSingleNavigator.js +53 -0
- package/lib/commonjs/EnsureSingleNavigator.js.map +1 -0
- package/lib/commonjs/Group.js +15 -0
- package/lib/commonjs/Group.js.map +1 -0
- package/lib/commonjs/NavigationBuilderContext.js +23 -0
- package/lib/commonjs/NavigationBuilderContext.js.map +1 -0
- package/lib/commonjs/NavigationContainerRefContext.js +20 -0
- package/lib/commonjs/NavigationContainerRefContext.js.map +1 -0
- package/lib/commonjs/NavigationContext.js +20 -0
- package/lib/commonjs/NavigationContext.js.map +1 -0
- package/lib/commonjs/NavigationHelpersContext.js +21 -0
- package/lib/commonjs/NavigationHelpersContext.js.map +1 -0
- package/lib/commonjs/NavigationRouteContext.js +20 -0
- package/lib/commonjs/NavigationRouteContext.js.map +1 -0
- package/lib/commonjs/NavigationStateContext.js +42 -0
- package/lib/commonjs/NavigationStateContext.js.map +1 -0
- package/lib/commonjs/SceneView.js +97 -0
- package/lib/commonjs/SceneView.js.map +1 -0
- package/lib/commonjs/Screen.js +15 -0
- package/lib/commonjs/Screen.js.map +1 -0
- package/lib/commonjs/StaticContainer.js +43 -0
- package/lib/commonjs/StaticContainer.js.map +1 -0
- package/lib/commonjs/UnhandledActionContext.js +17 -0
- package/lib/commonjs/UnhandledActionContext.js.map +1 -0
- package/lib/commonjs/checkDuplicateRouteNames.js +31 -0
- package/lib/commonjs/checkDuplicateRouteNames.js.map +1 -0
- package/lib/commonjs/checkSerializable.js +59 -0
- package/lib/commonjs/checkSerializable.js.map +1 -0
- package/lib/commonjs/createNavigationContainerRef.js +82 -0
- package/lib/commonjs/createNavigationContainerRef.js.map +1 -0
- package/lib/commonjs/createNavigatorFactory.js +34 -0
- package/lib/commonjs/createNavigatorFactory.js.map +1 -0
- package/lib/commonjs/findFocusedRoute.js +22 -0
- package/lib/commonjs/findFocusedRoute.js.map +1 -0
- package/lib/commonjs/fromEntries.js +19 -0
- package/lib/commonjs/fromEntries.js.map +1 -0
- package/lib/commonjs/getActionFromState.js +99 -0
- package/lib/commonjs/getActionFromState.js.map +1 -0
- package/lib/commonjs/getFocusedRouteNameFromRoute.js +23 -0
- package/lib/commonjs/getFocusedRouteNameFromRoute.js.map +1 -0
- package/lib/commonjs/getPathFromState.js +238 -0
- package/lib/commonjs/getPathFromState.js.map +1 -0
- package/lib/commonjs/getStateFromPath.js +450 -0
- package/lib/commonjs/getStateFromPath.js.map +1 -0
- package/lib/commonjs/index.js +227 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/isArrayEqual.js +15 -0
- package/lib/commonjs/isArrayEqual.js.map +1 -0
- package/lib/commonjs/types.js +18 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/useChildListeners.js +38 -0
- package/lib/commonjs/useChildListeners.js.map +1 -0
- package/lib/commonjs/useComponent.js +36 -0
- package/lib/commonjs/useComponent.js.map +1 -0
- package/lib/commonjs/useCurrentRender.js +33 -0
- package/lib/commonjs/useCurrentRender.js.map +1 -0
- package/lib/commonjs/useDescriptors.js +141 -0
- package/lib/commonjs/useDescriptors.js.map +1 -0
- package/lib/commonjs/useEventEmitter.js +118 -0
- package/lib/commonjs/useEventEmitter.js.map +1 -0
- package/lib/commonjs/useFocusEffect.js +97 -0
- package/lib/commonjs/useFocusEffect.js.map +1 -0
- package/lib/commonjs/useFocusEvents.js +77 -0
- package/lib/commonjs/useFocusEvents.js.map +1 -0
- package/lib/commonjs/useFocusedListenersChildrenAdapter.js +57 -0
- package/lib/commonjs/useFocusedListenersChildrenAdapter.js.map +1 -0
- package/lib/commonjs/useIsFocused.js +47 -0
- package/lib/commonjs/useIsFocused.js.map +1 -0
- package/lib/commonjs/useKeyedChildListeners.js +35 -0
- package/lib/commonjs/useKeyedChildListeners.js.map +1 -0
- package/lib/commonjs/useNavigation.js +36 -0
- package/lib/commonjs/useNavigation.js.map +1 -0
- package/lib/commonjs/useNavigationBuilder.js +480 -0
- package/lib/commonjs/useNavigationBuilder.js.map +1 -0
- package/lib/commonjs/useNavigationCache.js +122 -0
- package/lib/commonjs/useNavigationCache.js.map +1 -0
- package/lib/commonjs/useNavigationContainerRef.js +27 -0
- package/lib/commonjs/useNavigationContainerRef.js.map +1 -0
- package/lib/commonjs/useNavigationHelpers.js +77 -0
- package/lib/commonjs/useNavigationHelpers.js.map +1 -0
- package/lib/commonjs/useNavigationState.js +41 -0
- package/lib/commonjs/useNavigationState.js.map +1 -0
- package/lib/commonjs/useOnAction.js +118 -0
- package/lib/commonjs/useOnAction.js.map +1 -0
- package/lib/commonjs/useOnGetState.js +60 -0
- package/lib/commonjs/useOnGetState.js.map +1 -0
- package/lib/commonjs/useOnPreventRemove.js +89 -0
- package/lib/commonjs/useOnPreventRemove.js.map +1 -0
- package/lib/commonjs/useOnRouteFocus.js +45 -0
- package/lib/commonjs/useOnRouteFocus.js.map +1 -0
- package/lib/commonjs/useOptionsGetters.js +99 -0
- package/lib/commonjs/useOptionsGetters.js.map +1 -0
- package/lib/commonjs/useRegisterNavigator.js +40 -0
- package/lib/commonjs/useRegisterNavigator.js.map +1 -0
- package/lib/commonjs/useRoute.js +32 -0
- package/lib/commonjs/useRoute.js.map +1 -0
- package/lib/commonjs/useRouteCache.js +61 -0
- package/lib/commonjs/useRouteCache.js.map +1 -0
- package/lib/commonjs/useScheduleUpdate.js +44 -0
- package/lib/commonjs/useScheduleUpdate.js.map +1 -0
- package/lib/commonjs/useSyncState.js +76 -0
- package/lib/commonjs/useSyncState.js.map +1 -0
- package/lib/commonjs/validatePathConfig.js +31 -0
- package/lib/commonjs/validatePathConfig.js.map +1 -0
- package/lib/module/BaseNavigationContainer.js +360 -0
- package/lib/module/BaseNavigationContainer.js.map +1 -0
- package/lib/module/CurrentRenderContext.js +9 -0
- package/lib/module/CurrentRenderContext.js.map +1 -0
- package/lib/module/EnsureSingleNavigator.js +38 -0
- package/lib/module/EnsureSingleNavigator.js.map +1 -0
- package/lib/module/Group.js +8 -0
- package/lib/module/Group.js.map +1 -0
- package/lib/module/NavigationBuilderContext.js +11 -0
- package/lib/module/NavigationBuilderContext.js.map +1 -0
- package/lib/module/NavigationContainerRefContext.js +8 -0
- package/lib/module/NavigationContainerRefContext.js.map +1 -0
- package/lib/module/NavigationContext.js +8 -0
- package/lib/module/NavigationContext.js.map +1 -0
- package/lib/module/NavigationHelpersContext.js +9 -0
- package/lib/module/NavigationHelpersContext.js.map +1 -0
- package/lib/module/NavigationRouteContext.js +8 -0
- package/lib/module/NavigationRouteContext.js.map +1 -0
- package/lib/module/NavigationStateContext.js +27 -0
- package/lib/module/NavigationStateContext.js.map +1 -0
- package/lib/module/SceneView.js +80 -0
- package/lib/module/SceneView.js.map +1 -0
- package/lib/module/Screen.js +8 -0
- package/lib/module/Screen.js.map +1 -0
- package/lib/module/StaticContainer.js +30 -0
- package/lib/module/StaticContainer.js.map +1 -0
- package/lib/module/UnhandledActionContext.js +4 -0
- package/lib/module/UnhandledActionContext.js.map +1 -0
- package/lib/module/checkDuplicateRouteNames.js +24 -0
- package/lib/module/checkDuplicateRouteNames.js.map +1 -0
- package/lib/module/checkSerializable.js +52 -0
- package/lib/module/checkSerializable.js.map +1 -0
- package/lib/module/createNavigationContainerRef.js +71 -0
- package/lib/module/createNavigationContainerRef.js.map +1 -0
- package/lib/module/createNavigatorFactory.js +24 -0
- package/lib/module/createNavigatorFactory.js.map +1 -0
- package/lib/module/findFocusedRoute.js +15 -0
- package/lib/module/findFocusedRoute.js.map +1 -0
- package/lib/module/fromEntries.js +12 -0
- package/lib/module/fromEntries.js.map +1 -0
- package/lib/module/getActionFromState.js +92 -0
- package/lib/module/getActionFromState.js.map +1 -0
- package/lib/module/getFocusedRouteNameFromRoute.js +15 -0
- package/lib/module/getFocusedRouteNameFromRoute.js.map +1 -0
- package/lib/module/getPathFromState.js +223 -0
- package/lib/module/getPathFromState.js.map +1 -0
- package/lib/module/getStateFromPath.js +434 -0
- package/lib/module/getStateFromPath.js.map +1 -0
- package/lib/module/index.js +24 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/isArrayEqual.js +8 -0
- package/lib/module/isArrayEqual.js.map +1 -0
- package/lib/module/types.js +9 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/useChildListeners.js +27 -0
- package/lib/module/useChildListeners.js.map +1 -0
- package/lib/module/useComponent.js +24 -0
- package/lib/module/useComponent.js.map +1 -0
- package/lib/module/useCurrentRender.js +19 -0
- package/lib/module/useCurrentRender.js.map +1 -0
- package/lib/module/useDescriptors.js +122 -0
- package/lib/module/useDescriptors.js.map +1 -0
- package/lib/module/useEventEmitter.js +107 -0
- package/lib/module/useEventEmitter.js.map +1 -0
- package/lib/module/useFocusEffect.js +83 -0
- package/lib/module/useFocusEffect.js.map +1 -0
- package/lib/module/useFocusEvents.js +63 -0
- package/lib/module/useFocusEvents.js.map +1 -0
- package/lib/module/useFocusedListenersChildrenAdapter.js +43 -0
- package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -0
- package/lib/module/useIsFocused.js +34 -0
- package/lib/module/useIsFocused.js.map +1 -0
- package/lib/module/useKeyedChildListeners.js +24 -0
- package/lib/module/useKeyedChildListeners.js.map +1 -0
- package/lib/module/useNavigation.js +21 -0
- package/lib/module/useNavigation.js.map +1 -0
- package/lib/module/useNavigationBuilder.js +443 -0
- package/lib/module/useNavigationBuilder.js.map +1 -0
- package/lib/module/useNavigationCache.js +107 -0
- package/lib/module/useNavigationCache.js.map +1 -0
- package/lib/module/useNavigationContainerRef.js +12 -0
- package/lib/module/useNavigationContainerRef.js.map +1 -0
- package/lib/module/useNavigationHelpers.js +59 -0
- package/lib/module/useNavigationHelpers.js.map +1 -0
- package/lib/module/useNavigationState.js +27 -0
- package/lib/module/useNavigationState.js.map +1 -0
- package/lib/module/useOnAction.js +103 -0
- package/lib/module/useOnAction.js.map +1 -0
- package/lib/module/useOnGetState.js +43 -0
- package/lib/module/useOnGetState.js.map +1 -0
- package/lib/module/useOnPreventRemove.js +68 -0
- package/lib/module/useOnPreventRemove.js.map +1 -0
- package/lib/module/useOnRouteFocus.js +31 -0
- package/lib/module/useOnRouteFocus.js.map +1 -0
- package/lib/module/useOptionsGetters.js +83 -0
- package/lib/module/useOptionsGetters.js.map +1 -0
- package/lib/module/useRegisterNavigator.js +27 -0
- package/lib/module/useRegisterNavigator.js.map +1 -0
- package/lib/module/useRoute.js +18 -0
- package/lib/module/useRoute.js.map +1 -0
- package/lib/module/useRouteCache.js +47 -0
- package/lib/module/useRouteCache.js.map +1 -0
- package/lib/module/useScheduleUpdate.js +29 -0
- package/lib/module/useScheduleUpdate.js.map +1 -0
- package/lib/module/useSyncState.js +64 -0
- package/lib/module/useSyncState.js.map +1 -0
- package/lib/module/validatePathConfig.js +24 -0
- package/lib/module/validatePathConfig.js.map +1 -0
- package/lib/typescript/src/BaseNavigationContainer.d.ts +14 -0
- package/lib/typescript/src/CurrentRenderContext.d.ts +9 -0
- package/lib/typescript/src/EnsureSingleNavigator.d.ts +13 -0
- package/lib/typescript/src/Group.d.ts +6 -0
- package/lib/typescript/src/NavigationBuilderContext.d.ts +34 -0
- package/lib/typescript/src/NavigationContainerRefContext.d.ts +8 -0
- package/lib/typescript/src/NavigationContext.d.ts +24 -0
- package/lib/typescript/src/NavigationHelpersContext.d.ts +9 -0
- package/lib/typescript/src/NavigationRouteContext.d.ts +7 -0
- package/lib/typescript/src/NavigationStateContext.d.ts +45 -0
- package/lib/typescript/src/SceneView.d.ts +21 -0
- package/lib/typescript/src/Screen.d.ts +6 -0
- package/lib/typescript/src/StaticContainer.d.ts +7 -0
- package/lib/typescript/src/UnhandledActionContext.d.ts +4 -0
- package/lib/typescript/src/checkDuplicateRouteNames.d.ts +2 -0
- package/lib/typescript/src/checkSerializable.d.ts +9 -0
- package/lib/typescript/src/createNavigationContainerRef.d.ts +3 -0
- package/lib/typescript/src/createNavigatorFactory.d.ts +11 -0
- package/lib/typescript/src/findFocusedRoute.d.ts +22 -0
- package/lib/typescript/src/fromEntries.d.ts +1 -0
- package/lib/typescript/src/getActionFromState.d.ts +16 -0
- package/lib/typescript/src/getFocusedRouteNameFromRoute.d.ts +2 -0
- package/lib/typescript/src/getPathFromState.d.ts +38 -0
- package/lib/typescript/src/getStateFromPath.d.ts +32 -0
- package/lib/typescript/src/index.d.ts +23 -0
- package/lib/typescript/src/isArrayEqual.d.ts +5 -0
- package/lib/typescript/src/types.d.ts +485 -0
- package/lib/typescript/src/useChildListeners.d.ts +11 -0
- package/lib/typescript/src/useComponent.d.ts +2 -0
- package/lib/typescript/src/useCurrentRender.d.ts +13 -0
- package/lib/typescript/src/useDescriptors.d.ts +89 -0
- package/lib/typescript/src/useEventEmitter.d.ts +8 -0
- package/lib/typescript/src/useFocusEffect.d.ts +10 -0
- package/lib/typescript/src/useFocusEvents.d.ts +12 -0
- package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +12 -0
- package/lib/typescript/src/useIsFocused.d.ts +5 -0
- package/lib/typescript/src/useKeyedChildListeners.d.ts +11 -0
- package/lib/typescript/src/useNavigation.d.ts +7 -0
- package/lib/typescript/src/useNavigationBuilder.d.ts +223 -0
- package/lib/typescript/src/useNavigationCache.d.ts +19 -0
- package/lib/typescript/src/useNavigationContainerRef.d.ts +2 -0
- package/lib/typescript/src/useNavigationHelpers.d.ts +174 -0
- package/lib/typescript/src/useNavigationState.d.ts +9 -0
- package/lib/typescript/src/useOnAction.d.ts +25 -0
- package/lib/typescript/src/useOnGetState.d.ts +8 -0
- package/lib/typescript/src/useOnPreventRemove.d.ts +16 -0
- package/lib/typescript/src/useOnRouteFocus.d.ts +14 -0
- package/lib/typescript/src/useOptionsGetters.d.ts +12 -0
- package/lib/typescript/src/useRegisterNavigator.d.ts +5 -0
- package/lib/typescript/src/useRoute.d.ts +8 -0
- package/lib/typescript/src/useRouteCache.d.ts +13 -0
- package/lib/typescript/src/useScheduleUpdate.d.ts +13 -0
- package/lib/typescript/src/useSyncState.d.ts +4 -0
- package/lib/typescript/src/validatePathConfig.d.ts +1 -0
- package/package.json +73 -0
- package/src/BaseNavigationContainer.tsx +453 -0
- package/src/CurrentRenderContext.tsx +10 -0
- package/src/EnsureSingleNavigator.tsx +53 -0
- package/src/Group.tsx +14 -0
- package/src/NavigationBuilderContext.tsx +70 -0
- package/src/NavigationContainerRefContext.tsx +14 -0
- package/src/NavigationContext.tsx +12 -0
- package/src/NavigationHelpersContext.tsx +13 -0
- package/src/NavigationRouteContext.tsx +10 -0
- package/src/NavigationStateContext.tsx +39 -0
- package/src/SceneView.tsx +134 -0
- package/src/Screen.tsx +17 -0
- package/src/StaticContainer.tsx +29 -0
- package/src/UnhandledActionContext.tsx +9 -0
- package/src/checkDuplicateRouteNames.tsx +33 -0
- package/src/checkSerializable.tsx +74 -0
- package/src/createNavigationContainerRef.tsx +97 -0
- package/src/createNavigatorFactory.tsx +40 -0
- package/src/findFocusedRoute.tsx +13 -0
- package/src/fromEntries.tsx +13 -0
- package/src/getActionFromState.tsx +154 -0
- package/src/getFocusedRouteNameFromRoute.tsx +28 -0
- package/src/getPathFromState.tsx +297 -0
- package/src/getStateFromPath.tsx +575 -0
- package/src/index.tsx +23 -0
- package/src/isArrayEqual.tsx +7 -0
- package/src/types.tsx +645 -0
- package/src/useChildListeners.tsx +37 -0
- package/src/useComponent.tsx +30 -0
- package/src/useCurrentRender.tsx +35 -0
- package/src/useDescriptors.tsx +229 -0
- package/src/useEventEmitter.tsx +130 -0
- package/src/useFocusEffect.tsx +112 -0
- package/src/useFocusEvents.tsx +73 -0
- package/src/useFocusedListenersChildrenAdapter.tsx +47 -0
- package/src/useIsFocused.tsx +43 -0
- package/src/useKeyedChildListeners.tsx +40 -0
- package/src/useNavigation.tsx +26 -0
- package/src/useNavigationBuilder.tsx +640 -0
- package/src/useNavigationCache.tsx +159 -0
- package/src/useNavigationContainerRef.tsx +17 -0
- package/src/useNavigationHelpers.tsx +98 -0
- package/src/useNavigationState.tsx +41 -0
- package/src/useOnAction.tsx +166 -0
- package/src/useOnGetState.tsx +47 -0
- package/src/useOnPreventRemove.tsx +99 -0
- package/src/useOnRouteFocus.tsx +47 -0
- package/src/useOptionsGetters.tsx +98 -0
- package/src/useRegisterNavigator.tsx +29 -0
- package/src/useRoute.tsx +22 -0
- package/src/useRouteCache.tsx +55 -0
- package/src/useScheduleUpdate.tsx +32 -0
- package/src/useSyncState.tsx +74 -0
- package/src/validatePathConfig.tsx +32 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import createNavigationContainerRef from './createNavigationContainerRef';
|
|
3
|
+
export default function useNavigationContainerRef() {
|
|
4
|
+
const navigation = React.useRef(null);
|
|
5
|
+
|
|
6
|
+
if (navigation.current == null) {
|
|
7
|
+
navigation.current = createNavigationContainerRef();
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return navigation.current;
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=useNavigationContainerRef.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationContainerRef.tsx"],"names":["React","createNavigationContainerRef","useNavigationContainerRef","navigation","useRef","current"],"mappings":"AAAA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,4BAAP,MAAyC,gCAAzC;AAGA,eAAe,SAASC,yBAAT,GAEmC;AAChD,QAAMC,UAAU,GACdH,KAAK,CAACI,MAAN,CAAkE,IAAlE,CADF;;AAGA,MAAID,UAAU,CAACE,OAAX,IAAsB,IAA1B,EAAgC;AAC9BF,IAAAA,UAAU,CAACE,OAAX,GAAqBJ,4BAA4B,EAAjD;AACD;;AAED,SAAOE,UAAU,CAACE,OAAlB;AACD","sourcesContent":["import * as React from 'react';\n\nimport createNavigationContainerRef from './createNavigationContainerRef';\nimport type { NavigationContainerRefWithCurrent } from './types';\n\nexport default function useNavigationContainerRef<\n ParamList extends {} = ReactNavigation.RootParamList\n>(): NavigationContainerRefWithCurrent<ParamList> {\n const navigation =\n React.useRef<NavigationContainerRefWithCurrent<ParamList> | null>(null);\n\n if (navigation.current == null) {\n navigation.current = createNavigationContainerRef<ParamList>();\n }\n\n return navigation.current;\n}\n"]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { CommonActions } from '@react-navigation/routers';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import NavigationContext from './NavigationContext';
|
|
4
|
+
import { PrivateValueStore } from './types';
|
|
5
|
+
import UnhandledActionContext from './UnhandledActionContext';
|
|
6
|
+
// This is to make TypeScript compiler happy
|
|
7
|
+
// eslint-disable-next-line babel/no-unused-expressions
|
|
8
|
+
PrivateValueStore;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Navigation object with helper methods to be used by a navigator.
|
|
12
|
+
* This object includes methods for common actions as well as methods the parent screen's navigation object.
|
|
13
|
+
*/
|
|
14
|
+
export default function useNavigationHelpers({
|
|
15
|
+
onAction,
|
|
16
|
+
getState,
|
|
17
|
+
emitter,
|
|
18
|
+
router
|
|
19
|
+
}) {
|
|
20
|
+
const onUnhandledAction = React.useContext(UnhandledActionContext);
|
|
21
|
+
const parentNavigationHelpers = React.useContext(NavigationContext);
|
|
22
|
+
return React.useMemo(() => {
|
|
23
|
+
const dispatch = op => {
|
|
24
|
+
const action = typeof op === 'function' ? op(getState()) : op;
|
|
25
|
+
const handled = onAction(action);
|
|
26
|
+
|
|
27
|
+
if (!handled) {
|
|
28
|
+
onUnhandledAction === null || onUnhandledAction === void 0 ? void 0 : onUnhandledAction(action);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const actions = { ...router.actionCreators,
|
|
33
|
+
...CommonActions
|
|
34
|
+
};
|
|
35
|
+
const helpers = Object.keys(actions).reduce((acc, name) => {
|
|
36
|
+
// @ts-expect-error: name is a valid key, but TypeScript is dumb
|
|
37
|
+
acc[name] = (...args) => dispatch(actions[name](...args));
|
|
38
|
+
|
|
39
|
+
return acc;
|
|
40
|
+
}, {});
|
|
41
|
+
return { ...parentNavigationHelpers,
|
|
42
|
+
...helpers,
|
|
43
|
+
dispatch,
|
|
44
|
+
emit: emitter.emit,
|
|
45
|
+
isFocused: parentNavigationHelpers ? parentNavigationHelpers.isFocused : () => true,
|
|
46
|
+
canGoBack: () => {
|
|
47
|
+
const state = getState();
|
|
48
|
+
return router.getStateForAction(state, CommonActions.goBack(), {
|
|
49
|
+
routeNames: state.routeNames,
|
|
50
|
+
routeParamList: {},
|
|
51
|
+
routeGetIdList: {}
|
|
52
|
+
}) !== null || (parentNavigationHelpers === null || parentNavigationHelpers === void 0 ? void 0 : parentNavigationHelpers.canGoBack()) || false;
|
|
53
|
+
},
|
|
54
|
+
getParent: () => parentNavigationHelpers,
|
|
55
|
+
getState
|
|
56
|
+
};
|
|
57
|
+
}, [emitter.emit, getState, onAction, onUnhandledAction, parentNavigationHelpers, router]);
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=useNavigationHelpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationHelpers.tsx"],"names":["CommonActions","React","NavigationContext","PrivateValueStore","UnhandledActionContext","useNavigationHelpers","onAction","getState","emitter","router","onUnhandledAction","useContext","parentNavigationHelpers","useMemo","dispatch","op","action","handled","actions","actionCreators","helpers","Object","keys","reduce","acc","name","args","emit","isFocused","canGoBack","state","getStateForAction","goBack","routeNames","routeParamList","routeGetIdList","getParent"],"mappings":"AAAA,SACEA,aADF,QAMO,2BANP;AAOA,OAAO,KAAKC,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,iBAAP,MAA8B,qBAA9B;AACA,SAA4CC,iBAA5C,QAAqE,SAArE;AACA,OAAOC,sBAAP,MAAmC,0BAAnC;AAGA;AACA;AACAD,iBAAiB;;AASjB;AACA;AACA;AACA;AACA,eAAe,SAASE,oBAAT,CAKb;AAAEC,EAAAA,QAAF;AAAYC,EAAAA,QAAZ;AAAsBC,EAAAA,OAAtB;AAA+BC,EAAAA;AAA/B,CALa,EAKoD;AACjE,QAAMC,iBAAiB,GAAGT,KAAK,CAACU,UAAN,CAAiBP,sBAAjB,CAA1B;AACA,QAAMQ,uBAAuB,GAAGX,KAAK,CAACU,UAAN,CAAiBT,iBAAjB,CAAhC;AAEA,SAAOD,KAAK,CAACY,OAAN,CAAc,MAAM;AACzB,UAAMC,QAAQ,GAAIC,EAAD,IAA6C;AAC5D,YAAMC,MAAM,GAAG,OAAOD,EAAP,KAAc,UAAd,GAA2BA,EAAE,CAACR,QAAQ,EAAT,CAA7B,GAA4CQ,EAA3D;AAEA,YAAME,OAAO,GAAGX,QAAQ,CAACU,MAAD,CAAxB;;AAEA,UAAI,CAACC,OAAL,EAAc;AACZP,QAAAA,iBAAiB,SAAjB,IAAAA,iBAAiB,WAAjB,YAAAA,iBAAiB,CAAGM,MAAH,CAAjB;AACD;AACF,KARD;;AAUA,UAAME,OAAO,GAAG,EACd,GAAGT,MAAM,CAACU,cADI;AAEd,SAAGnB;AAFW,KAAhB;AAKA,UAAMoB,OAAO,GAAGC,MAAM,CAACC,IAAP,CAAYJ,OAAZ,EAAqBK,MAArB,CACd,CAACC,GAAD,EAAMC,IAAN,KAAe;AACb;AACAD,MAAAA,GAAG,CAACC,IAAD,CAAH,GAAY,CAAC,GAAGC,IAAJ,KAAkBZ,QAAQ,CAACI,OAAO,CAACO,IAAD,CAAP,CAAc,GAAGC,IAAjB,CAAD,CAAtC;;AACA,aAAOF,GAAP;AACD,KALa,EAMd,EANc,CAAhB;AASA,WAAO,EACL,GAAGZ,uBADE;AAEL,SAAGQ,OAFE;AAGLN,MAAAA,QAHK;AAILa,MAAAA,IAAI,EAAEnB,OAAO,CAACmB,IAJT;AAKLC,MAAAA,SAAS,EAAEhB,uBAAuB,GAC9BA,uBAAuB,CAACgB,SADM,GAE9B,MAAM,IAPL;AAQLC,MAAAA,SAAS,EAAE,MAAM;AACf,cAAMC,KAAK,GAAGvB,QAAQ,EAAtB;AAEA,eACEE,MAAM,CAACsB,iBAAP,CAAyBD,KAAzB,EAAgC9B,aAAa,CAACgC,MAAd,EAAhC,EAAkE;AAChEC,UAAAA,UAAU,EAAEH,KAAK,CAACG,UAD8C;AAEhEC,UAAAA,cAAc,EAAE,EAFgD;AAGhEC,UAAAA,cAAc,EAAE;AAHgD,SAAlE,MAIO,IAJP,KAKAvB,uBALA,aAKAA,uBALA,uBAKAA,uBAAuB,CAAEiB,SAAzB,EALA,KAMA,KAPF;AASD,OApBI;AAqBLO,MAAAA,SAAS,EAAE,MAAMxB,uBArBZ;AAsBLL,MAAAA;AAtBK,KAAP;AA0BD,GAnDM,EAmDJ,CACDC,OAAO,CAACmB,IADP,EAEDpB,QAFC,EAGDD,QAHC,EAIDI,iBAJC,EAKDE,uBALC,EAMDH,MANC,CAnDI,CAAP;AA2DD","sourcesContent":["import {\n CommonActions,\n NavigationAction,\n NavigationState,\n ParamListBase,\n Router,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationContext from './NavigationContext';\nimport { NavigationHelpers, NavigationProp, PrivateValueStore } from './types';\nimport UnhandledActionContext from './UnhandledActionContext';\nimport type { NavigationEventEmitter } from './useEventEmitter';\n\n// This is to make TypeScript compiler happy\n// eslint-disable-next-line babel/no-unused-expressions\nPrivateValueStore;\n\ntype Options<State extends NavigationState, Action extends NavigationAction> = {\n onAction: (action: NavigationAction) => boolean;\n getState: () => State;\n emitter: NavigationEventEmitter<any>;\n router: Router<State, Action>;\n};\n\n/**\n * Navigation object with helper methods to be used by a navigator.\n * This object includes methods for common actions as well as methods the parent screen's navigation object.\n */\nexport default function useNavigationHelpers<\n State extends NavigationState,\n ActionHelpers extends Record<string, () => void>,\n Action extends NavigationAction,\n EventMap extends Record<string, any>\n>({ onAction, getState, emitter, router }: Options<State, Action>) {\n const onUnhandledAction = React.useContext(UnhandledActionContext);\n const parentNavigationHelpers = React.useContext(NavigationContext);\n\n return React.useMemo(() => {\n const dispatch = (op: Action | ((state: State) => Action)) => {\n const action = typeof op === 'function' ? op(getState()) : op;\n\n const handled = onAction(action);\n\n if (!handled) {\n onUnhandledAction?.(action);\n }\n };\n\n const actions = {\n ...router.actionCreators,\n ...CommonActions,\n };\n\n const helpers = Object.keys(actions).reduce<Record<string, () => void>>(\n (acc, name) => {\n // @ts-expect-error: name is a valid key, but TypeScript is dumb\n acc[name] = (...args: any) => dispatch(actions[name](...args));\n return acc;\n },\n {}\n );\n\n return {\n ...parentNavigationHelpers,\n ...helpers,\n dispatch,\n emit: emitter.emit,\n isFocused: parentNavigationHelpers\n ? parentNavigationHelpers.isFocused\n : () => true,\n canGoBack: () => {\n const state = getState();\n\n return (\n router.getStateForAction(state, CommonActions.goBack() as Action, {\n routeNames: state.routeNames,\n routeParamList: {},\n routeGetIdList: {},\n }) !== null ||\n parentNavigationHelpers?.canGoBack() ||\n false\n );\n },\n getParent: () => parentNavigationHelpers as any,\n getState,\n } as NavigationHelpers<ParamListBase, EventMap> &\n (NavigationProp<ParamListBase, string, any, any, any> | undefined) &\n ActionHelpers;\n }, [\n emitter.emit,\n getState,\n onAction,\n onUnhandledAction,\n parentNavigationHelpers,\n router,\n ]);\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import useNavigation from './useNavigation';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hook to get a value from the current navigation state using a selector.
|
|
6
|
+
*
|
|
7
|
+
* @param selector Selector function to get a value from the state.
|
|
8
|
+
*/
|
|
9
|
+
export default function useNavigationState(selector) {
|
|
10
|
+
const navigation = useNavigation(); // We don't care about the state value, we run the selector again at the end
|
|
11
|
+
// The state is only to make sure that there's a re-render when we have a new value
|
|
12
|
+
|
|
13
|
+
const [, setResult] = React.useState(() => selector(navigation.getState())); // We store the selector in a ref to avoid re-subscribing listeners every render
|
|
14
|
+
|
|
15
|
+
const selectorRef = React.useRef(selector);
|
|
16
|
+
React.useEffect(() => {
|
|
17
|
+
selectorRef.current = selector;
|
|
18
|
+
});
|
|
19
|
+
React.useEffect(() => {
|
|
20
|
+
const unsubscribe = navigation.addListener('state', e => {
|
|
21
|
+
setResult(selectorRef.current(e.data.state));
|
|
22
|
+
});
|
|
23
|
+
return unsubscribe;
|
|
24
|
+
}, [navigation]);
|
|
25
|
+
return selector(navigation.getState());
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=useNavigationState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useNavigationState.tsx"],"names":["React","useNavigation","useNavigationState","selector","navigation","setResult","useState","getState","selectorRef","useRef","useEffect","current","unsubscribe","addListener","e","data","state"],"mappings":"AACA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAGA,OAAOC,aAAP,MAA0B,iBAA1B;;AAMA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,kBAAT,CACbC,QADa,EAEV;AACH,QAAMC,UAAU,GAAGH,aAAa,EAAhC,CADG,CAGH;AACA;;AACA,QAAM,GAAGI,SAAH,IAAgBL,KAAK,CAACM,QAAN,CAAe,MAAMH,QAAQ,CAACC,UAAU,CAACG,QAAX,EAAD,CAA7B,CAAtB,CALG,CAOH;;AACA,QAAMC,WAAW,GAAGR,KAAK,CAACS,MAAN,CAAaN,QAAb,CAApB;AAEAH,EAAAA,KAAK,CAACU,SAAN,CAAgB,MAAM;AACpBF,IAAAA,WAAW,CAACG,OAAZ,GAAsBR,QAAtB;AACD,GAFD;AAIAH,EAAAA,KAAK,CAACU,SAAN,CAAgB,MAAM;AACpB,UAAME,WAAW,GAAGR,UAAU,CAACS,WAAX,CAAuB,OAAvB,EAAiCC,CAAD,IAAO;AACzDT,MAAAA,SAAS,CAACG,WAAW,CAACG,OAAZ,CAAoBG,CAAC,CAACC,IAAF,CAAOC,KAA3B,CAAD,CAAT;AACD,KAFmB,CAApB;AAIA,WAAOJ,WAAP;AACD,GAND,EAMG,CAACR,UAAD,CANH;AAQA,SAAOD,QAAQ,CAACC,UAAU,CAACG,QAAX,EAAD,CAAf;AACD","sourcesContent":["import type { NavigationState, ParamListBase } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport type { NavigationProp } from './types';\nimport useNavigation from './useNavigation';\n\ntype Selector<ParamList extends ParamListBase, T> = (\n state: NavigationState<ParamList>\n) => T;\n\n/**\n * Hook to get a value from the current navigation state using a selector.\n *\n * @param selector Selector function to get a value from the state.\n */\nexport default function useNavigationState<ParamList extends ParamListBase, T>(\n selector: Selector<ParamList, T>\n): T {\n const navigation = useNavigation<NavigationProp<ParamList>>();\n\n // We don't care about the state value, we run the selector again at the end\n // The state is only to make sure that there's a re-render when we have a new value\n const [, setResult] = React.useState(() => selector(navigation.getState()));\n\n // We store the selector in a ref to avoid re-subscribing listeners every render\n const selectorRef = React.useRef(selector);\n\n React.useEffect(() => {\n selectorRef.current = selector;\n });\n\n React.useEffect(() => {\n const unsubscribe = navigation.addListener('state', (e) => {\n setResult(selectorRef.current(e.data.state));\n });\n\n return unsubscribe;\n }, [navigation]);\n\n return selector(navigation.getState());\n}\n"]}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import NavigationBuilderContext from './NavigationBuilderContext';
|
|
3
|
+
import useOnPreventRemove, { shouldPreventRemove } from './useOnPreventRemove';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Hook to handle actions for a navigator, including state updates and bubbling.
|
|
7
|
+
*
|
|
8
|
+
* Bubbling an action is achieved in 2 ways:
|
|
9
|
+
* 1. To bubble action to parent, we expose the action handler in context and then access the parent context
|
|
10
|
+
* 2. To bubble action to child, child adds event listeners subscribing to actions from parent
|
|
11
|
+
*
|
|
12
|
+
* When the action handler handles as action, it returns `true`, otherwise `false`.
|
|
13
|
+
*/
|
|
14
|
+
export default function useOnAction({
|
|
15
|
+
router,
|
|
16
|
+
getState,
|
|
17
|
+
setState,
|
|
18
|
+
key,
|
|
19
|
+
actionListeners,
|
|
20
|
+
beforeRemoveListeners,
|
|
21
|
+
routerConfigOptions,
|
|
22
|
+
emitter
|
|
23
|
+
}) {
|
|
24
|
+
const {
|
|
25
|
+
onAction: onActionParent,
|
|
26
|
+
onRouteFocus: onRouteFocusParent,
|
|
27
|
+
addListener: addListenerParent,
|
|
28
|
+
onDispatchAction
|
|
29
|
+
} = React.useContext(NavigationBuilderContext);
|
|
30
|
+
const routerConfigOptionsRef = React.useRef(routerConfigOptions);
|
|
31
|
+
React.useEffect(() => {
|
|
32
|
+
routerConfigOptionsRef.current = routerConfigOptions;
|
|
33
|
+
});
|
|
34
|
+
const onAction = React.useCallback((action, visitedNavigators = new Set()) => {
|
|
35
|
+
const state = getState(); // Since actions can bubble both up and down, they could come to the same navigator again
|
|
36
|
+
// We keep track of navigators which have already tried to handle the action and return if it's already visited
|
|
37
|
+
|
|
38
|
+
if (visitedNavigators.has(state.key)) {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
visitedNavigators.add(state.key);
|
|
43
|
+
|
|
44
|
+
if (typeof action.target !== 'string' || action.target === state.key) {
|
|
45
|
+
let result = router.getStateForAction(state, action, routerConfigOptionsRef.current); // If a target is specified and set to current navigator, the action shouldn't bubble
|
|
46
|
+
// So instead of `null`, we use the state object for such cases to signal that action was handled
|
|
47
|
+
|
|
48
|
+
result = result === null && action.target === state.key ? state : result;
|
|
49
|
+
|
|
50
|
+
if (result !== null) {
|
|
51
|
+
onDispatchAction(action, state === result);
|
|
52
|
+
|
|
53
|
+
if (state !== result) {
|
|
54
|
+
const isPrevented = shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, result.routes, action);
|
|
55
|
+
|
|
56
|
+
if (isPrevented) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
setState(result);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (onRouteFocusParent !== undefined) {
|
|
64
|
+
// Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree
|
|
65
|
+
// This means we need to focus all of the parent navigators of this navigator as well
|
|
66
|
+
const shouldFocus = router.shouldActionChangeFocus(action);
|
|
67
|
+
|
|
68
|
+
if (shouldFocus && key !== undefined) {
|
|
69
|
+
onRouteFocusParent(key);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (onActionParent !== undefined) {
|
|
78
|
+
// Bubble action to the parent if the current navigator didn't handle it
|
|
79
|
+
if (onActionParent(action, visitedNavigators)) {
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
} // If the action wasn't handled by current navigator or a parent navigator, let children handle it
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
for (let i = actionListeners.length - 1; i >= 0; i--) {
|
|
86
|
+
const listener = actionListeners[i];
|
|
87
|
+
|
|
88
|
+
if (listener(action, visitedNavigators)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return false;
|
|
94
|
+
}, [actionListeners, beforeRemoveListeners, emitter, getState, key, onActionParent, onDispatchAction, onRouteFocusParent, router, setState]);
|
|
95
|
+
useOnPreventRemove({
|
|
96
|
+
getState,
|
|
97
|
+
emitter,
|
|
98
|
+
beforeRemoveListeners
|
|
99
|
+
});
|
|
100
|
+
React.useEffect(() => addListenerParent === null || addListenerParent === void 0 ? void 0 : addListenerParent('action', onAction), [addListenerParent, onAction]);
|
|
101
|
+
return onAction;
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=useOnAction.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnAction.tsx"],"names":["React","NavigationBuilderContext","useOnPreventRemove","shouldPreventRemove","useOnAction","router","getState","setState","key","actionListeners","beforeRemoveListeners","routerConfigOptions","emitter","onAction","onActionParent","onRouteFocus","onRouteFocusParent","addListener","addListenerParent","onDispatchAction","useContext","routerConfigOptionsRef","useRef","useEffect","current","useCallback","action","visitedNavigators","Set","state","has","add","target","result","getStateForAction","isPrevented","routes","undefined","shouldFocus","shouldActionChangeFocus","i","length","listener"],"mappings":"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","sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n PartialState,\n Router,\n RouterConfigOptions,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext, {\n ChildActionListener,\n ChildBeforeRemoveListener,\n} from './NavigationBuilderContext';\nimport type { EventMapCore } from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\nimport useOnPreventRemove, { shouldPreventRemove } from './useOnPreventRemove';\n\ntype Options = {\n router: Router<NavigationState, NavigationAction>;\n key?: string;\n getState: () => NavigationState;\n setState: (state: NavigationState | PartialState<NavigationState>) => void;\n actionListeners: ChildActionListener[];\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>;\n routerConfigOptions: RouterConfigOptions;\n emitter: NavigationEventEmitter<EventMapCore<any>>;\n};\n\n/**\n * Hook to handle actions for a navigator, including state updates and bubbling.\n *\n * Bubbling an action is achieved in 2 ways:\n * 1. To bubble action to parent, we expose the action handler in context and then access the parent context\n * 2. To bubble action to child, child adds event listeners subscribing to actions from parent\n *\n * When the action handler handles as action, it returns `true`, otherwise `false`.\n */\nexport default function useOnAction({\n router,\n getState,\n setState,\n key,\n actionListeners,\n beforeRemoveListeners,\n routerConfigOptions,\n emitter,\n}: Options) {\n const {\n onAction: onActionParent,\n onRouteFocus: onRouteFocusParent,\n addListener: addListenerParent,\n onDispatchAction,\n } = React.useContext(NavigationBuilderContext);\n\n const routerConfigOptionsRef =\n React.useRef<RouterConfigOptions>(routerConfigOptions);\n\n React.useEffect(() => {\n routerConfigOptionsRef.current = routerConfigOptions;\n });\n\n const onAction = React.useCallback(\n (\n action: NavigationAction,\n visitedNavigators: Set<string> = new Set<string>()\n ) => {\n const state = getState();\n\n // Since actions can bubble both up and down, they could come to the same navigator again\n // We keep track of navigators which have already tried to handle the action and return if it's already visited\n if (visitedNavigators.has(state.key)) {\n return false;\n }\n\n visitedNavigators.add(state.key);\n\n if (typeof action.target !== 'string' || action.target === state.key) {\n let result = router.getStateForAction(\n state,\n action,\n routerConfigOptionsRef.current\n );\n\n // If a target is specified and set to current navigator, the action shouldn't bubble\n // So instead of `null`, we use the state object for such cases to signal that action was handled\n result =\n result === null && action.target === state.key ? state : result;\n\n if (result !== null) {\n onDispatchAction(action, state === result);\n\n if (state !== result) {\n const isPrevented = shouldPreventRemove(\n emitter,\n beforeRemoveListeners,\n state.routes,\n result.routes,\n action\n );\n\n if (isPrevented) {\n return true;\n }\n\n setState(result);\n }\n\n if (onRouteFocusParent !== undefined) {\n // Some actions such as `NAVIGATE` also want to bring the navigated route to focus in the whole tree\n // This means we need to focus all of the parent navigators of this navigator as well\n const shouldFocus = router.shouldActionChangeFocus(action);\n\n if (shouldFocus && key !== undefined) {\n onRouteFocusParent(key);\n }\n }\n\n return true;\n }\n }\n\n if (onActionParent !== undefined) {\n // Bubble action to the parent if the current navigator didn't handle it\n if (onActionParent(action, visitedNavigators)) {\n return true;\n }\n }\n\n // If the action wasn't handled by current navigator or a parent navigator, let children handle it\n for (let i = actionListeners.length - 1; i >= 0; i--) {\n const listener = actionListeners[i];\n\n if (listener(action, visitedNavigators)) {\n return true;\n }\n }\n\n return false;\n },\n [\n actionListeners,\n beforeRemoveListeners,\n emitter,\n getState,\n key,\n onActionParent,\n onDispatchAction,\n onRouteFocusParent,\n router,\n setState,\n ]\n );\n\n useOnPreventRemove({\n getState,\n emitter,\n beforeRemoveListeners,\n });\n\n React.useEffect(\n () => addListenerParent?.('action', onAction),\n [addListenerParent, onAction]\n );\n\n return onAction;\n}\n"]}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import isArrayEqual from './isArrayEqual';
|
|
3
|
+
import NavigationBuilderContext from './NavigationBuilderContext';
|
|
4
|
+
import NavigationRouteContext from './NavigationRouteContext';
|
|
5
|
+
export default function useOnGetState({
|
|
6
|
+
getState,
|
|
7
|
+
getStateListeners
|
|
8
|
+
}) {
|
|
9
|
+
const {
|
|
10
|
+
addKeyedListener
|
|
11
|
+
} = React.useContext(NavigationBuilderContext);
|
|
12
|
+
const route = React.useContext(NavigationRouteContext);
|
|
13
|
+
const key = route ? route.key : 'root';
|
|
14
|
+
const getRehydratedState = React.useCallback(() => {
|
|
15
|
+
const state = getState(); // Avoid returning new route objects if we don't need to
|
|
16
|
+
|
|
17
|
+
const routes = state.routes.map(route => {
|
|
18
|
+
var _getStateListeners$ro;
|
|
19
|
+
|
|
20
|
+
const childState = (_getStateListeners$ro = getStateListeners[route.key]) === null || _getStateListeners$ro === void 0 ? void 0 : _getStateListeners$ro.call(getStateListeners);
|
|
21
|
+
|
|
22
|
+
if (route.state === childState) {
|
|
23
|
+
return route;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return { ...route,
|
|
27
|
+
state: childState
|
|
28
|
+
};
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (isArrayEqual(state.routes, routes)) {
|
|
32
|
+
return state;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return { ...state,
|
|
36
|
+
routes
|
|
37
|
+
};
|
|
38
|
+
}, [getState, getStateListeners]);
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('getState', key, getRehydratedState);
|
|
41
|
+
}, [addKeyedListener, getRehydratedState, key]);
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=useOnGetState.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnGetState.tsx"],"names":["React","isArrayEqual","NavigationBuilderContext","NavigationRouteContext","useOnGetState","getState","getStateListeners","addKeyedListener","useContext","route","key","getRehydratedState","useCallback","state","routes","map","childState","useEffect"],"mappings":"AACA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,YAAP,MAAyB,gBAAzB;AACA,OAAOC,wBAAP,MAEO,4BAFP;AAGA,OAAOC,sBAAP,MAAmC,0BAAnC;AAOA,eAAe,SAASC,aAAT,CAAuB;AACpCC,EAAAA,QADoC;AAEpCC,EAAAA;AAFoC,CAAvB,EAGH;AACV,QAAM;AAAEC,IAAAA;AAAF,MAAuBP,KAAK,CAACQ,UAAN,CAAiBN,wBAAjB,CAA7B;AACA,QAAMO,KAAK,GAAGT,KAAK,CAACQ,UAAN,CAAiBL,sBAAjB,CAAd;AACA,QAAMO,GAAG,GAAGD,KAAK,GAAGA,KAAK,CAACC,GAAT,GAAe,MAAhC;AAEA,QAAMC,kBAAkB,GAAGX,KAAK,CAACY,WAAN,CAAkB,MAAM;AACjD,UAAMC,KAAK,GAAGR,QAAQ,EAAtB,CADiD,CAGjD;;AACA,UAAMS,MAAM,GAAGD,KAAK,CAACC,MAAN,CAAaC,GAAb,CAAkBN,KAAD,IAAW;AAAA;;AACzC,YAAMO,UAAU,4BAAGV,iBAAiB,CAACG,KAAK,CAACC,GAAP,CAApB,0DAAG,2BAAAJ,iBAAiB,CAApC;;AAEA,UAAIG,KAAK,CAACI,KAAN,KAAgBG,UAApB,EAAgC;AAC9B,eAAOP,KAAP;AACD;;AAED,aAAO,EAAE,GAAGA,KAAL;AAAYI,QAAAA,KAAK,EAAEG;AAAnB,OAAP;AACD,KARc,CAAf;;AAUA,QAAIf,YAAY,CAACY,KAAK,CAACC,MAAP,EAAeA,MAAf,CAAhB,EAAwC;AACtC,aAAOD,KAAP;AACD;;AAED,WAAO,EAAE,GAAGA,KAAL;AAAYC,MAAAA;AAAZ,KAAP;AACD,GAnB0B,EAmBxB,CAACT,QAAD,EAAWC,iBAAX,CAnBwB,CAA3B;AAqBAN,EAAAA,KAAK,CAACiB,SAAN,CAAgB,MAAM;AACpB,WAAOV,gBAAP,aAAOA,gBAAP,uBAAOA,gBAAgB,CAAG,UAAH,EAAeG,GAAf,EAAoBC,kBAApB,CAAvB;AACD,GAFD,EAEG,CAACJ,gBAAD,EAAmBI,kBAAnB,EAAuCD,GAAvC,CAFH;AAGD","sourcesContent":["import type { NavigationState } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport isArrayEqual from './isArrayEqual';\nimport NavigationBuilderContext, {\n GetStateListener,\n} from './NavigationBuilderContext';\nimport NavigationRouteContext from './NavigationRouteContext';\n\ntype Options = {\n getState: () => NavigationState;\n getStateListeners: Record<string, GetStateListener | undefined>;\n};\n\nexport default function useOnGetState({\n getState,\n getStateListeners,\n}: Options) {\n const { addKeyedListener } = React.useContext(NavigationBuilderContext);\n const route = React.useContext(NavigationRouteContext);\n const key = route ? route.key : 'root';\n\n const getRehydratedState = React.useCallback(() => {\n const state = getState();\n\n // Avoid returning new route objects if we don't need to\n const routes = state.routes.map((route) => {\n const childState = getStateListeners[route.key]?.();\n\n if (route.state === childState) {\n return route;\n }\n\n return { ...route, state: childState };\n });\n\n if (isArrayEqual(state.routes, routes)) {\n return state;\n }\n\n return { ...state, routes };\n }, [getState, getStateListeners]);\n\n React.useEffect(() => {\n return addKeyedListener?.('getState', key, getRehydratedState);\n }, [addKeyedListener, getRehydratedState, key]);\n}\n"]}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import NavigationBuilderContext from './NavigationBuilderContext';
|
|
3
|
+
import NavigationRouteContext from './NavigationRouteContext';
|
|
4
|
+
const VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS');
|
|
5
|
+
export const shouldPreventRemove = (emitter, beforeRemoveListeners, currentRoutes, nextRoutes, action) => {
|
|
6
|
+
var _action$VISITED_ROUTE;
|
|
7
|
+
|
|
8
|
+
const nextRouteKeys = nextRoutes.map(route => route.key); // Call these in reverse order so last screens handle the event first
|
|
9
|
+
|
|
10
|
+
const removedRoutes = currentRoutes.filter(route => !nextRouteKeys.includes(route.key)).reverse();
|
|
11
|
+
const visitedRouteKeys = // @ts-expect-error: add this property to mark that we've already emitted this action
|
|
12
|
+
(_action$VISITED_ROUTE = action[VISITED_ROUTE_KEYS]) !== null && _action$VISITED_ROUTE !== void 0 ? _action$VISITED_ROUTE : new Set();
|
|
13
|
+
const beforeRemoveAction = { ...action,
|
|
14
|
+
[VISITED_ROUTE_KEYS]: visitedRouteKeys
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
for (const route of removedRoutes) {
|
|
18
|
+
var _beforeRemoveListener;
|
|
19
|
+
|
|
20
|
+
if (visitedRouteKeys.has(route.key)) {
|
|
21
|
+
// Skip if we've already emitted this action for this screen
|
|
22
|
+
continue;
|
|
23
|
+
} // First, we need to check if any child screens want to prevent it
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
const isPrevented = (_beforeRemoveListener = beforeRemoveListeners[route.key]) === null || _beforeRemoveListener === void 0 ? void 0 : _beforeRemoveListener.call(beforeRemoveListeners, beforeRemoveAction);
|
|
27
|
+
|
|
28
|
+
if (isPrevented) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
visitedRouteKeys.add(route.key);
|
|
33
|
+
const event = emitter.emit({
|
|
34
|
+
type: 'beforeRemove',
|
|
35
|
+
target: route.key,
|
|
36
|
+
data: {
|
|
37
|
+
action: beforeRemoveAction
|
|
38
|
+
},
|
|
39
|
+
canPreventDefault: true
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
if (event.defaultPrevented) {
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return false;
|
|
48
|
+
};
|
|
49
|
+
export default function useOnPreventRemove({
|
|
50
|
+
getState,
|
|
51
|
+
emitter,
|
|
52
|
+
beforeRemoveListeners
|
|
53
|
+
}) {
|
|
54
|
+
const {
|
|
55
|
+
addKeyedListener
|
|
56
|
+
} = React.useContext(NavigationBuilderContext);
|
|
57
|
+
const route = React.useContext(NavigationRouteContext);
|
|
58
|
+
const routeKey = route === null || route === void 0 ? void 0 : route.key;
|
|
59
|
+
React.useEffect(() => {
|
|
60
|
+
if (routeKey) {
|
|
61
|
+
return addKeyedListener === null || addKeyedListener === void 0 ? void 0 : addKeyedListener('beforeRemove', routeKey, action => {
|
|
62
|
+
const state = getState();
|
|
63
|
+
return shouldPreventRemove(emitter, beforeRemoveListeners, state.routes, [], action);
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=useOnPreventRemove.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnPreventRemove.tsx"],"names":["React","NavigationBuilderContext","NavigationRouteContext","VISITED_ROUTE_KEYS","Symbol","shouldPreventRemove","emitter","beforeRemoveListeners","currentRoutes","nextRoutes","action","nextRouteKeys","map","route","key","removedRoutes","filter","includes","reverse","visitedRouteKeys","Set","beforeRemoveAction","has","isPrevented","add","event","emit","type","target","data","canPreventDefault","defaultPrevented","useOnPreventRemove","getState","addKeyedListener","useContext","routeKey","useEffect","state","routes"],"mappings":"AAIA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,wBAAP,MAEO,4BAFP;AAGA,OAAOC,sBAAP,MAAmC,0BAAnC;AAUA,MAAMC,kBAAkB,GAAGC,MAAM,CAAC,oBAAD,CAAjC;AAEA,OAAO,MAAMC,mBAAmB,GAAG,CACjCC,OADiC,EAEjCC,qBAFiC,EAGjCC,aAHiC,EAIjCC,UAJiC,EAKjCC,MALiC,KAM9B;AAAA;;AACH,QAAMC,aAAa,GAAGF,UAAU,CAACG,GAAX,CAAgBC,KAAD,IAAWA,KAAK,CAACC,GAAhC,CAAtB,CADG,CAGH;;AACA,QAAMC,aAAa,GAAGP,aAAa,CAChCQ,MADmB,CACXH,KAAD,IAAW,CAACF,aAAa,CAACM,QAAd,CAAuBJ,KAAK,CAACC,GAA7B,CADA,EAEnBI,OAFmB,EAAtB;AAIA,QAAMC,gBAA6B,GACjC;AADiC,2BAEjCT,MAAM,CAACP,kBAAD,CAF2B,yEAEH,IAAIiB,GAAJ,EAFhC;AAIA,QAAMC,kBAAkB,GAAG,EACzB,GAAGX,MADsB;AAEzB,KAACP,kBAAD,GAAsBgB;AAFG,GAA3B;;AAKA,OAAK,MAAMN,KAAX,IAAoBE,aAApB,EAAmC;AAAA;;AACjC,QAAII,gBAAgB,CAACG,GAAjB,CAAqBT,KAAK,CAACC,GAA3B,CAAJ,EAAqC;AACnC;AACA;AACD,KAJgC,CAMjC;;;AACA,UAAMS,WAAW,4BAAGhB,qBAAqB,CAACM,KAAK,CAACC,GAAP,CAAxB,0DAAG,2BAAAP,qBAAqB,EAAcc,kBAAd,CAAzC;;AAEA,QAAIE,WAAJ,EAAiB;AACf,aAAO,IAAP;AACD;;AAEDJ,IAAAA,gBAAgB,CAACK,GAAjB,CAAqBX,KAAK,CAACC,GAA3B;AAEA,UAAMW,KAAK,GAAGnB,OAAO,CAACoB,IAAR,CAAa;AACzBC,MAAAA,IAAI,EAAE,cADmB;AAEzBC,MAAAA,MAAM,EAAEf,KAAK,CAACC,GAFW;AAGzBe,MAAAA,IAAI,EAAE;AAAEnB,QAAAA,MAAM,EAAEW;AAAV,OAHmB;AAIzBS,MAAAA,iBAAiB,EAAE;AAJM,KAAb,CAAd;;AAOA,QAAIL,KAAK,CAACM,gBAAV,EAA4B;AAC1B,aAAO,IAAP;AACD;AACF;;AAED,SAAO,KAAP;AACD,CAnDM;AAqDP,eAAe,SAASC,kBAAT,CAA4B;AACzCC,EAAAA,QADyC;AAEzC3B,EAAAA,OAFyC;AAGzCC,EAAAA;AAHyC,CAA5B,EAIH;AACV,QAAM;AAAE2B,IAAAA;AAAF,MAAuBlC,KAAK,CAACmC,UAAN,CAAiBlC,wBAAjB,CAA7B;AACA,QAAMY,KAAK,GAAGb,KAAK,CAACmC,UAAN,CAAiBjC,sBAAjB,CAAd;AACA,QAAMkC,QAAQ,GAAGvB,KAAH,aAAGA,KAAH,uBAAGA,KAAK,CAAEC,GAAxB;AAEAd,EAAAA,KAAK,CAACqC,SAAN,CAAgB,MAAM;AACpB,QAAID,QAAJ,EAAc;AACZ,aAAOF,gBAAP,aAAOA,gBAAP,uBAAOA,gBAAgB,CAAG,cAAH,EAAmBE,QAAnB,EAA8B1B,MAAD,IAAY;AAC9D,cAAM4B,KAAK,GAAGL,QAAQ,EAAtB;AAEA,eAAO5B,mBAAmB,CACxBC,OADwB,EAExBC,qBAFwB,EAGxB+B,KAAK,CAACC,MAHkB,EAIxB,EAJwB,EAKxB7B,MALwB,CAA1B;AAOD,OAVsB,CAAvB;AAWD;AACF,GAdD,EAcG,CAACwB,gBAAD,EAAmB3B,qBAAnB,EAA0CD,OAA1C,EAAmD2B,QAAnD,EAA6DG,QAA7D,CAdH;AAeD","sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext, {\n ChildBeforeRemoveListener,\n} from './NavigationBuilderContext';\nimport NavigationRouteContext from './NavigationRouteContext';\nimport type { EventMapCore } from './types';\nimport type { NavigationEventEmitter } from './useEventEmitter';\n\ntype Options = {\n getState: () => NavigationState;\n emitter: NavigationEventEmitter<EventMapCore<any>>;\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>;\n};\n\nconst VISITED_ROUTE_KEYS = Symbol('VISITED_ROUTE_KEYS');\n\nexport const shouldPreventRemove = (\n emitter: NavigationEventEmitter<EventMapCore<any>>,\n beforeRemoveListeners: Record<string, ChildBeforeRemoveListener | undefined>,\n currentRoutes: { key: string }[],\n nextRoutes: { key?: string | undefined }[],\n action: NavigationAction\n) => {\n const nextRouteKeys = nextRoutes.map((route) => route.key);\n\n // Call these in reverse order so last screens handle the event first\n const removedRoutes = currentRoutes\n .filter((route) => !nextRouteKeys.includes(route.key))\n .reverse();\n\n const visitedRouteKeys: Set<string> =\n // @ts-expect-error: add this property to mark that we've already emitted this action\n action[VISITED_ROUTE_KEYS] ?? new Set<string>();\n\n const beforeRemoveAction = {\n ...action,\n [VISITED_ROUTE_KEYS]: visitedRouteKeys,\n };\n\n for (const route of removedRoutes) {\n if (visitedRouteKeys.has(route.key)) {\n // Skip if we've already emitted this action for this screen\n continue;\n }\n\n // First, we need to check if any child screens want to prevent it\n const isPrevented = beforeRemoveListeners[route.key]?.(beforeRemoveAction);\n\n if (isPrevented) {\n return true;\n }\n\n visitedRouteKeys.add(route.key);\n\n const event = emitter.emit({\n type: 'beforeRemove',\n target: route.key,\n data: { action: beforeRemoveAction },\n canPreventDefault: true,\n });\n\n if (event.defaultPrevented) {\n return true;\n }\n }\n\n return false;\n};\n\nexport default function useOnPreventRemove({\n getState,\n emitter,\n beforeRemoveListeners,\n}: Options) {\n const { addKeyedListener } = React.useContext(NavigationBuilderContext);\n const route = React.useContext(NavigationRouteContext);\n const routeKey = route?.key;\n\n React.useEffect(() => {\n if (routeKey) {\n return addKeyedListener?.('beforeRemove', routeKey, (action) => {\n const state = getState();\n\n return shouldPreventRemove(\n emitter,\n beforeRemoveListeners,\n state.routes,\n [],\n action\n );\n });\n }\n }, [addKeyedListener, beforeRemoveListeners, emitter, getState, routeKey]);\n}\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import NavigationBuilderContext from './NavigationBuilderContext';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hook to handle focus actions for a route.
|
|
6
|
+
* Focus action needs to be treated specially, coz when a nested route is focused,
|
|
7
|
+
* the parent navigators also needs to be focused.
|
|
8
|
+
*/
|
|
9
|
+
export default function useOnRouteFocus({
|
|
10
|
+
router,
|
|
11
|
+
getState,
|
|
12
|
+
key: sourceRouteKey,
|
|
13
|
+
setState
|
|
14
|
+
}) {
|
|
15
|
+
const {
|
|
16
|
+
onRouteFocus: onRouteFocusParent
|
|
17
|
+
} = React.useContext(NavigationBuilderContext);
|
|
18
|
+
return React.useCallback(key => {
|
|
19
|
+
const state = getState();
|
|
20
|
+
const result = router.getStateForRouteFocus(state, key);
|
|
21
|
+
|
|
22
|
+
if (result !== state) {
|
|
23
|
+
setState(result);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (onRouteFocusParent !== undefined && sourceRouteKey !== undefined) {
|
|
27
|
+
onRouteFocusParent(sourceRouteKey);
|
|
28
|
+
}
|
|
29
|
+
}, [getState, onRouteFocusParent, router, setState, sourceRouteKey]);
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=useOnRouteFocus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOnRouteFocus.tsx"],"names":["React","NavigationBuilderContext","useOnRouteFocus","router","getState","key","sourceRouteKey","setState","onRouteFocus","onRouteFocusParent","useContext","useCallback","state","result","getStateForRouteFocus","undefined"],"mappings":"AAKA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,wBAAP,MAAqC,4BAArC;;AASA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,eAAT,CAA0D;AACvEC,EAAAA,MADuE;AAEvEC,EAAAA,QAFuE;AAGvEC,EAAAA,GAAG,EAAEC,cAHkE;AAIvEC,EAAAA;AAJuE,CAA1D,EAKK;AAClB,QAAM;AAAEC,IAAAA,YAAY,EAAEC;AAAhB,MAAuCT,KAAK,CAACU,UAAN,CAC3CT,wBAD2C,CAA7C;AAIA,SAAOD,KAAK,CAACW,WAAN,CACJN,GAAD,IAAiB;AACf,UAAMO,KAAK,GAAGR,QAAQ,EAAtB;AACA,UAAMS,MAAM,GAAGV,MAAM,CAACW,qBAAP,CAA6BF,KAA7B,EAAoCP,GAApC,CAAf;;AAEA,QAAIQ,MAAM,KAAKD,KAAf,EAAsB;AACpBL,MAAAA,QAAQ,CAACM,MAAD,CAAR;AACD;;AAED,QAAIJ,kBAAkB,KAAKM,SAAvB,IAAoCT,cAAc,KAAKS,SAA3D,EAAsE;AACpEN,MAAAA,kBAAkB,CAACH,cAAD,CAAlB;AACD;AACF,GAZI,EAaL,CAACF,QAAD,EAAWK,kBAAX,EAA+BN,MAA/B,EAAuCI,QAAvC,EAAiDD,cAAjD,CAbK,CAAP;AAeD","sourcesContent":["import type {\n NavigationAction,\n NavigationState,\n Router,\n} from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext from './NavigationBuilderContext';\n\ntype Options<Action extends NavigationAction> = {\n router: Router<NavigationState, Action>;\n getState: () => NavigationState;\n setState: (state: NavigationState) => void;\n key?: string;\n};\n\n/**\n * Hook to handle focus actions for a route.\n * Focus action needs to be treated specially, coz when a nested route is focused,\n * the parent navigators also needs to be focused.\n */\nexport default function useOnRouteFocus<Action extends NavigationAction>({\n router,\n getState,\n key: sourceRouteKey,\n setState,\n}: Options<Action>) {\n const { onRouteFocus: onRouteFocusParent } = React.useContext(\n NavigationBuilderContext\n );\n\n return React.useCallback(\n (key: string) => {\n const state = getState();\n const result = router.getStateForRouteFocus(state, key);\n\n if (result !== state) {\n setState(result);\n }\n\n if (onRouteFocusParent !== undefined && sourceRouteKey !== undefined) {\n onRouteFocusParent(sourceRouteKey);\n }\n },\n [getState, onRouteFocusParent, router, setState, sourceRouteKey]\n );\n}\n"]}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import NavigationBuilderContext from './NavigationBuilderContext';
|
|
3
|
+
import NavigationStateContext from './NavigationStateContext';
|
|
4
|
+
export default function useOptionsGetters({
|
|
5
|
+
key,
|
|
6
|
+
options,
|
|
7
|
+
navigation
|
|
8
|
+
}) {
|
|
9
|
+
const optionsRef = React.useRef(options);
|
|
10
|
+
const optionsGettersFromChildRef = React.useRef({});
|
|
11
|
+
const {
|
|
12
|
+
onOptionsChange
|
|
13
|
+
} = React.useContext(NavigationBuilderContext);
|
|
14
|
+
const {
|
|
15
|
+
addOptionsGetter: parentAddOptionsGetter
|
|
16
|
+
} = React.useContext(NavigationStateContext);
|
|
17
|
+
const optionsChangeListener = React.useCallback(() => {
|
|
18
|
+
var _navigation$isFocused;
|
|
19
|
+
|
|
20
|
+
const isFocused = (_navigation$isFocused = navigation === null || navigation === void 0 ? void 0 : navigation.isFocused()) !== null && _navigation$isFocused !== void 0 ? _navigation$isFocused : true;
|
|
21
|
+
const hasChildren = Object.keys(optionsGettersFromChildRef.current).length;
|
|
22
|
+
|
|
23
|
+
if (isFocused && !hasChildren) {
|
|
24
|
+
var _optionsRef$current;
|
|
25
|
+
|
|
26
|
+
onOptionsChange((_optionsRef$current = optionsRef.current) !== null && _optionsRef$current !== void 0 ? _optionsRef$current : {});
|
|
27
|
+
}
|
|
28
|
+
}, [navigation, onOptionsChange]);
|
|
29
|
+
React.useEffect(() => {
|
|
30
|
+
optionsRef.current = options;
|
|
31
|
+
optionsChangeListener();
|
|
32
|
+
return navigation === null || navigation === void 0 ? void 0 : navigation.addListener('focus', optionsChangeListener);
|
|
33
|
+
}, [navigation, options, optionsChangeListener]);
|
|
34
|
+
const getOptionsFromListener = React.useCallback(() => {
|
|
35
|
+
for (let key in optionsGettersFromChildRef.current) {
|
|
36
|
+
if (optionsGettersFromChildRef.current.hasOwnProperty(key)) {
|
|
37
|
+
var _optionsGettersFromCh, _optionsGettersFromCh2;
|
|
38
|
+
|
|
39
|
+
const result = (_optionsGettersFromCh = (_optionsGettersFromCh2 = optionsGettersFromChildRef.current)[key]) === null || _optionsGettersFromCh === void 0 ? void 0 : _optionsGettersFromCh.call(_optionsGettersFromCh2); // null means unfocused route
|
|
40
|
+
|
|
41
|
+
if (result !== null) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return null;
|
|
48
|
+
}, []);
|
|
49
|
+
const getCurrentOptions = React.useCallback(() => {
|
|
50
|
+
var _navigation$isFocused2;
|
|
51
|
+
|
|
52
|
+
const isFocused = (_navigation$isFocused2 = navigation === null || navigation === void 0 ? void 0 : navigation.isFocused()) !== null && _navigation$isFocused2 !== void 0 ? _navigation$isFocused2 : true;
|
|
53
|
+
|
|
54
|
+
if (!isFocused) {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const optionsFromListener = getOptionsFromListener();
|
|
59
|
+
|
|
60
|
+
if (optionsFromListener !== null) {
|
|
61
|
+
return optionsFromListener;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return optionsRef.current;
|
|
65
|
+
}, [navigation, getOptionsFromListener]);
|
|
66
|
+
React.useEffect(() => {
|
|
67
|
+
return parentAddOptionsGetter === null || parentAddOptionsGetter === void 0 ? void 0 : parentAddOptionsGetter(key, getCurrentOptions);
|
|
68
|
+
}, [getCurrentOptions, parentAddOptionsGetter, key]);
|
|
69
|
+
const addOptionsGetter = React.useCallback((key, getter) => {
|
|
70
|
+
optionsGettersFromChildRef.current[key] = getter;
|
|
71
|
+
optionsChangeListener();
|
|
72
|
+
return () => {
|
|
73
|
+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
|
74
|
+
delete optionsGettersFromChildRef.current[key];
|
|
75
|
+
optionsChangeListener();
|
|
76
|
+
};
|
|
77
|
+
}, [optionsChangeListener]);
|
|
78
|
+
return {
|
|
79
|
+
addOptionsGetter,
|
|
80
|
+
getCurrentOptions
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=useOptionsGetters.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useOptionsGetters.tsx"],"names":["React","NavigationBuilderContext","NavigationStateContext","useOptionsGetters","key","options","navigation","optionsRef","useRef","optionsGettersFromChildRef","onOptionsChange","useContext","addOptionsGetter","parentAddOptionsGetter","optionsChangeListener","useCallback","isFocused","hasChildren","Object","keys","current","length","useEffect","addListener","getOptionsFromListener","hasOwnProperty","result","getCurrentOptions","optionsFromListener","getter"],"mappings":"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","sourcesContent":["import type { ParamListBase } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationBuilderContext from './NavigationBuilderContext';\nimport NavigationStateContext from './NavigationStateContext';\nimport type { NavigationProp } from './types';\n\ntype Options = {\n key?: string;\n navigation?: NavigationProp<ParamListBase>;\n options?: object | undefined;\n};\n\nexport default function useOptionsGetters({\n key,\n options,\n navigation,\n}: Options) {\n const optionsRef = React.useRef<object | undefined>(options);\n const optionsGettersFromChildRef = React.useRef<\n Record<string, () => object | undefined | null>\n >({});\n\n const { onOptionsChange } = React.useContext(NavigationBuilderContext);\n const { addOptionsGetter: parentAddOptionsGetter } = React.useContext(\n NavigationStateContext\n );\n\n const optionsChangeListener = React.useCallback(() => {\n const isFocused = navigation?.isFocused() ?? true;\n const hasChildren = Object.keys(optionsGettersFromChildRef.current).length;\n\n if (isFocused && !hasChildren) {\n onOptionsChange(optionsRef.current ?? {});\n }\n }, [navigation, onOptionsChange]);\n\n React.useEffect(() => {\n optionsRef.current = options;\n optionsChangeListener();\n\n return navigation?.addListener('focus', optionsChangeListener);\n }, [navigation, options, optionsChangeListener]);\n\n const getOptionsFromListener = React.useCallback(() => {\n for (let key in optionsGettersFromChildRef.current) {\n if (optionsGettersFromChildRef.current.hasOwnProperty(key)) {\n const result = optionsGettersFromChildRef.current[key]?.();\n\n // null means unfocused route\n if (result !== null) {\n return result;\n }\n }\n }\n\n return null;\n }, []);\n\n const getCurrentOptions = React.useCallback(() => {\n const isFocused = navigation?.isFocused() ?? true;\n\n if (!isFocused) {\n return null;\n }\n\n const optionsFromListener = getOptionsFromListener();\n\n if (optionsFromListener !== null) {\n return optionsFromListener;\n }\n\n return optionsRef.current;\n }, [navigation, getOptionsFromListener]);\n\n React.useEffect(() => {\n return parentAddOptionsGetter?.(key!, getCurrentOptions);\n }, [getCurrentOptions, parentAddOptionsGetter, key]);\n\n const addOptionsGetter = React.useCallback(\n (key: string, getter: () => object | undefined | null) => {\n optionsGettersFromChildRef.current[key] = getter;\n optionsChangeListener();\n\n return () => {\n // eslint-disable-next-line @typescript-eslint/no-dynamic-delete\n delete optionsGettersFromChildRef.current[key];\n optionsChangeListener();\n };\n },\n [optionsChangeListener]\n );\n\n return {\n addOptionsGetter,\n getCurrentOptions,\n };\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import * as React from 'react';
|
|
3
|
+
import { SingleNavigatorContext } from './EnsureSingleNavigator';
|
|
4
|
+
/**
|
|
5
|
+
* Register a navigator in the parent context (either a navigation container or a screen).
|
|
6
|
+
* This is used to prevent multiple navigators under a single container or screen.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export default function useRegisterNavigator() {
|
|
10
|
+
const [key] = React.useState(() => nanoid());
|
|
11
|
+
const container = React.useContext(SingleNavigatorContext);
|
|
12
|
+
|
|
13
|
+
if (container === undefined) {
|
|
14
|
+
throw new Error("Couldn't register the navigator. Have you wrapped your app with 'NavigationContainer'?\n\nThis can also happen if there are multiple copies of '@react-navigation' packages installed.");
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
React.useEffect(() => {
|
|
18
|
+
const {
|
|
19
|
+
register,
|
|
20
|
+
unregister
|
|
21
|
+
} = container;
|
|
22
|
+
register(key);
|
|
23
|
+
return () => unregister(key);
|
|
24
|
+
}, [container, key]);
|
|
25
|
+
return key;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=useRegisterNavigator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useRegisterNavigator.tsx"],"names":["nanoid","React","SingleNavigatorContext","useRegisterNavigator","key","useState","container","useContext","undefined","Error","useEffect","register","unregister"],"mappings":"AAAA,SAASA,MAAT,QAAuB,mBAAvB;AACA,OAAO,KAAKC,KAAZ,MAAuB,OAAvB;AAEA,SAASC,sBAAT,QAAuC,yBAAvC;AAEA;AACA;AACA;AACA;;AACA,eAAe,SAASC,oBAAT,GAAgC;AAC7C,QAAM,CAACC,GAAD,IAAQH,KAAK,CAACI,QAAN,CAAe,MAAML,MAAM,EAA3B,CAAd;AACA,QAAMM,SAAS,GAAGL,KAAK,CAACM,UAAN,CAAiBL,sBAAjB,CAAlB;;AAEA,MAAII,SAAS,KAAKE,SAAlB,EAA6B;AAC3B,UAAM,IAAIC,KAAJ,CACJ,wLADI,CAAN;AAGD;;AAEDR,EAAAA,KAAK,CAACS,SAAN,CAAgB,MAAM;AACpB,UAAM;AAAEC,MAAAA,QAAF;AAAYC,MAAAA;AAAZ,QAA2BN,SAAjC;AAEAK,IAAAA,QAAQ,CAACP,GAAD,CAAR;AAEA,WAAO,MAAMQ,UAAU,CAACR,GAAD,CAAvB;AACD,GAND,EAMG,CAACE,SAAD,EAAYF,GAAZ,CANH;AAQA,SAAOA,GAAP;AACD","sourcesContent":["import { nanoid } from 'nanoid/non-secure';\nimport * as React from 'react';\n\nimport { SingleNavigatorContext } from './EnsureSingleNavigator';\n\n/**\n * Register a navigator in the parent context (either a navigation container or a screen).\n * This is used to prevent multiple navigators under a single container or screen.\n */\nexport default function useRegisterNavigator() {\n const [key] = React.useState(() => nanoid());\n const container = React.useContext(SingleNavigatorContext);\n\n if (container === undefined) {\n throw new Error(\n \"Couldn't register the navigator. Have you wrapped your app with 'NavigationContainer'?\\n\\nThis can also happen if there are multiple copies of '@react-navigation' packages installed.\"\n );\n }\n\n React.useEffect(() => {\n const { register, unregister } = container;\n\n register(key);\n\n return () => unregister(key);\n }, [container, key]);\n\n return key;\n}\n"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import NavigationRouteContext from './NavigationRouteContext';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Hook to access the route prop of the parent screen anywhere.
|
|
6
|
+
*
|
|
7
|
+
* @returns Route prop of the parent screen.
|
|
8
|
+
*/
|
|
9
|
+
export default function useRoute() {
|
|
10
|
+
const route = React.useContext(NavigationRouteContext);
|
|
11
|
+
|
|
12
|
+
if (route === undefined) {
|
|
13
|
+
throw new Error("Couldn't find a route object. Is your component inside a screen in a navigator?");
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return route;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=useRoute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useRoute.tsx"],"names":["React","NavigationRouteContext","useRoute","route","useContext","undefined","Error"],"mappings":"AACA,OAAO,KAAKA,KAAZ,MAAuB,OAAvB;AAEA,OAAOC,sBAAP,MAAmC,0BAAnC;;AAGA;AACA;AACA;AACA;AACA;AACA,eAAe,SAASC,QAAT,GAA2D;AACxE,QAAMC,KAAK,GAAGH,KAAK,CAACI,UAAN,CAAiBH,sBAAjB,CAAd;;AAEA,MAAIE,KAAK,KAAKE,SAAd,EAAyB;AACvB,UAAM,IAAIC,KAAJ,CACJ,iFADI,CAAN;AAGD;;AAED,SAAOH,KAAP;AACD","sourcesContent":["import type { ParamListBase } from '@react-navigation/routers';\nimport * as React from 'react';\n\nimport NavigationRouteContext from './NavigationRouteContext';\nimport type { RouteProp } from './types';\n\n/**\n * Hook to access the route prop of the parent screen anywhere.\n *\n * @returns Route prop of the parent screen.\n */\nexport default function useRoute<T extends RouteProp<ParamListBase>>(): T {\n const route = React.useContext(NavigationRouteContext);\n\n if (route === undefined) {\n throw new Error(\n \"Couldn't find a route object. Is your component inside a screen in a navigator?\"\n );\n }\n\n return route as T;\n}\n"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utilites such as `getFocusedRouteNameFromRoute` need to access state.
|
|
5
|
+
* So we need a way to suppress the warning for those use cases.
|
|
6
|
+
* This is fine since they are internal utilities and this is not public API.
|
|
7
|
+
*/
|
|
8
|
+
export const CHILD_STATE = Symbol('CHILD_STATE');
|
|
9
|
+
/**
|
|
10
|
+
* Hook to cache route props for each screen in the navigator.
|
|
11
|
+
* This lets add warnings and modifications to the route object but keep references between renders.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export default function useRouteCache(routes) {
|
|
15
|
+
// Cache object which holds route objects for each screen
|
|
16
|
+
const cache = React.useMemo(() => ({
|
|
17
|
+
current: new Map()
|
|
18
|
+
}), []);
|
|
19
|
+
|
|
20
|
+
if (process.env.NODE_ENV === 'production') {
|
|
21
|
+
// We don't want the overhead of creating extra maps every render in prod
|
|
22
|
+
return routes;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
cache.current = routes.reduce((acc, route) => {
|
|
26
|
+
const previous = cache.current.get(route);
|
|
27
|
+
|
|
28
|
+
if (previous) {
|
|
29
|
+
// If a cached route object already exists, reuse it
|
|
30
|
+
acc.set(route, previous);
|
|
31
|
+
} else {
|
|
32
|
+
const {
|
|
33
|
+
state,
|
|
34
|
+
...proxy
|
|
35
|
+
} = route;
|
|
36
|
+
Object.defineProperty(proxy, CHILD_STATE, {
|
|
37
|
+
enumerable: false,
|
|
38
|
+
value: state
|
|
39
|
+
});
|
|
40
|
+
acc.set(route, proxy);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return acc;
|
|
44
|
+
}, new Map());
|
|
45
|
+
return Array.from(cache.current.values());
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=useRouteCache.js.map
|