@react-navigation/core 8.0.0-alpha.4 → 8.0.0-alpha.40

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 (323) hide show
  1. package/README.md +17 -14
  2. package/lib/module/BaseNavigationContainer.js +168 -87
  3. package/lib/module/BaseNavigationContainer.js.map +1 -1
  4. package/lib/module/ConsumedParamsContext.js +5 -0
  5. package/lib/module/ConsumedParamsContext.js.map +1 -0
  6. package/lib/module/DataLoading.js +129 -0
  7. package/lib/module/DataLoading.js.map +1 -0
  8. package/lib/module/NavigationBuilderContext.js +7 -10
  9. package/lib/module/NavigationBuilderContext.js.map +1 -1
  10. package/lib/module/NavigationIndependentTree.js +54 -9
  11. package/lib/module/NavigationIndependentTree.js.map +1 -1
  12. package/lib/module/NavigationProvider.js +36 -11
  13. package/lib/module/NavigationProvider.js.map +1 -1
  14. package/lib/module/NavigationRootContext.js +8 -0
  15. package/lib/module/NavigationRootContext.js.map +1 -0
  16. package/lib/module/NavigationStateContext.js +3 -0
  17. package/lib/module/NavigationStateContext.js.map +1 -1
  18. package/lib/module/PreventRemoveContext.js.map +1 -1
  19. package/lib/module/PreventRemoveProvider.js +40 -31
  20. package/lib/module/PreventRemoveProvider.js.map +1 -1
  21. package/lib/module/SceneView.js +12 -42
  22. package/lib/module/SceneView.js.map +1 -1
  23. package/lib/module/StaticNavigation.js +219 -67
  24. package/lib/module/StaticNavigation.js.map +1 -1
  25. package/lib/module/StaticTreeContext.js +5 -0
  26. package/lib/module/StaticTreeContext.js.map +1 -0
  27. package/lib/module/createNavigationContainerRef.js +7 -9
  28. package/lib/module/createNavigationContainerRef.js.map +1 -1
  29. package/lib/module/createNavigatorFactory.js +27 -5
  30. package/lib/module/createNavigatorFactory.js.map +1 -1
  31. package/lib/module/findFocusedRoute.js +7 -4
  32. package/lib/module/findFocusedRoute.js.map +1 -1
  33. package/lib/module/getActionFromState.js +45 -20
  34. package/lib/module/getActionFromState.js.map +1 -1
  35. package/lib/module/getPathFromState.js +204 -94
  36. package/lib/module/getPathFromState.js.map +1 -1
  37. package/lib/module/getPatternParts.js +61 -14
  38. package/lib/module/getPatternParts.js.map +1 -1
  39. package/lib/module/getStateFromPath.js +608 -272
  40. package/lib/module/getStateFromPath.js.map +1 -1
  41. package/lib/module/getStateFromRouteParams.js +23 -0
  42. package/lib/module/getStateFromRouteParams.js.map +1 -0
  43. package/lib/module/index.js +7 -6
  44. package/lib/module/index.js.map +1 -1
  45. package/lib/module/internal.js +2 -0
  46. package/lib/module/internal.js.map +1 -0
  47. package/lib/module/isRecordEqual.js +1 -1
  48. package/lib/module/isRecordEqual.js.map +1 -1
  49. package/lib/module/queryString.js +71 -0
  50. package/lib/module/queryString.js.map +1 -0
  51. package/lib/module/theming/useTheme.js +1 -1
  52. package/lib/module/theming/useTheme.js.map +1 -1
  53. package/lib/module/types.js +35 -1
  54. package/lib/module/types.js.map +1 -1
  55. package/lib/module/useDescriptors.js +65 -25
  56. package/lib/module/useDescriptors.js.map +1 -1
  57. package/lib/module/useEventEmitter.js +48 -33
  58. package/lib/module/useEventEmitter.js.map +1 -1
  59. package/lib/module/useFocusEffect.js +4 -2
  60. package/lib/module/useFocusEffect.js.map +1 -1
  61. package/lib/module/useFocusEvents.js +15 -5
  62. package/lib/module/useFocusEvents.js.map +1 -1
  63. package/lib/module/useFocusedListenersChildrenAdapter.js +3 -3
  64. package/lib/module/useFocusedListenersChildrenAdapter.js.map +1 -1
  65. package/lib/module/useIsFocused.js +8 -12
  66. package/lib/module/useIsFocused.js.map +1 -1
  67. package/lib/module/useKeyedChildListeners.js +5 -2
  68. package/lib/module/useKeyedChildListeners.js.map +1 -1
  69. package/lib/module/useLazyValue.js.map +1 -1
  70. package/lib/module/useNavigation.js +4 -17
  71. package/lib/module/useNavigation.js.map +1 -1
  72. package/lib/module/useNavigationBuilder.js +244 -147
  73. package/lib/module/useNavigationBuilder.js.map +1 -1
  74. package/lib/module/useNavigationCache.js +92 -89
  75. package/lib/module/useNavigationCache.js.map +1 -1
  76. package/lib/module/useNavigationHelpers.js +22 -29
  77. package/lib/module/useNavigationHelpers.js.map +1 -1
  78. package/lib/module/useNavigationIndependentTree.js +1 -1
  79. package/lib/module/useNavigationIndependentTree.js.map +1 -1
  80. package/lib/module/useNavigationState.js +158 -46
  81. package/lib/module/useNavigationState.js.map +1 -1
  82. package/lib/module/useOnAction.js +58 -11
  83. package/lib/module/useOnAction.js.map +1 -1
  84. package/lib/module/useOnGetNavigation.js +26 -0
  85. package/lib/module/useOnGetNavigation.js.map +1 -0
  86. package/lib/module/useOnGetState.js +7 -4
  87. package/lib/module/useOnGetState.js.map +1 -1
  88. package/lib/module/useOnPreventRemove.js +40 -31
  89. package/lib/module/useOnPreventRemove.js.map +1 -1
  90. package/lib/module/useOnRouteFocus.js +2 -2
  91. package/lib/module/useOnRouteFocus.js.map +1 -1
  92. package/lib/module/useOptionsGetters.js +34 -24
  93. package/lib/module/useOptionsGetters.js.map +1 -1
  94. package/lib/module/usePreventRemove.js +11 -5
  95. package/lib/module/usePreventRemove.js.map +1 -1
  96. package/lib/module/usePreventRemoveContext.js +2 -2
  97. package/lib/module/usePreventRemoveContext.js.map +1 -1
  98. package/lib/module/useRegisterNavigator.js +3 -4
  99. package/lib/module/useRegisterNavigator.js.map +1 -1
  100. package/lib/module/useRoute.js +2 -0
  101. package/lib/module/useRoute.js.map +1 -1
  102. package/lib/module/useRouteCache.js +51 -35
  103. package/lib/module/useRouteCache.js.map +1 -1
  104. package/lib/module/useScheduleUpdate.js +12 -10
  105. package/lib/module/useScheduleUpdate.js.map +1 -1
  106. package/lib/module/useStateForPath.js +1 -1
  107. package/lib/module/useStateForPath.js.map +1 -1
  108. package/lib/module/useSyncState.js +37 -15
  109. package/lib/module/useSyncState.js.map +1 -1
  110. package/lib/module/validatePathConfig.js +50 -36
  111. package/lib/module/validatePathConfig.js.map +1 -1
  112. package/lib/typescript/src/BaseNavigationContainer.d.ts +6 -3
  113. package/lib/typescript/src/BaseNavigationContainer.d.ts.map +1 -1
  114. package/lib/typescript/src/ConsumedParamsContext.d.ts +3 -0
  115. package/lib/typescript/src/ConsumedParamsContext.d.ts.map +1 -0
  116. package/lib/typescript/src/DataLoading.d.ts +21 -0
  117. package/lib/typescript/src/DataLoading.d.ts.map +1 -0
  118. package/lib/typescript/src/EnsureSingleNavigator.d.ts +1 -1
  119. package/lib/typescript/src/EnsureSingleNavigator.d.ts.map +1 -1
  120. package/lib/typescript/src/Group.d.ts +1 -1
  121. package/lib/typescript/src/Group.d.ts.map +1 -1
  122. package/lib/typescript/src/NavigationBuilderContext.d.ts +22 -14
  123. package/lib/typescript/src/NavigationBuilderContext.d.ts.map +1 -1
  124. package/lib/typescript/src/NavigationContainerRefContext.d.ts +1 -1
  125. package/lib/typescript/src/NavigationContainerRefContext.d.ts.map +1 -1
  126. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts +4 -4
  127. package/lib/typescript/src/NavigationFocusedRouteStateContext.d.ts.map +1 -1
  128. package/lib/typescript/src/NavigationHelpersContext.d.ts +1 -1
  129. package/lib/typescript/src/NavigationHelpersContext.d.ts.map +1 -1
  130. package/lib/typescript/src/NavigationIndependentTree.d.ts +1 -1
  131. package/lib/typescript/src/NavigationIndependentTree.d.ts.map +1 -1
  132. package/lib/typescript/src/NavigationProvider.d.ts +21 -18
  133. package/lib/typescript/src/NavigationProvider.d.ts.map +1 -1
  134. package/lib/typescript/src/NavigationRootContext.d.ts +8 -0
  135. package/lib/typescript/src/NavigationRootContext.d.ts.map +1 -0
  136. package/lib/typescript/src/NavigationStateContext.d.ts +4 -3
  137. package/lib/typescript/src/NavigationStateContext.d.ts.map +1 -1
  138. package/lib/typescript/src/PreventRemoveContext.d.ts +1 -0
  139. package/lib/typescript/src/PreventRemoveContext.d.ts.map +1 -1
  140. package/lib/typescript/src/PreventRemoveProvider.d.ts +1 -1
  141. package/lib/typescript/src/PreventRemoveProvider.d.ts.map +1 -1
  142. package/lib/typescript/src/SceneView.d.ts +5 -2
  143. package/lib/typescript/src/SceneView.d.ts.map +1 -1
  144. package/lib/typescript/src/Screen.d.ts +1 -1
  145. package/lib/typescript/src/Screen.d.ts.map +1 -1
  146. package/lib/typescript/src/StaticContainer.d.ts +1 -1
  147. package/lib/typescript/src/StaticContainer.d.ts.map +1 -1
  148. package/lib/typescript/src/StaticNavigation.d.ts +189 -111
  149. package/lib/typescript/src/StaticNavigation.d.ts.map +1 -1
  150. package/lib/typescript/src/StaticTreeContext.d.ts +4 -0
  151. package/lib/typescript/src/StaticTreeContext.d.ts.map +1 -0
  152. package/lib/typescript/src/createNavigationContainerRef.d.ts +1 -1
  153. package/lib/typescript/src/createNavigationContainerRef.d.ts.map +1 -1
  154. package/lib/typescript/src/createNavigatorFactory.d.ts +23 -5
  155. package/lib/typescript/src/createNavigatorFactory.d.ts.map +1 -1
  156. package/lib/typescript/src/findFocusedRoute.d.ts +3 -8
  157. package/lib/typescript/src/findFocusedRoute.d.ts.map +1 -1
  158. package/lib/typescript/src/getActionFromState.d.ts +4 -4
  159. package/lib/typescript/src/getActionFromState.d.ts.map +1 -1
  160. package/lib/typescript/src/getPathFromState.d.ts +3 -3
  161. package/lib/typescript/src/getPathFromState.d.ts.map +1 -1
  162. package/lib/typescript/src/getPatternParts.d.ts +5 -2
  163. package/lib/typescript/src/getPatternParts.d.ts.map +1 -1
  164. package/lib/typescript/src/getStateFromPath.d.ts +4 -7
  165. package/lib/typescript/src/getStateFromPath.d.ts.map +1 -1
  166. package/lib/typescript/src/getStateFromRouteParams.d.ts +3 -0
  167. package/lib/typescript/src/getStateFromRouteParams.d.ts.map +1 -0
  168. package/lib/typescript/src/index.d.ts +36 -33
  169. package/lib/typescript/src/index.d.ts.map +1 -1
  170. package/lib/typescript/src/internal.d.ts +3 -0
  171. package/lib/typescript/src/internal.d.ts.map +1 -0
  172. package/lib/typescript/src/isRecordEqual.d.ts.map +1 -1
  173. package/lib/typescript/src/queryString.d.ts +5 -0
  174. package/lib/typescript/src/queryString.d.ts.map +1 -0
  175. package/lib/typescript/src/theming/ThemeContext.d.ts +1 -1
  176. package/lib/typescript/src/theming/ThemeContext.d.ts.map +1 -1
  177. package/lib/typescript/src/theming/ThemeProvider.d.ts +2 -2
  178. package/lib/typescript/src/theming/ThemeProvider.d.ts.map +1 -1
  179. package/lib/typescript/src/theming/useTheme.d.ts +1 -1
  180. package/lib/typescript/src/theming/useTheme.d.ts.map +1 -1
  181. package/lib/typescript/src/types.d.ts +265 -167
  182. package/lib/typescript/src/types.d.ts.map +1 -1
  183. package/lib/typescript/src/useChildListeners.d.ts +3 -3
  184. package/lib/typescript/src/useChildListeners.d.ts.map +1 -1
  185. package/lib/typescript/src/useDescriptors.d.ts +13 -11
  186. package/lib/typescript/src/useDescriptors.d.ts.map +1 -1
  187. package/lib/typescript/src/useEventEmitter.d.ts +2 -2
  188. package/lib/typescript/src/useEventEmitter.d.ts.map +1 -1
  189. package/lib/typescript/src/useFocusEffect.d.ts.map +1 -1
  190. package/lib/typescript/src/useFocusEvents.d.ts +2 -2
  191. package/lib/typescript/src/useFocusEvents.d.ts.map +1 -1
  192. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts +2 -2
  193. package/lib/typescript/src/useFocusedListenersChildrenAdapter.d.ts.map +1 -1
  194. package/lib/typescript/src/useIsFocused.d.ts +4 -0
  195. package/lib/typescript/src/useIsFocused.d.ts.map +1 -1
  196. package/lib/typescript/src/useKeyedChildListeners.d.ts +4 -3
  197. package/lib/typescript/src/useKeyedChildListeners.d.ts.map +1 -1
  198. package/lib/typescript/src/useLazyValue.d.ts.map +1 -1
  199. package/lib/typescript/src/useNavigation.d.ts +4 -2
  200. package/lib/typescript/src/useNavigation.d.ts.map +1 -1
  201. package/lib/typescript/src/useNavigationBuilder.d.ts +22 -63
  202. package/lib/typescript/src/useNavigationBuilder.d.ts.map +1 -1
  203. package/lib/typescript/src/useNavigationCache.d.ts +6 -5
  204. package/lib/typescript/src/useNavigationCache.d.ts.map +1 -1
  205. package/lib/typescript/src/useNavigationContainerRef.d.ts +1 -1
  206. package/lib/typescript/src/useNavigationContainerRef.d.ts.map +1 -1
  207. package/lib/typescript/src/useNavigationHelpers.d.ts +18 -58
  208. package/lib/typescript/src/useNavigationHelpers.d.ts.map +1 -1
  209. package/lib/typescript/src/useNavigationState.d.ts +19 -3
  210. package/lib/typescript/src/useNavigationState.d.ts.map +1 -1
  211. package/lib/typescript/src/useOnAction.d.ts +9 -9
  212. package/lib/typescript/src/useOnAction.d.ts.map +1 -1
  213. package/lib/typescript/src/useOnGetNavigation.d.ts +12 -0
  214. package/lib/typescript/src/useOnGetNavigation.d.ts.map +1 -0
  215. package/lib/typescript/src/useOnGetState.d.ts +1 -1
  216. package/lib/typescript/src/useOnGetState.d.ts.map +1 -1
  217. package/lib/typescript/src/useOnPreventRemove.d.ts +5 -9
  218. package/lib/typescript/src/useOnPreventRemove.d.ts.map +1 -1
  219. package/lib/typescript/src/useOnRouteFocus.d.ts +6 -6
  220. package/lib/typescript/src/useOnRouteFocus.d.ts.map +1 -1
  221. package/lib/typescript/src/useOptionsGetters.d.ts +2 -4
  222. package/lib/typescript/src/useOptionsGetters.d.ts.map +1 -1
  223. package/lib/typescript/src/usePreventRemove.d.ts.map +1 -1
  224. package/lib/typescript/src/usePreventRemoveContext.d.ts +2 -1
  225. package/lib/typescript/src/usePreventRemoveContext.d.ts.map +1 -1
  226. package/lib/typescript/src/useRegisterNavigator.d.ts.map +1 -1
  227. package/lib/typescript/src/useRoute.d.ts +7 -3
  228. package/lib/typescript/src/useRoute.d.ts.map +1 -1
  229. package/lib/typescript/src/useRouteCache.d.ts +7 -12
  230. package/lib/typescript/src/useRouteCache.d.ts.map +1 -1
  231. package/lib/typescript/src/useScheduleUpdate.d.ts +5 -4
  232. package/lib/typescript/src/useScheduleUpdate.d.ts.map +1 -1
  233. package/lib/typescript/src/useStateForPath.d.ts +1 -1
  234. package/lib/typescript/src/useStateForPath.d.ts.map +1 -1
  235. package/lib/typescript/src/useSyncState.d.ts +1 -0
  236. package/lib/typescript/src/useSyncState.d.ts.map +1 -1
  237. package/lib/typescript/src/utilities.d.ts +79 -39
  238. package/lib/typescript/src/utilities.d.ts.map +1 -1
  239. package/lib/typescript/src/validatePathConfig.d.ts.map +1 -1
  240. package/package.json +26 -22
  241. package/src/BaseNavigationContainer.tsx +468 -333
  242. package/src/ConsumedParamsContext.tsx +5 -0
  243. package/src/DataLoading.tsx +206 -0
  244. package/src/NavigationBuilderContext.tsx +44 -25
  245. package/src/NavigationFocusedRouteStateContext.tsx +4 -4
  246. package/src/NavigationIndependentTree.tsx +60 -12
  247. package/src/NavigationProvider.tsx +47 -20
  248. package/src/NavigationRootContext.tsx +11 -0
  249. package/src/NavigationStateContext.tsx +9 -6
  250. package/src/PreventRemoveContext.tsx +1 -0
  251. package/src/PreventRemoveProvider.tsx +55 -56
  252. package/src/SceneView.tsx +14 -40
  253. package/src/StaticNavigation.tsx +715 -328
  254. package/src/StaticTreeContext.tsx +7 -0
  255. package/src/createNavigationContainerRef.tsx +12 -11
  256. package/src/createNavigatorFactory.tsx +92 -8
  257. package/src/findFocusedRoute.tsx +16 -16
  258. package/src/getActionFromState.tsx +87 -51
  259. package/src/getPathFromState.tsx +310 -134
  260. package/src/getPatternParts.tsx +98 -16
  261. package/src/getStateFromPath.tsx +971 -344
  262. package/src/getStateFromRouteParams.tsx +59 -0
  263. package/src/index.tsx +57 -6
  264. package/src/internal.tsx +2 -0
  265. package/src/isRecordEqual.tsx +3 -1
  266. package/src/queryString.ts +109 -0
  267. package/src/theming/useTheme.tsx +1 -1
  268. package/src/types.tsx +799 -377
  269. package/src/useDescriptors.tsx +151 -43
  270. package/src/useEventEmitter.tsx +53 -39
  271. package/src/useFocusEffect.tsx +5 -2
  272. package/src/useFocusEvents.tsx +19 -8
  273. package/src/useFocusedListenersChildrenAdapter.tsx +3 -3
  274. package/src/useIsFocused.tsx +18 -21
  275. package/src/useKeyedChildListeners.tsx +5 -2
  276. package/src/useLazyValue.tsx +2 -2
  277. package/src/useNavigation.tsx +9 -26
  278. package/src/useNavigationBuilder.tsx +370 -201
  279. package/src/useNavigationCache.tsx +148 -123
  280. package/src/useNavigationHelpers.tsx +27 -37
  281. package/src/useNavigationIndependentTree.tsx +1 -1
  282. package/src/useNavigationState.tsx +207 -69
  283. package/src/useOnAction.tsx +92 -18
  284. package/src/useOnGetNavigation.tsx +30 -0
  285. package/src/useOnGetState.tsx +8 -4
  286. package/src/useOnPreventRemove.tsx +78 -43
  287. package/src/useOnRouteFocus.tsx +11 -15
  288. package/src/useOptionsGetters.tsx +41 -28
  289. package/src/usePreventRemove.tsx +13 -5
  290. package/src/usePreventRemoveContext.tsx +2 -2
  291. package/src/useRegisterNavigator.tsx +4 -4
  292. package/src/useRoute.tsx +20 -6
  293. package/src/useRouteCache.tsx +57 -35
  294. package/src/useScheduleUpdate.tsx +13 -11
  295. package/src/useStateForPath.tsx +1 -1
  296. package/src/useSyncState.tsx +39 -15
  297. package/src/utilities.tsx +151 -35
  298. package/src/validatePathConfig.tsx +86 -57
  299. package/lib/module/CurrentRenderContext.js +0 -10
  300. package/lib/module/CurrentRenderContext.js.map +0 -1
  301. package/lib/module/NavigationMetaContext.js +0 -15
  302. package/lib/module/NavigationMetaContext.js.map +0 -1
  303. package/lib/module/checkSerializable.js +0 -46
  304. package/lib/module/checkSerializable.js.map +0 -1
  305. package/lib/module/useComponent.js +0 -34
  306. package/lib/module/useComponent.js.map +0 -1
  307. package/lib/module/useCurrentRender.js +0 -19
  308. package/lib/module/useCurrentRender.js.map +0 -1
  309. package/lib/typescript/src/CurrentRenderContext.d.ts +0 -9
  310. package/lib/typescript/src/CurrentRenderContext.d.ts.map +0 -1
  311. package/lib/typescript/src/NavigationMetaContext.d.ts +0 -12
  312. package/lib/typescript/src/NavigationMetaContext.d.ts.map +0 -1
  313. package/lib/typescript/src/checkSerializable.d.ts +0 -10
  314. package/lib/typescript/src/checkSerializable.d.ts.map +0 -1
  315. package/lib/typescript/src/useComponent.d.ts +0 -7
  316. package/lib/typescript/src/useComponent.d.ts.map +0 -1
  317. package/lib/typescript/src/useCurrentRender.d.ts +0 -14
  318. package/lib/typescript/src/useCurrentRender.d.ts.map +0 -1
  319. package/src/CurrentRenderContext.tsx +0 -9
  320. package/src/NavigationMetaContext.tsx +0 -14
  321. package/src/checkSerializable.tsx +0 -74
  322. package/src/useComponent.tsx +0 -37
  323. package/src/useCurrentRender.tsx +0 -31
