@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.
Files changed (327) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +38 -0
  3. package/lib/commonjs/BaseNavigationContainer.js +393 -0
  4. package/lib/commonjs/BaseNavigationContainer.js.map +1 -0
  5. package/lib/commonjs/CurrentRenderContext.js +21 -0
  6. package/lib/commonjs/CurrentRenderContext.js.map +1 -0
  7. package/lib/commonjs/EnsureSingleNavigator.js +53 -0
  8. package/lib/commonjs/EnsureSingleNavigator.js.map +1 -0
  9. package/lib/commonjs/Group.js +15 -0
  10. package/lib/commonjs/Group.js.map +1 -0
  11. package/lib/commonjs/NavigationBuilderContext.js +23 -0
  12. package/lib/commonjs/NavigationBuilderContext.js.map +1 -0
  13. package/lib/commonjs/NavigationContainerRefContext.js +20 -0
  14. package/lib/commonjs/NavigationContainerRefContext.js.map +1 -0
  15. package/lib/commonjs/NavigationContext.js +20 -0
  16. package/lib/commonjs/NavigationContext.js.map +1 -0
  17. package/lib/commonjs/NavigationHelpersContext.js +21 -0
  18. package/lib/commonjs/NavigationHelpersContext.js.map +1 -0
  19. package/lib/commonjs/NavigationRouteContext.js +20 -0
  20. package/lib/commonjs/NavigationRouteContext.js.map +1 -0
  21. package/lib/commonjs/NavigationStateContext.js +42 -0
  22. package/lib/commonjs/NavigationStateContext.js.map +1 -0
  23. package/lib/commonjs/SceneView.js +97 -0
  24. package/lib/commonjs/SceneView.js.map +1 -0
  25. package/lib/commonjs/Screen.js +15 -0
  26. package/lib/commonjs/Screen.js.map +1 -0
  27. package/lib/commonjs/StaticContainer.js +43 -0
  28. package/lib/commonjs/StaticContainer.js.map +1 -0
  29. package/lib/commonjs/UnhandledActionContext.js +17 -0
  30. package/lib/commonjs/UnhandledActionContext.js.map +1 -0
  31. package/lib/commonjs/checkDuplicateRouteNames.js +31 -0
  32. package/lib/commonjs/checkDuplicateRouteNames.js.map +1 -0
  33. package/lib/commonjs/checkSerializable.js +59 -0
  34. package/lib/commonjs/checkSerializable.js.map +1 -0
  35. package/lib/commonjs/createNavigationContainerRef.js +82 -0
  36. package/lib/commonjs/createNavigationContainerRef.js.map +1 -0
  37. package/lib/commonjs/createNavigatorFactory.js +34 -0
  38. package/lib/commonjs/createNavigatorFactory.js.map +1 -0
  39. package/lib/commonjs/findFocusedRoute.js +22 -0
  40. package/lib/commonjs/findFocusedRoute.js.map +1 -0
  41. package/lib/commonjs/fromEntries.js +19 -0
  42. package/lib/commonjs/fromEntries.js.map +1 -0
  43. package/lib/commonjs/getActionFromState.js +99 -0
  44. package/lib/commonjs/getActionFromState.js.map +1 -0
  45. package/lib/commonjs/getFocusedRouteNameFromRoute.js +23 -0
  46. package/lib/commonjs/getFocusedRouteNameFromRoute.js.map +1 -0
  47. package/lib/commonjs/getPathFromState.js +238 -0
  48. package/lib/commonjs/getPathFromState.js.map +1 -0
  49. package/lib/commonjs/getStateFromPath.js +450 -0
  50. package/lib/commonjs/getStateFromPath.js.map +1 -0
  51. package/lib/commonjs/index.js +227 -0
  52. package/lib/commonjs/index.js.map +1 -0
  53. package/lib/commonjs/isArrayEqual.js +15 -0
  54. package/lib/commonjs/isArrayEqual.js.map +1 -0
  55. package/lib/commonjs/types.js +18 -0
  56. package/lib/commonjs/types.js.map +1 -0
  57. package/lib/commonjs/useChildListeners.js +38 -0
  58. package/lib/commonjs/useChildListeners.js.map +1 -0
  59. package/lib/commonjs/useComponent.js +36 -0
  60. package/lib/commonjs/useComponent.js.map +1 -0
  61. package/lib/commonjs/useCurrentRender.js +33 -0
  62. package/lib/commonjs/useCurrentRender.js.map +1 -0
  63. package/lib/commonjs/useDescriptors.js +141 -0
  64. package/lib/commonjs/useDescriptors.js.map +1 -0
  65. package/lib/commonjs/useEventEmitter.js +118 -0
  66. package/lib/commonjs/useEventEmitter.js.map +1 -0
  67. package/lib/commonjs/useFocusEffect.js +97 -0
  68. package/lib/commonjs/useFocusEffect.js.map +1 -0
  69. package/lib/commonjs/useFocusEvents.js +77 -0
  70. package/lib/commonjs/useFocusEvents.js.map +1 -0
  71. package/lib/commonjs/useFocusedListenersChildrenAdapter.js +57 -0
  72. package/lib/commonjs/useFocusedListenersChildrenAdapter.js.map +1 -0
  73. package/lib/commonjs/useIsFocused.js +47 -0
  74. package/lib/commonjs/useIsFocused.js.map +1 -0
  75. package/lib/commonjs/useKeyedChildListeners.js +35 -0
  76. package/lib/commonjs/useKeyedChildListeners.js.map +1 -0
  77. package/lib/commonjs/useNavigation.js +36 -0
  78. package/lib/commonjs/useNavigation.js.map +1 -0
  79. package/lib/commonjs/useNavigationBuilder.js +480 -0
  80. package/lib/commonjs/useNavigationBuilder.js.map +1 -0
  81. package/lib/commonjs/useNavigationCache.js +122 -0
  82. package/lib/commonjs/useNavigationCache.js.map +1 -0
  83. package/lib/commonjs/useNavigationContainerRef.js +27 -0
  84. package/lib/commonjs/useNavigationContainerRef.js.map +1 -0
  85. package/lib/commonjs/useNavigationHelpers.js +77 -0
  86. package/lib/commonjs/useNavigationHelpers.js.map +1 -0
  87. package/lib/commonjs/useNavigationState.js +41 -0
  88. package/lib/commonjs/useNavigationState.js.map +1 -0
  89. package/lib/commonjs/useOnAction.js +118 -0
  90. package/lib/commonjs/useOnAction.js.map +1 -0
  91. package/lib/commonjs/useOnGetState.js +60 -0
  92. package/lib/commonjs/useOnGetState.js.map +1 -0
  93. package/lib/commonjs/useOnPreventRemove.js +89 -0
  94. package/lib/commonjs/useOnPreventRemove.js.map +1 -0
  95. package/lib/commonjs/useOnRouteFocus.js +45 -0
  96. package/lib/commonjs/useOnRouteFocus.js.map +1 -0
  97. package/lib/commonjs/useOptionsGetters.js +99 -0
  98. package/lib/commonjs/useOptionsGetters.js.map +1 -0
  99. package/lib/commonjs/useRegisterNavigator.js +40 -0
  100. package/lib/commonjs/useRegisterNavigator.js.map +1 -0
  101. package/lib/commonjs/useRoute.js +32 -0
  102. package/lib/commonjs/useRoute.js.map +1 -0
  103. package/lib/commonjs/useRouteCache.js +61 -0
  104. package/lib/commonjs/useRouteCache.js.map +1 -0
  105. package/lib/commonjs/useScheduleUpdate.js +44 -0
  106. package/lib/commonjs/useScheduleUpdate.js.map +1 -0
  107. package/lib/commonjs/useSyncState.js +76 -0
  108. package/lib/commonjs/useSyncState.js.map +1 -0
  109. package/lib/commonjs/validatePathConfig.js +31 -0
  110. package/lib/commonjs/validatePathConfig.js.map +1 -0
  111. package/lib/module/BaseNavigationContainer.js +360 -0
  112. package/lib/module/BaseNavigationContainer.js.map +1 -0
  113. package/lib/module/CurrentRenderContext.js +9 -0
  114. package/lib/module/CurrentRenderContext.js.map +1 -0
  115. package/lib/module/EnsureSingleNavigator.js +38 -0
  116. package/lib/module/EnsureSingleNavigator.js.map +1 -0
  117. package/lib/module/Group.js +8 -0
  118. package/lib/module/Group.js.map +1 -0
  119. package/lib/module/NavigationBuilderContext.js +11 -0
  120. package/lib/module/NavigationBuilderContext.js.map +1 -0
  121. package/lib/module/NavigationContainerRefContext.js +8 -0
  122. package/lib/module/NavigationContainerRefContext.js.map +1 -0
  123. package/lib/module/NavigationContext.js +8 -0
  124. package/lib/module/NavigationContext.js.map +1 -0
  125. package/lib/module/NavigationHelpersContext.js +9 -0
  126. package/lib/module/NavigationHelpersContext.js.map +1 -0
  127. package/lib/module/NavigationRouteContext.js +8 -0
  128. package/lib/module/NavigationRouteContext.js.map +1 -0
  129. package/lib/module/NavigationStateContext.js +27 -0
  130. package/lib/module/NavigationStateContext.js.map +1 -0
  131. package/lib/module/SceneView.js +80 -0
  132. package/lib/module/SceneView.js.map +1 -0
  133. package/lib/module/Screen.js +8 -0
  134. package/lib/module/Screen.js.map +1 -0
  135. package/lib/module/StaticContainer.js +30 -0
  136. package/lib/module/StaticContainer.js.map +1 -0
  137. package/lib/module/UnhandledActionContext.js +4 -0
  138. package/lib/module/UnhandledActionContext.js.map +1 -0
  139. package/lib/module/checkDuplicateRouteNames.js +24 -0
  140. package/lib/module/checkDuplicateRouteNames.js.map +1 -0
  141. package/lib/module/checkSerializable.js +52 -0
  142. package/lib/module/checkSerializable.js.map +1 -0
  143. package/lib/module/createNavigationContainerRef.js +71 -0
  144. package/lib/module/createNavigationContainerRef.js.map +1 -0
  145. package/lib/module/createNavigatorFactory.js +24 -0
  146. package/lib/module/createNavigatorFactory.js.map +1 -0
  147. package/lib/module/findFocusedRoute.js +15 -0
  148. package/lib/module/findFocusedRoute.js.map +1 -0
  149. package/lib/module/fromEntries.js +12 -0
  150. package/lib/module/fromEntries.js.map +1 -0
  151. package/lib/module/getActionFromState.js +92 -0
  152. package/lib/module/getActionFromState.js.map +1 -0
  153. package/lib/module/getFocusedRouteNameFromRoute.js +15 -0
  154. package/lib/module/getFocusedRouteNameFromRoute.js.map +1 -0
  155. package/lib/module/getPathFromState.js +223 -0
  156. package/lib/module/getPathFromState.js.map +1 -0
  157. package/lib/module/getStateFromPath.js +434 -0
  158. package/lib/module/getStateFromPath.js.map +1 -0
  159. package/lib/module/index.js +24 -0
  160. package/lib/module/index.js.map +1 -0
  161. package/lib/module/isArrayEqual.js +8 -0
  162. package/lib/module/isArrayEqual.js.map +1 -0
  163. package/lib/module/types.js +9 -0
  164. package/lib/module/types.js.map +1 -0
  165. package/lib/module/useChildListeners.js +27 -0
  166. package/lib/module/useChildListeners.js.map +1 -0
  167. package/lib/module/useComponent.js +24 -0
  168. package/lib/module/useComponent.js.map +1 -0
  169. package/lib/module/useCurrentRender.js +19 -0
  170. package/lib/module/useCurrentRender.js.map +1 -0
  171. package/lib/module/useDescriptors.js +122 -0
  172. package/lib/module/useDescriptors.js.map +1 -0
  173. package/lib/module/useEventEmitter.js +107 -0
  174. package/lib/module/useEventEmitter.js.map +1 -0
  175. package/lib/module/useFocusEffect.js +83 -0
  176. package/lib/module/useFocusEffect.js.map +1 -0
  177. package/lib/module/useFocusEvents.js +63 -0
  178. package/lib/module/useFocusEvents.js.map +1 -0
  179. package/lib/module/useFocusedListenersChildrenAdapter.js +43 -0
  180. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -0
  181. package/lib/module/useIsFocused.js +34 -0
  182. package/lib/module/useIsFocused.js.map +1 -0
  183. package/lib/module/useKeyedChildListeners.js +24 -0
  184. package/lib/module/useKeyedChildListeners.js.map +1 -0
  185. package/lib/module/useNavigation.js +21 -0
  186. package/lib/module/useNavigation.js.map +1 -0
  187. package/lib/module/useNavigationBuilder.js +443 -0
  188. package/lib/module/useNavigationBuilder.js.map +1 -0
  189. package/lib/module/useNavigationCache.js +107 -0
  190. package/lib/module/useNavigationCache.js.map +1 -0
  191. package/lib/module/useNavigationContainerRef.js +12 -0
  192. package/lib/module/useNavigationContainerRef.js.map +1 -0
  193. package/lib/module/useNavigationHelpers.js +59 -0
  194. package/lib/module/useNavigationHelpers.js.map +1 -0
  195. package/lib/module/useNavigationState.js +27 -0
  196. package/lib/module/useNavigationState.js.map +1 -0
  197. package/lib/module/useOnAction.js +103 -0
  198. package/lib/module/useOnAction.js.map +1 -0
  199. package/lib/module/useOnGetState.js +43 -0
  200. package/lib/module/useOnGetState.js.map +1 -0
  201. package/lib/module/useOnPreventRemove.js +68 -0
  202. package/lib/module/useOnPreventRemove.js.map +1 -0
  203. package/lib/module/useOnRouteFocus.js +31 -0
  204. package/lib/module/useOnRouteFocus.js.map +1 -0
  205. package/lib/module/useOptionsGetters.js +83 -0
  206. package/lib/module/useOptionsGetters.js.map +1 -0
  207. package/lib/module/useRegisterNavigator.js +27 -0
  208. package/lib/module/useRegisterNavigator.js.map +1 -0
  209. package/lib/module/useRoute.js +18 -0
  210. package/lib/module/useRoute.js.map +1 -0
  211. package/lib/module/useRouteCache.js +47 -0
  212. package/lib/module/useRouteCache.js.map +1 -0
  213. package/lib/module/useScheduleUpdate.js +29 -0
  214. package/lib/module/useScheduleUpdate.js.map +1 -0
  215. package/lib/module/useSyncState.js +64 -0
  216. package/lib/module/useSyncState.js.map +1 -0
  217. package/lib/module/validatePathConfig.js +24 -0
  218. package/lib/module/validatePathConfig.js.map +1 -0
  219. package/lib/typescript/src/BaseNavigationContainer.d.ts +14 -0
  220. package/lib/typescript/src/CurrentRenderContext.d.ts +9 -0
  221. package/lib/typescript/src/EnsureSingleNavigator.d.ts +13 -0
  222. package/lib/typescript/src/Group.d.ts +6 -0
  223. package/lib/typescript/src/NavigationBuilderContext.d.ts +34 -0
  224. package/lib/typescript/src/NavigationContainerRefContext.d.ts +8 -0
  225. package/lib/typescript/src/NavigationContext.d.ts +24 -0
  226. package/lib/typescript/src/NavigationHelpersContext.d.ts +9 -0
  227. package/lib/typescript/src/NavigationRouteContext.d.ts +7 -0
  228. package/lib/typescript/src/NavigationStateContext.d.ts +45 -0
  229. package/lib/typescript/src/SceneView.d.ts +21 -0
  230. package/lib/typescript/src/Screen.d.ts +6 -0
  231. package/lib/typescript/src/StaticContainer.d.ts +7 -0
  232. package/lib/typescript/src/UnhandledActionContext.d.ts +4 -0
  233. package/lib/typescript/src/checkDuplicateRouteNames.d.ts +2 -0
  234. package/lib/typescript/src/checkSerializable.d.ts +9 -0
  235. package/lib/typescript/src/createNavigationContainerRef.d.ts +3 -0
  236. package/lib/typescript/src/createNavigatorFactory.d.ts +11 -0
  237. package/lib/typescript/src/findFocusedRoute.d.ts +22 -0
  238. package/lib/typescript/src/fromEntries.d.ts +1 -0
  239. package/lib/typescript/src/getActionFromState.d.ts +16 -0
  240. package/lib/typescript/src/getFocusedRouteNameFromRoute.d.ts +2 -0
  241. package/lib/typescript/src/getPathFromState.d.ts +38 -0
  242. package/lib/typescript/src/getStateFromPath.d.ts +32 -0
  243. package/lib/typescript/src/index.d.ts +23 -0
  244. package/lib/typescript/src/isArrayEqual.d.ts +5 -0
  245. package/lib/typescript/src/types.d.ts +485 -0
  246. package/lib/typescript/src/useChildListeners.d.ts +11 -0
  247. package/lib/typescript/src/useComponent.d.ts +2 -0
  248. package/lib/typescript/src/useCurrentRender.d.ts +13 -0
  249. package/lib/typescript/src/useDescriptors.d.ts +89 -0
  250. package/lib/typescript/src/useEventEmitter.d.ts +8 -0
  251. package/lib/typescript/src/useFocusEffect.d.ts +10 -0
  252. package/lib/typescript/src/useFocusEvents.d.ts +12 -0
  253. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +12 -0
  254. package/lib/typescript/src/useIsFocused.d.ts +5 -0
  255. package/lib/typescript/src/useKeyedChildListeners.d.ts +11 -0
  256. package/lib/typescript/src/useNavigation.d.ts +7 -0
  257. package/lib/typescript/src/useNavigationBuilder.d.ts +223 -0
  258. package/lib/typescript/src/useNavigationCache.d.ts +19 -0
  259. package/lib/typescript/src/useNavigationContainerRef.d.ts +2 -0
  260. package/lib/typescript/src/useNavigationHelpers.d.ts +174 -0
  261. package/lib/typescript/src/useNavigationState.d.ts +9 -0
  262. package/lib/typescript/src/useOnAction.d.ts +25 -0
  263. package/lib/typescript/src/useOnGetState.d.ts +8 -0
  264. package/lib/typescript/src/useOnPreventRemove.d.ts +16 -0
  265. package/lib/typescript/src/useOnRouteFocus.d.ts +14 -0
  266. package/lib/typescript/src/useOptionsGetters.d.ts +12 -0
  267. package/lib/typescript/src/useRegisterNavigator.d.ts +5 -0
  268. package/lib/typescript/src/useRoute.d.ts +8 -0
  269. package/lib/typescript/src/useRouteCache.d.ts +13 -0
  270. package/lib/typescript/src/useScheduleUpdate.d.ts +13 -0
  271. package/lib/typescript/src/useSyncState.d.ts +4 -0
  272. package/lib/typescript/src/validatePathConfig.d.ts +1 -0
  273. package/package.json +73 -0
  274. package/src/BaseNavigationContainer.tsx +453 -0
  275. package/src/CurrentRenderContext.tsx +10 -0
  276. package/src/EnsureSingleNavigator.tsx +53 -0
  277. package/src/Group.tsx +14 -0
  278. package/src/NavigationBuilderContext.tsx +70 -0
  279. package/src/NavigationContainerRefContext.tsx +14 -0
  280. package/src/NavigationContext.tsx +12 -0
  281. package/src/NavigationHelpersContext.tsx +13 -0
  282. package/src/NavigationRouteContext.tsx +10 -0
  283. package/src/NavigationStateContext.tsx +39 -0
  284. package/src/SceneView.tsx +134 -0
  285. package/src/Screen.tsx +17 -0
  286. package/src/StaticContainer.tsx +29 -0
  287. package/src/UnhandledActionContext.tsx +9 -0
  288. package/src/checkDuplicateRouteNames.tsx +33 -0
  289. package/src/checkSerializable.tsx +74 -0
  290. package/src/createNavigationContainerRef.tsx +97 -0
  291. package/src/createNavigatorFactory.tsx +40 -0
  292. package/src/findFocusedRoute.tsx +13 -0
  293. package/src/fromEntries.tsx +13 -0
  294. package/src/getActionFromState.tsx +154 -0
  295. package/src/getFocusedRouteNameFromRoute.tsx +28 -0
  296. package/src/getPathFromState.tsx +297 -0
  297. package/src/getStateFromPath.tsx +575 -0
  298. package/src/index.tsx +23 -0
  299. package/src/isArrayEqual.tsx +7 -0
  300. package/src/types.tsx +645 -0
  301. package/src/useChildListeners.tsx +37 -0
  302. package/src/useComponent.tsx +30 -0
  303. package/src/useCurrentRender.tsx +35 -0
  304. package/src/useDescriptors.tsx +229 -0
  305. package/src/useEventEmitter.tsx +130 -0
  306. package/src/useFocusEffect.tsx +112 -0
  307. package/src/useFocusEvents.tsx +73 -0
  308. package/src/useFocusedListenersChildrenAdapter.tsx +47 -0
  309. package/src/useIsFocused.tsx +43 -0
  310. package/src/useKeyedChildListeners.tsx +40 -0
  311. package/src/useNavigation.tsx +26 -0
  312. package/src/useNavigationBuilder.tsx +640 -0
  313. package/src/useNavigationCache.tsx +159 -0
  314. package/src/useNavigationContainerRef.tsx +17 -0
  315. package/src/useNavigationHelpers.tsx +98 -0
  316. package/src/useNavigationState.tsx +41 -0
  317. package/src/useOnAction.tsx +166 -0
  318. package/src/useOnGetState.tsx +47 -0
  319. package/src/useOnPreventRemove.tsx +99 -0
  320. package/src/useOnRouteFocus.tsx +47 -0
  321. package/src/useOptionsGetters.tsx +98 -0
  322. package/src/useRegisterNavigator.tsx +29 -0
  323. package/src/useRoute.tsx +22 -0
  324. package/src/useRouteCache.tsx +55 -0
  325. package/src/useScheduleUpdate.tsx +32 -0
  326. package/src/useSyncState.tsx +74 -0
  327. package/src/validatePathConfig.tsx +32 -0