@@ -0,0 +1,7 @@
1
+ import * as React from 'react';
2
+
3
+ import type { TreeForPathConfig } from './StaticNavigation';
4
+
5
+ export const StaticTreeContext = React.createContext<
6
+ TreeForPathConfig | undefined
7
+ >(undefined);
@@ -19,17 +19,17 @@ export function createNavigationContainerRef<
19
19
  'removeListener',
20
20
  'resetRoot',
21
21
  'dispatch',
22
- 'isFocused',
23
22
  'canGoBack',
24
23
  'getRootState',
25
24
  'getState',
26
- 'getParent',
27
25
  'getCurrentRoute',
28
26
  'getCurrentOptions',
29
27
  ] as const;
30
28
 
31
29
  const listeners: Record<string, ((...args: any[]) => void)[]> = {};
32
30
 
31
+ let current: NavigationContainerRef<ParamList> | null = null;
32
+
33
33
  const removeListener = (
34
34
  event: string,
35
35
  callback: (...args: any[]) => void
@@ -37,9 +37,12 @@ export function createNavigationContainerRef<
37
37
  if (listeners[event]) {
38
38
  listeners[event] = listeners[event].filter((cb) => cb !== callback);
39
39
  }
40
- };
41
40
 
42
- let current: NavigationContainerRef<ParamList> | null = null;
41
+ current?.removeListener(
42
+ event as keyof NavigationContainerEventMap,
43
+ callback
44
+ );
45
+ };
43
46
 
44
47
  const ref: NavigationContainerRefWithCurrent<ParamList> = {
45
48
  get current() {
@@ -68,7 +71,11 @@ export function createNavigationContainerRef<
68
71
  },
69
72
  ...methods.reduce<any>((acc, name) => {
70
73
  acc[name] = (...args: any[]) => {
71
- if (current == null) {
74
+ if (name === 'removeListener') {
75
+ const [event, callback] = args;
76
+
77
+ removeListener(event, callback);
78
+ } else if (current == null) {
72
79
  switch (name) {
73
80
  case 'addListener': {
74
81
  const [event, callback] = args;
@@ -78,12 +85,6 @@ export function createNavigationContainerRef<
78
85
 
79
86
  return () => removeListener(event, callback);
80
87
  }
81
- case 'removeListener': {
82
- const [event, callback] = args;
83
-
84
- removeListener(event, callback);
85
- break;
86
- }
87
88
  default:
88
89
  console.error(NOT_INITIALIZED_ERROR);
89
90
  }
@@ -1,23 +1,107 @@
1
- import type * as React from 'react';
1
+ import type { ParamListBase } from '@react-navigation/routers';
2
+ import * as React from 'react';
2
3
 
3
4
  import { Group } from './Group';
4
5
  import { Screen } from './Screen';
6
+ import {
7
+ createComponentForStaticConfig,
8
+ type NoExcessStaticConfig,
9
+ type StaticConfig,
10
+ type StaticParamList,
11
+ } from './StaticNavigation';
12
+ import type {
13
+ NavigatorTypeBagBase,
14
+ NavigatorTypeBagFor,
15
+ NestedNavigatorsForParamList,
16
+ TypedNavigator,
17
+ } from './types';
18
+ import type { KeysOf, NoExcessObject } from './utilities';
19
+
20
+ type NavigatorNesting<ParamList extends ParamListBase, Nesting> = {
21
+ [RouteName in keyof ParamList]?: unknown;
22
+ } & Record<Exclude<KeysOf<Nesting>, keyof ParamList>, never>;
23
+
24
+ type ValidNavigatorNesting<ParamList extends ParamListBase, Nesting> =
25
+ Nesting extends NavigatorNesting<ParamList, Nesting> ? Nesting : never;
26
+
27
+ type AnyStaticConfig<TypeBag extends NavigatorTypeBagBase> = StaticConfig<
28
+ NavigatorTypeBagFor<TypeBag, ParamListBase>
29
+ >;
30
+
31
+ export type TypedNavigatorFactory<in out TypeBag extends NavigatorTypeBagBase> =
32
+ {
33
+ <
34
+ const ParamList extends ParamListBase,
35
+ const Nesting extends NavigatorNesting<ParamList, Nesting> =
36
+ ValidNavigatorNesting<
37
+ ParamList,
38
+ NestedNavigatorsForParamList<ParamList>
39
+ >,
40
+ >(): TypedNavigator<
41
+ NavigatorTypeBagFor<TypeBag, ParamList>,
42
+ undefined,
43
+ Nesting
44
+ >;
45
+ <
46
+ const Config extends AnyStaticConfig<TypeBag> &
47
+ NoExcessObject<Config, AnyStaticConfig<TypeBag>>,
48
+ >(
49
+ config: Config & NoExcessStaticConfig<NoInfer<Config>, TypeBag>
50
+ ): TypedNavigator<
51
+ NavigatorTypeBagFor<TypeBag, StaticParamList<{ config: Config }>>,
52
+ Config
53
+ >;
54
+ };
5
55
 
6
56
  /**
7
- * Higher order component to create a `Navigator` and `Screen` pair.
8
- * Custom navigators should wrap the navigator component in `createNavigator` before exporting.
57
+ * Higher order function to create a navigator factory
58
+ * A navigator factory creates a navigator:
59
+ * - `Navigator` and `Screen` pairs for dynamic configuration
60
+ * - Static navigator object for static configuration
9
61
  *
10
62
  * @param Navigator The navigator component to wrap.
11
- * @returns Factory method to create a `Navigator` and `Screen` pair.
63
+ * @returns Factory method to create a navigator
12
64
  */
13
- export function createNavigatorFactory(Navigator: React.ComponentType<any>) {
65
+ export function createNavigatorFactory<TypeBag extends NavigatorTypeBagBase>(
66
+ Navigator: React.ComponentType<any>
67
+ ): TypedNavigatorFactory<TypeBag> {
68
+ const displayName = Navigator.displayName ?? Navigator.name ?? 'Navigator';
69
+
14
70
  function createNavigator(config?: any): any {
15
71
  if (config != null) {
72
+ const NavigatorComponent = createComponentForStaticConfig(
73
+ {
74
+ Navigator,
75
+ Screen,
76
+ Group,
77
+ config,
78
+ },
79
+ displayName
80
+ );
81
+
16
82
  return {
17
- Navigator,
18
- Screen,
19
- Group,
20
83
  config,
84
+ with(
85
+ DecoratorComponent: React.ComponentType<{
86
+ Navigator: React.ComponentType<any>;
87
+ }>
88
+ ) {
89
+ const WithComponent = () => {
90
+ return React.createElement(DecoratorComponent, {
91
+ Navigator: NavigatorComponent,
92
+ });
93
+ };
94
+
95
+ WithComponent.displayName = `${displayName}With`;
96
+
97
+ return {
98
+ config,
99
+ getComponent: () => WithComponent,
100
+ };
101
+ },
102
+ getComponent() {
103
+ return NavigatorComponent;
104
+ },
21
105
  };
22
106
  }
23
107
 
@@ -1,22 +1,22 @@
1
- import type { InitialState } from '@react-navigation/routers';
1
+ import type { NavigationState, PartialState } from '@react-navigation/routers';
2
2
 
3
- type Result =
4
- | {
5
- key?: string;
6
- name: string;
7
- params?: object;
8
- path?: string;
9
- }
10
- | undefined;
3
+ type State = NavigationState | PartialState<NavigationState>;
11
4
 
12
- export function findFocusedRoute(state: InitialState): Result {
13
- let current: InitialState | undefined = state;
5
+ export function findFocusedRoute(
6
+ state: State
7
+ ): State['routes'][number] | undefined {
8
+ let current: State | undefined = state;
14
9
 
15
- while (current?.routes[current.index ?? 0].state != null) {
16
- current = current.routes[current.index ?? 0].state;
17
- }
10
+ while (current != null) {
11
+ const route: State['routes'][number] | undefined =
12
+ current.routes[current.index ?? current.routes.length - 1];
18
13
 
19
- const route = current?.routes[current?.index ?? 0];
14
+ if (route?.state == null) {
15
+ return route;
16
+ }
17
+
18
+ current = route.state;
19
+ }
20
20
 
21
- return route;
21
+ return undefined;
22
22
  }
@@ -2,20 +2,18 @@ import type {
2
2
  CommonActions,
3
3
  NavigationState,
4
4
  ParamListBase,
5
- PartialRoute,
6
5
  PartialState,
7
- Route,
8
6
  } from '@react-navigation/routers';
9
7
 
10
8
  import type { NavigatorScreenParams, PathConfig, PathConfigMap } from './types';
11
9
 
12
10
  type ConfigItem = {
13
- initialRouteName?: string;
14
- screens?: Record<string, ConfigItem>;
11
+ initialRouteName?: string | undefined;
12
+ screens?: Record<string, ConfigItem> | undefined;
15
13
  };
16
14
 
17
15
  type Options = {
18
- initialRouteName?: string;
16
+ initialRouteName?: string | undefined;
19
17
  screens: PathConfigMap<object>;
20
18
  };
21
19
 
@@ -23,34 +21,60 @@ type NavigateAction<State extends NavigationState> = {
23
21
  type: 'NAVIGATE';
24
22
  payload: {
25
23
  name: string;
26
- params?: NavigatorScreenParams<State>;
27
- path?: string;
24
+ params?: NavigatorScreenParams<State> | undefined;
25
+ path?: string | undefined;
28
26
  };
29
27
  };
30
28
 
29
+ // Cache the normalized config across calls for the same `options` reference,
30
+ // so we don't re-walk the config tree on every action conversion.
31
+ const cachedNormalizedConfig = new WeakMap<Options, ConfigItem>();
32
+
31
33
  export function getActionFromState(
32
34
  state: PartialState<NavigationState>,
33
35
  options?: Options
34
36
  ): NavigateAction<NavigationState> | CommonActions.Action | undefined {
35
- // Create a normalized configs object which will be easier to use
36
- const normalizedConfig = options
37
- ? createNormalizedConfigItem(options as PathConfig<object> | string)
38
- : {};
37
+ let normalizedConfig;
38
+
39
+ if (options) {
40
+ const cached = cachedNormalizedConfig.get(options);
41
+
42
+ if (cached) {
43
+ normalizedConfig = cached;
44
+ } else {
45
+ normalizedConfig = createNormalizedConfigItem(
46
+ options as PathConfig<object> | string
47
+ );
48
+ cachedNormalizedConfig.set(options, normalizedConfig as ConfigItem);
49
+ }
50
+ } else {
51
+ normalizedConfig = {};
52
+ }
39
53
 
40
- const routes =
41
- state.index != null ? state.routes.slice(0, state.index + 1) : state.routes;
54
+ const routesLength =
55
+ state.index == null || state.index >= state.routes.length
56
+ ? state.routes.length
57
+ : state.index + 1;
42
58
 
43
- if (routes.length === 0) {
59
+ if (routesLength === 0) {
44
60
  return undefined;
45
61
  }
46
62
 
63
+ const firstRoute = state.routes[0];
64
+
65
+ if (firstRoute == null) {
66
+ return undefined;
67
+ }
68
+
69
+ const secondRoute = state.routes[1];
70
+
47
71
  if (
48
72
  !(
49
- (routes.length === 1 && routes[0].key === undefined) ||
50
- (routes.length === 2 &&
51
- routes[0].key === undefined &&
52
- routes[0].name === normalizedConfig?.initialRouteName &&
53
- routes[1].key === undefined)
73
+ (routesLength === 1 && firstRoute.key == null) ||
74
+ (routesLength === 2 &&
75
+ firstRoute.key == null &&
76
+ firstRoute.name === normalizedConfig?.initialRouteName &&
77
+ secondRoute?.key == null)
54
78
  )
55
79
  ) {
56
80
  return {
@@ -61,20 +85,21 @@ export function getActionFromState(
61
85
 
62
86
  const route = state.routes[state.index ?? state.routes.length - 1];
63
87
 
64
- let current: PartialState<NavigationState> | undefined = route?.state;
65
- let config: ConfigItem | undefined = normalizedConfig?.screens?.[route?.name];
88
+ if (route == null) {
89
+ return undefined;
90
+ }
91
+
92
+ let current: NavigationState | PartialState<NavigationState> | undefined =
93
+ route.state;
94
+ let config: ConfigItem | undefined = normalizedConfig?.screens?.[route.name];
66
95
  let params = { ...route.params } as NavigatorScreenParams<ParamListBase>;
67
96
 
68
- const payload:
69
- | {
70
- name: string;
71
- params: NavigatorScreenParams<ParamListBase>;
72
- path?: string;
73
- pop?: boolean;
74
- }
75
- | undefined = route
76
- ? { name: route.name, path: route.path, params }
77
- : undefined;
97
+ const payload: {
98
+ name: string;
99
+ params: NavigatorScreenParams<ParamListBase>;
100
+ path?: string | undefined;
101
+ pop?: boolean | undefined;
102
+ } = { name: route.name, path: route.path, params };
78
103
 
79
104
  // If the screen contains a navigator, pop other screens to navigate to it
80
105
  // This avoid pushing multiple instances of navigators onto a stack
@@ -90,7 +115,7 @@ export function getActionFromState(
90
115
  // There are 2 ways we can detect if a screen contains a navigator:
91
116
  // - The route contains nested state in `route.state`
92
117
  // - Nested screens are defined in the config
93
- if (payload && config?.screens && Object.keys(config.screens).length) {
118
+ if (config?.screens && Object.keys(config.screens).length) {
94
119
  payload.pop = true;
95
120
  }
96
121
 
@@ -99,13 +124,24 @@ export function getActionFromState(
99
124
  return undefined;
100
125
  }
101
126
 
102
- const routes =
103
- current.index != null
104
- ? current.routes.slice(0, current.index + 1)
105
- : current.routes;
127
+ const routesLength =
128
+ current.index == null || current.index >= current.routes.length
129
+ ? current.routes.length
130
+ : current.index + 1;
131
+
132
+ const route = current.routes[routesLength - 1];
133
+
134
+ if (route == null) {
135
+ return undefined;
136
+ }
137
+
138
+ const firstRoute = current.routes[0];
139
+
140
+ if (firstRoute == null) {
141
+ return undefined;
142
+ }
106
143
 
107
- const route: Route<string> | PartialRoute<Route<string>> =
108
- routes[routes.length - 1];
144
+ const secondRoute = current.routes[1];
109
145
 
110
146
  // Explicitly set to override existing value when merging params
111
147
  Object.assign(params, {
@@ -115,14 +151,14 @@ export function getActionFromState(
115
151
  state: undefined,
116
152
  });
117
153
 
118
- if (routes.length === 1 && routes[0].key === undefined) {
154
+ if (routesLength === 1 && firstRoute.key == null) {
119
155
  params.initial = true;
120
156
  params.screen = route.name;
121
157
  } else if (
122
- routes.length === 2 &&
123
- routes[0].key === undefined &&
124
- routes[0].name === config?.initialRouteName &&
125
- routes[1].key === undefined
158
+ routesLength === 2 &&
159
+ firstRoute.key == null &&
160
+ firstRoute.name === config?.initialRouteName &&
161
+ secondRoute?.key == null
126
162
  ) {
127
163
  params.initial = false;
128
164
  params.screen = route.name;
@@ -148,14 +184,10 @@ export function getActionFromState(
148
184
  }
149
185
  }
150
186
 
151
- if (payload?.params.screen || payload?.params.state) {
187
+ if (payload.params.screen || payload.params.state) {
152
188
  payload.pop = true;
153
189
  }
154
190
 
155
- if (!payload) {
156
- return;
157
- }
158
-
159
191
  // Try to construct payload for a `NAVIGATE` action from the state
160
192
  // This lets us preserve the navigation state and not lose it
161
193
  return {
@@ -164,11 +196,16 @@ export function getActionFromState(
164
196
  };
165
197
  }
166
198
 
167
- const createNormalizedConfigItem = (config: PathConfig<object> | string) =>
199
+ const createNormalizedConfigItem = (
200
+ config: PathConfig<object> | string
201
+ ): ConfigItem =>
168
202
  typeof config === 'object' && config != null
169
203
  ? {
170
204
  initialRouteName:
171
- 'initialRouteName' in config ? config.initialRouteName : undefined,
205
+ 'initialRouteName' in config &&
206
+ typeof config.initialRouteName === 'string'
207
+ ? config.initialRouteName
208
+ : undefined,
172
209
  screens:
173
210
  'screens' in config && config.screens != null
174
211
  ? createNormalizedConfigs(config.screens)
@@ -178,7 +215,6 @@ const createNormalizedConfigItem = (config: PathConfig<object> | string) =>
178
215
 
179
216
  const createNormalizedConfigs = (options: PathConfigMap<object>) =>
180
217
  Object.entries(options).reduce<Record<string, ConfigItem>>((acc, [k, v]) => {
181
- // @ts-expect-error -we can't properly type this
182
218
  acc[k] = createNormalizedConfigItem(v);
183
219
  return acc;
184
220
  }, {});