package/src/types.tsx ADDED
@@ -0,0 +1,645 @@
1
+ import type {
2
+ DefaultRouterOptions,
3
+ InitialState,
4
+ NavigationAction,
5
+ NavigationState,
6
+ ParamListBase,
7
+ PartialState,
8
+ Route,
9
+ } from '@react-navigation/routers';
10
+ import type * as React from 'react';
11
+
12
+ declare global {
13
+ // eslint-disable-next-line @typescript-eslint/no-namespace
14
+ namespace ReactNavigation {
15
+ // eslint-disable-next-line @typescript-eslint/no-empty-interface
16
+ interface RootParamList {}
17
+ }
18
+ }
19
+
20
+ type Keyof<T extends {}> = Extract<keyof T, string>;
21
+
22
+ export type DefaultNavigatorOptions<
23
+ ParamList extends ParamListBase,
24
+ State extends NavigationState,
25
+ ScreenOptions extends {},
26
+ EventMap extends EventMapBase
27
+ > = DefaultRouterOptions<Keyof<ParamList>> & {
28
+ /**
29
+ * Children React Elements to extract the route configuration from.
30
+ * Only `Screen`, `Group` and `React.Fragment` are supported as children.
31
+ */
32
+ children: React.ReactNode;
33
+ /**
34
+ * Event listeners for all the screens in the navigator.
35
+ */
36
+ screenListeners?:
37
+ | ScreenListeners<State, EventMap>
38
+ | ((props: {
39
+ route: RouteProp<ParamList>;
40
+ navigation: any;
41
+ }) => ScreenListeners<State, EventMap>);
42
+ /**
43
+ * Default options for all screens under this navigator.
44
+ */
45
+ screenOptions?:
46
+ | ScreenOptions
47
+ | ((props: {
48
+ route: RouteProp<ParamList>;
49
+ navigation: any;
50
+ }) => ScreenOptions);
51
+ /**
52
+ * Default options specified by the navigator.
53
+ * It receives the custom options in the arguments if a function is specified.
54
+ */
55
+ defaultScreenOptions?:
56
+ | ScreenOptions
57
+ | ((props: {
58
+ route: RouteProp<ParamList>;
59
+ navigation: any;
60
+ options: ScreenOptions;
61
+ }) => ScreenOptions);
62
+ };
63
+
64
+ export type EventMapBase = Record<
65
+ string,
66
+ { data?: any; canPreventDefault?: boolean }
67
+ >;
68
+
69
+ export type EventMapCore<State extends NavigationState> = {
70
+ focus: { data: undefined };
71
+ blur: { data: undefined };
72
+ state: { data: { state: State } };
73
+ beforeRemove: { data: { action: NavigationAction }; canPreventDefault: true };
74
+ };
75
+
76
+ export type EventArg<
77
+ EventName extends string,
78
+ CanPreventDefault extends boolean | undefined = false,
79
+ Data = undefined
80
+ > = {
81
+ /**
82
+ * Type of the event (e.g. `focus`, `blur`)
83
+ */
84
+ readonly type: EventName;
85
+ readonly target?: string;
86
+ } & (CanPreventDefault extends true
87
+ ? {
88
+ /**
89
+ * Whether `event.preventDefault()` was called on this event object.
90
+ */
91
+ readonly defaultPrevented: boolean;
92
+ /**
93
+ * Prevent the default action which happens on this event.
94
+ */
95
+ preventDefault(): void;
96
+ }
97
+ : {}) &
98
+ (undefined extends Data
99
+ ? { readonly data?: Readonly<Data> }
100
+ : { readonly data: Readonly<Data> });
101
+
102
+ export type EventListenerCallback<
103
+ EventMap extends EventMapBase,
104
+ EventName extends keyof EventMap
105
+ > = (
106
+ e: EventArg<
107
+ Extract<EventName, string>,
108
+ EventMap[EventName]['canPreventDefault'],
109
+ EventMap[EventName]['data']
110
+ >
111
+ ) => void;
112
+
113
+ export type EventConsumer<EventMap extends EventMapBase> = {
114
+ /**
115
+ * Subscribe to events from the parent navigator.
116
+ *
117
+ * @param type Type of the event (e.g. `focus`, `blur`)
118
+ * @param callback Callback listener which is executed upon receiving the event.
119
+ */
120
+ addListener<EventName extends Keyof<EventMap>>(
121
+ type: EventName,
122
+ callback: EventListenerCallback<EventMap, EventName>
123
+ ): () => void;
124
+ removeListener<EventName extends Keyof<EventMap>>(
125
+ type: EventName,
126
+ callback: EventListenerCallback<EventMap, EventName>
127
+ ): void;
128
+ };
129
+
130
+ export type EventEmitter<EventMap extends EventMapBase> = {
131
+ /**
132
+ * Emit an event to child screens.
133
+ *
134
+ * @param options.type Type of the event (e.g. `focus`, `blur`)
135
+ * @param [options.data] Optional information regarding the event.
136
+ * @param [options.target] Key of the target route which should receive the event.
137
+ * If not specified, all routes receive the event.
138
+ */
139
+ emit<EventName extends Keyof<EventMap>>(
140
+ options: {
141
+ type: EventName;
142
+ target?: string;
143
+ } & (EventMap[EventName]['canPreventDefault'] extends true
144
+ ? { canPreventDefault: true }
145
+ : {}) &
146
+ (undefined extends EventMap[EventName]['data']
147
+ ? { data?: EventMap[EventName]['data'] }
148
+ : { data: EventMap[EventName]['data'] })
149
+ ): EventArg<
150
+ EventName,
151
+ EventMap[EventName]['canPreventDefault'],
152
+ EventMap[EventName]['data']
153
+ >;
154
+ };
155
+
156
+ export class PrivateValueStore<A, B, C> {
157
+ /**
158
+ * UGLY HACK! DO NOT USE THE TYPE!!!
159
+ *
160
+ * TypeScript requires a type to be used to be able to infer it.
161
+ * The type should exist as its own without any operations such as union.
162
+ * So we need to figure out a way to store this type in a property.
163
+ * The problem with a normal property is that it shows up in intelliSense.
164
+ * Adding private keyword works, but the annotation is stripped away in declaration.
165
+ * Turns out if we use an empty string, it doesn't show up in intelliSense.
166
+ */
167
+ protected ''?: { a: A; b: B; c: C };
168
+ }
169
+
170
+ type NavigationHelpersCommon<
171
+ ParamList extends ParamListBase,
172
+ State extends NavigationState = NavigationState
173
+ > = {
174
+ /**
175
+ * Dispatch an action or an update function to the router.
176
+ * The update function will receive the current state,
177
+ *
178
+ * @param action Action object or update function.
179
+ */
180
+ dispatch(
181
+ action: NavigationAction | ((state: State) => NavigationAction)
182
+ ): void;
183
+
184
+ /**
185
+ * Navigate to a route in current navigation tree.
186
+ *
187
+ * @param name Name of the route to navigate to.
188
+ * @param [params] Params object for the route.
189
+ */
190
+ navigate<RouteName extends keyof ParamList>(
191
+ ...args: undefined extends ParamList[RouteName]
192
+ ? [screen: RouteName] | [screen: RouteName, params: ParamList[RouteName]]
193
+ : [screen: RouteName, params: ParamList[RouteName]]
194
+ ): void;
195
+
196
+ /**
197
+ * Navigate to a route in current navigation tree.
198
+ *
199
+ * @param route Object with `key` or `name` for the route to navigate to, and a `params` object.
200
+ */
201
+ navigate<RouteName extends keyof ParamList>(
202
+ options:
203
+ | { key: string; params?: ParamList[RouteName]; merge?: boolean }
204
+ | {
205
+ name: RouteName;
206
+ key?: string;
207
+ params: ParamList[RouteName];
208
+ merge?: boolean;
209
+ }
210
+ ): void;
211
+
212
+ /**
213
+ * Reset the navigation state to the provided state.
214
+ *
215
+ * @param state Navigation state object.
216
+ */
217
+ reset(state: PartialState<State> | State): void;
218
+
219
+ /**
220
+ * Go back to the previous route in history.
221
+ */
222
+ goBack(): void;
223
+
224
+ /**
225
+ * Check if the screen is focused. The method returns `true` if focused, `false` otherwise.
226
+ * Note that this method doesn't re-render screen when the focus changes. So don't use it in `render`.
227
+ * To get notified of focus changes, use `addListener('focus', cb)` and `addListener('blur', cb)`.
228
+ * To conditionally render content based on focus state, use the `useIsFocused` hook.
229
+ */
230
+ isFocused(): boolean;
231
+
232
+ /**
233
+ * Check if dispatching back action will be handled by navigation.
234
+ * Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
235
+ */
236
+ canGoBack(): boolean;
237
+
238
+ /**
239
+ * Returns the navigation prop from the parent navigator,
240
+ */
241
+ getParent<T = NavigationProp<ParamListBase> | undefined>(): T;
242
+
243
+ /**
244
+ * Returns the navigator's state.
245
+ * Note that this method doesn't re-render screen when the result changes. So don't use it in `render`.
246
+ */
247
+ getState(): State;
248
+ } & PrivateValueStore<ParamList, keyof ParamList, {}>;
249
+
250
+ export type NavigationHelpers<
251
+ ParamList extends ParamListBase,
252
+ EventMap extends EventMapBase = {}
253
+ > = NavigationHelpersCommon<ParamList> &
254
+ EventEmitter<EventMap> & {
255
+ /**
256
+ * Update the param object for the route.
257
+ * The new params will be shallow merged with the old one.
258
+ *
259
+ * @param params Params object for the current route.
260
+ */
261
+ setParams<RouteName extends keyof ParamList>(
262
+ params: Partial<ParamList[RouteName]>
263
+ ): void;
264
+ };
265
+
266
+ export type NavigationContainerProps = {
267
+ /**
268
+ * Initial navigation state for the child navigators.
269
+ */
270
+ initialState?: InitialState;
271
+ /**
272
+ * Callback which is called with the latest navigation state when it changes.
273
+ */
274
+ onStateChange?: (state: NavigationState | undefined) => void;
275
+ /**
276
+ * Callback which is called when an action is not handled.
277
+ */
278
+ onUnhandledAction?: (action: NavigationAction) => void;
279
+ /**
280
+ * Whether this navigation container should be independent of parent containers.
281
+ * If this is not set to `true`, this container cannot be nested inside another container.
282
+ * Setting it to `true` disconnects any children navigators from parent container.
283
+ */
284
+ independent?: boolean;
285
+ /**
286
+ * Children elements to render.
287
+ */
288
+ children: React.ReactNode;
289
+ };
290
+
291
+ export type NavigationProp<
292
+ ParamList extends {},
293
+ RouteName extends keyof ParamList = Keyof<ParamList>,
294
+ State extends NavigationState = NavigationState<ParamList>,
295
+ ScreenOptions extends {} = {},
296
+ EventMap extends EventMapBase = {}
297
+ > = NavigationHelpersCommon<ParamList, State> & {
298
+ /**
299
+ * Update the param object for the route.
300
+ * The new params will be shallow merged with the old one.
301
+ *
302
+ * @param params Params object for the current route.
303
+ */
304
+ setParams(params: Partial<ParamList[RouteName]>): void;
305
+
306
+ /**
307
+ * Update the options for the route.
308
+ * The options object will be shallow merged with default options object.
309
+ *
310
+ * @param options Options object for the route.
311
+ */
312
+ setOptions(options: Partial<ScreenOptions>): void;
313
+ } & EventConsumer<EventMap & EventMapCore<State>> &
314
+ PrivateValueStore<ParamList, RouteName, EventMap>;
315
+
316
+ export type RouteProp<
317
+ ParamList extends ParamListBase,
318
+ RouteName extends keyof ParamList = Keyof<ParamList>
319
+ > = Route<Extract<RouteName, string>, ParamList[RouteName]>;
320
+
321
+ export type CompositeNavigationProp<
322
+ A extends NavigationProp<ParamListBase, string, any, any>,
323
+ B extends NavigationHelpersCommon<ParamListBase, any>
324
+ > = Omit<A & B, keyof NavigationProp<any>> &
325
+ NavigationProp<
326
+ /**
327
+ * Param list from both navigation objects needs to be combined
328
+ * For example, we should be able to navigate to screens in both A and B
329
+ */
330
+ (A extends NavigationHelpersCommon<infer T> ? T : never) &
331
+ (B extends NavigationHelpersCommon<infer U> ? U : never),
332
+ /**
333
+ * The route name should refer to the route name specified in the first type
334
+ * Ideally it should work for any of them, but it's not possible to infer that way
335
+ */
336
+ A extends NavigationProp<any, infer R> ? R : string,
337
+ /**
338
+ * The type of state should refer to the state specified in the first type
339
+ */
340
+ A extends NavigationProp<any, any, infer S> ? S : NavigationState,
341
+ /**
342
+ * Screen options from both navigation objects needs to be combined
343
+ * This allows typechecking `setOptions`
344
+ */
345
+ (A extends NavigationProp<any, any, any, infer O> ? O : {}) &
346
+ (B extends NavigationProp<any, any, any, infer P> ? P : {}),
347
+ /**
348
+ * Event consumer config should refer to the config specified in the first type
349
+ * This allows typechecking `addListener`/`removeListener`
350
+ */
351
+ A extends NavigationProp<any, any, any, any, infer E> ? E : {}
352
+ >;
353
+
354
+ export type CompositeScreenProps<
355
+ A extends {
356
+ navigation: NavigationProp<ParamListBase, string, any, any>;
357
+ route: RouteProp<ParamListBase>;
358
+ },
359
+ B extends {
360
+ navigation: NavigationHelpersCommon<ParamListBase, any>;
361
+ }
362
+ > = {
363
+ navigation: CompositeNavigationProp<A['navigation'], B['navigation']>;
364
+ route: A['route'];
365
+ };
366
+
367
+ export type Descriptor<
368
+ ScreenOptions extends {},
369
+ Navigation extends NavigationProp<any, any, any, any, any>,
370
+ Route extends RouteProp<any, any>
371
+ > = {
372
+ /**
373
+ * Render the component associated with this route.
374
+ */
375
+ render(): JSX.Element;
376
+
377
+ /**
378
+ * Options for the route.
379
+ */
380
+ options: ScreenOptions;
381
+
382
+ /**
383
+ * Route object for the screen
384
+ */
385
+ route: Route;
386
+
387
+ /**
388
+ * Navigation object for the screen
389
+ */
390
+ navigation: Navigation;
391
+ };
392
+
393
+ export type ScreenListeners<
394
+ State extends NavigationState,
395
+ EventMap extends EventMapBase
396
+ > = Partial<
397
+ {
398
+ [EventName in keyof (EventMap &
399
+ EventMapCore<State>)]: EventListenerCallback<EventMap, EventName>;
400
+ }
401
+ >;
402
+
403
+ export type RouteConfigComponent<
404
+ ParamList extends ParamListBase,
405
+ RouteName extends keyof ParamList
406
+ > =
407
+ | {
408
+ /**
409
+ * React component to render for this screen.
410
+ */
411
+ component: React.ComponentType<any>;
412
+ getComponent?: never;
413
+ children?: never;
414
+ }
415
+ | {
416
+ /**
417
+ * Lazily get a React component to render for this screen.
418
+ */
419
+ getComponent: () => React.ComponentType<any>;
420
+ component?: never;
421
+ children?: never;
422
+ }
423
+ | {
424
+ /**
425
+ * Render callback to render content of this screen.
426
+ */
427
+ children: (props: {
428
+ route: RouteProp<ParamList, RouteName>;
429
+ navigation: any;
430
+ }) => React.ReactNode;
431
+ component?: never;
432
+ getComponent?: never;
433
+ };
434
+
435
+ export type RouteConfig<
436
+ ParamList extends ParamListBase,
437
+ RouteName extends keyof ParamList,
438
+ State extends NavigationState,
439
+ ScreenOptions extends {},
440
+ EventMap extends EventMapBase
441
+ > = {
442
+ /**
443
+ * Route name of this screen.
444
+ */
445
+ name: RouteName;
446
+
447
+ /**
448
+ * Navigator options for this screen.
449
+ */
450
+ options?:
451
+ | ScreenOptions
452
+ | ((props: {
453
+ route: RouteProp<ParamList, RouteName>;
454
+ navigation: any;
455
+ }) => ScreenOptions);
456
+
457
+ /**
458
+ * Event listeners for this screen.
459
+ */
460
+ listeners?:
461
+ | ScreenListeners<State, EventMap>
462
+ | ((props: {
463
+ route: RouteProp<ParamList, RouteName>;
464
+ navigation: any;
465
+ }) => ScreenListeners<State, EventMap>);
466
+
467
+ /**
468
+ * Function to return an unique ID for this screen.
469
+ * Receives an object with the route params.
470
+ * For a given screen name, there will always be only one screen corresponding to an ID.
471
+ * If `undefined` is returned, it acts same as no `getId` being specified.
472
+ */
473
+ getId?: ({ params }: { params: ParamList[RouteName] }) => string | undefined;
474
+
475
+ /**
476
+ * Initial params object for the route.
477
+ */
478
+ initialParams?: Partial<ParamList[RouteName]>;
479
+ } & RouteConfigComponent<ParamList, RouteName>;
480
+
481
+ export type RouteGroupConfig<
482
+ ParamList extends ParamListBase,
483
+ ScreenOptions extends {}
484
+ > = {
485
+ /**
486
+ * Navigator options for this screen.
487
+ */
488
+ screenOptions?:
489
+ | ScreenOptions
490
+ | ((props: {
491
+ route: RouteProp<ParamList, keyof ParamList>;
492
+ navigation: any;
493
+ }) => ScreenOptions);
494
+ /**
495
+ * Children React Elements to extract the route configuration from.
496
+ * Only `Screen`, `Group` and `React.Fragment` are supported as children.
497
+ */
498
+ children: React.ReactNode;
499
+ };
500
+
501
+ export type NavigationContainerEventMap = {
502
+ /**
503
+ * Event which fires when the navigation state changes.
504
+ */
505
+ state: {
506
+ data: {
507
+ /**
508
+ * The updated state object after the state change.
509
+ */
510
+ state: NavigationState | PartialState<NavigationState> | undefined;
511
+ };
512
+ };
513
+ /**
514
+ * Event which fires when current options changes.
515
+ */
516
+ options: { data: { options: object } };
517
+ /**
518
+ * Event which fires when an action is dispatched.
519
+ * Only intended for debugging purposes, don't use it for app logic.
520
+ * This event will be emitted before state changes have been applied.
521
+ */
522
+ __unsafe_action__: {
523
+ data: {
524
+ /**
525
+ * The action object which was dispatched.
526
+ */
527
+ action: NavigationAction;
528
+ /**
529
+ * Whether the action was a no-op, i.e. resulted any state changes.
530
+ */
531
+ noop: boolean;
532
+ /**
533
+ * Stack trace of the action, this will only be available during development.
534
+ */
535
+ stack: string | undefined;
536
+ };
537
+ };
538
+ };
539
+
540
+ export type NavigationContainerRef<ParamList extends {}> =
541
+ NavigationHelpers<ParamList> &
542
+ EventConsumer<NavigationContainerEventMap> & {
543
+ /**
544
+ * Reset the navigation state of the root navigator to the provided state.
545
+ *
546
+ * @param state Navigation state object.
547
+ */
548
+ resetRoot(state?: PartialState<NavigationState> | NavigationState): void;
549
+ /**
550
+ * Get the rehydrated navigation state of the navigation tree.
551
+ */
552
+ getRootState(): NavigationState;
553
+ /**
554
+ * Get the currently focused navigation route.
555
+ */
556
+ getCurrentRoute(): Route<string> | undefined;
557
+ /**
558
+ * Get the currently focused route's options.
559
+ */
560
+ getCurrentOptions(): object | undefined;
561
+ /**
562
+ * Whether the navigation container is ready to handle actions.
563
+ */
564
+ isReady(): boolean;
565
+ };
566
+
567
+ export type NavigationContainerRefWithCurrent<ParamList extends {}> =
568
+ NavigationContainerRef<ParamList> & {
569
+ current: NavigationContainerRef<ParamList> | null;
570
+ };
571
+
572
+ export type TypedNavigator<
573
+ ParamList extends ParamListBase,
574
+ State extends NavigationState,
575
+ ScreenOptions extends {},
576
+ EventMap extends EventMapBase,
577
+ Navigator extends React.ComponentType<any>
578
+ > = {
579
+ /**
580
+ * Navigator component which manages the child screens.
581
+ */
582
+ Navigator: React.ComponentType<
583
+ Omit<
584
+ React.ComponentProps<Navigator>,
585
+ keyof DefaultNavigatorOptions<any, any, any, any>
586
+ > &
587
+ DefaultNavigatorOptions<ParamList, State, ScreenOptions, EventMap>
588
+ >;
589
+ /**
590
+ * Component used for grouping multiple route configuration.
591
+ */
592
+ Group: React.ComponentType<RouteGroupConfig<ParamList, ScreenOptions>>;
593
+ /**
594
+ * Component used for specifying route configuration.
595
+ */
596
+ Screen: <RouteName extends keyof ParamList>(
597
+ _: RouteConfig<ParamList, RouteName, State, ScreenOptions, EventMap>
598
+ ) => null;
599
+ };
600
+
601
+ export type NavigatorScreenParams<
602
+ ParamList,
603
+ State extends NavigationState = NavigationState
604
+ > =
605
+ | {
606
+ screen?: never;
607
+ params?: never;
608
+ initial?: never;
609
+ path?: string;
610
+ state: PartialState<State> | State | undefined;
611
+ }
612
+ | {
613
+ [RouteName in keyof ParamList]: undefined extends ParamList[RouteName]
614
+ ? {
615
+ screen: RouteName;
616
+ params?: ParamList[RouteName];
617
+ initial?: boolean;
618
+ path?: string;
619
+ state?: never;
620
+ }
621
+ : {
622
+ screen: RouteName;
623
+ params: ParamList[RouteName];
624
+ initial?: boolean;
625
+ path?: string;
626
+ state?: never;
627
+ };
628
+ }[keyof ParamList];
629
+
630
+ export type PathConfig<ParamList extends {}> = {
631
+ path?: string;
632
+ exact?: boolean;
633
+ parse?: Record<string, (value: string) => any>;
634
+ stringify?: Record<string, (value: any) => string>;
635
+ screens?: PathConfigMap<ParamList>;
636
+ initialRouteName?: keyof ParamList;
637
+ };
638
+
639
+ export type PathConfigMap<ParamList extends {}> = {
640
+ [RouteName in keyof ParamList]?: NonNullable<
641
+ ParamList[RouteName]
642
+ > extends NavigatorScreenParams<infer T, any>
643
+ ? string | PathConfig<T>
644
+ : string | Omit<PathConfig<{}>, 'screens' | 'initialRouteName'>;
645
+ };
@@ -0,0 +1,37 @@
1
+ import * as React from 'react';
2
+
3
+ import type { ListenerMap } from './NavigationBuilderContext';
4
+
5
+ /**
6
+ * Hook which lets child navigators add action listeners.
7
+ */
8
+ export default function useChildListeners() {
9
+ const { current: listeners } = React.useRef<
10
+ {
11
+ [K in keyof ListenerMap]: ListenerMap[K][];
12
+ }
13
+ >({
14
+ action: [],
15
+ focus: [],
16
+ });
17
+
18
+ const addListener = React.useCallback(
19
+ <T extends keyof ListenerMap>(type: T, listener: ListenerMap[T]) => {
20
+ // @ts-expect-error: listener should be correct type according to `type`
21
+ listeners[type].push(listener);
22
+
23
+ return () => {
24
+ // @ts-expect-error: listener should be correct type according to `type`
25
+ const index = listeners[type].indexOf(listener);
26
+
27
+ listeners[type].splice(index, 1);
28
+ };
29
+ },
30
+ [listeners]
31
+ );
32
+
33
+ return {
34
+ listeners,
35
+ addListener,
36
+ };
37
+ }
@@ -0,0 +1,30 @@
1
+ import * as React from 'react';
2
+
3
+ export default function useComponent<
4
+ T extends React.ComponentType<any>,
5
+ P extends {}
6
+ >(Component: T, props: P) {
7
+ const propsRef = React.useRef<P | null>(props);
8
+
9
+ // Normally refs shouldn't be mutated in render
10
+ // But we return a component which will be rendered
11
+ // So it's just for immediate consumption
12
+ propsRef.current = props;
13
+
14
+ React.useEffect(() => {
15
+ propsRef.current = null;
16
+ });
17
+
18
+ return React.useRef((rest: Omit<React.ComponentProps<T>, keyof P>) => {
19
+ const props = propsRef.current;
20
+
21
+ if (props === null) {
22
+ throw new Error(
23
+ 'The returned component must be rendered in the same render phase as the hook.'
24
+ );
25
+ }
26
+
27
+ // @ts-expect-error: the props should be fine here
28
+ return <Component {...props} {...rest} />;
29
+ }).current;
30
+ }