@react-navigation/core 7.3.1 → 7.5.0

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 (43) hide show
  1. package/lib/commonjs/deepFreeze.js +6 -3
  2. package/lib/commonjs/deepFreeze.js.map +1 -1
  3. package/lib/commonjs/getActionFromState.js +25 -0
  4. package/lib/commonjs/getActionFromState.js.map +1 -1
  5. package/lib/commonjs/types.js.map +1 -1
  6. package/lib/commonjs/useNavigationBuilder.js +13 -10
  7. package/lib/commonjs/useNavigationBuilder.js.map +1 -1
  8. package/lib/module/deepFreeze.js +6 -3
  9. package/lib/module/deepFreeze.js.map +1 -1
  10. package/lib/module/getActionFromState.js +25 -0
  11. package/lib/module/getActionFromState.js.map +1 -1
  12. package/lib/module/types.js.map +1 -1
  13. package/lib/module/useNavigationBuilder.js +13 -10
  14. package/lib/module/useNavigationBuilder.js.map +1 -1
  15. package/lib/typescript/commonjs/src/deepFreeze.d.ts.map +1 -1
  16. package/lib/typescript/commonjs/src/getActionFromState.d.ts.map +1 -1
  17. package/lib/typescript/commonjs/src/types.d.ts +16 -4
  18. package/lib/typescript/commonjs/src/types.d.ts.map +1 -1
  19. package/lib/typescript/commonjs/src/useDescriptors.d.ts +2 -0
  20. package/lib/typescript/commonjs/src/useDescriptors.d.ts.map +1 -1
  21. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts +4 -1
  22. package/lib/typescript/commonjs/src/useNavigationBuilder.d.ts.map +1 -1
  23. package/lib/typescript/commonjs/src/useNavigationCache.d.ts +1 -0
  24. package/lib/typescript/commonjs/src/useNavigationCache.d.ts.map +1 -1
  25. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts +1 -0
  26. package/lib/typescript/commonjs/src/useNavigationHelpers.d.ts.map +1 -1
  27. package/lib/typescript/module/src/deepFreeze.d.ts.map +1 -1
  28. package/lib/typescript/module/src/getActionFromState.d.ts.map +1 -1
  29. package/lib/typescript/module/src/types.d.ts +16 -4
  30. package/lib/typescript/module/src/types.d.ts.map +1 -1
  31. package/lib/typescript/module/src/useDescriptors.d.ts +2 -0
  32. package/lib/typescript/module/src/useDescriptors.d.ts.map +1 -1
  33. package/lib/typescript/module/src/useNavigationBuilder.d.ts +4 -1
  34. package/lib/typescript/module/src/useNavigationBuilder.d.ts.map +1 -1
  35. package/lib/typescript/module/src/useNavigationCache.d.ts +1 -0
  36. package/lib/typescript/module/src/useNavigationCache.d.ts.map +1 -1
  37. package/lib/typescript/module/src/useNavigationHelpers.d.ts +1 -0
  38. package/lib/typescript/module/src/useNavigationHelpers.d.ts.map +1 -1
  39. package/package.json +3 -3
  40. package/src/deepFreeze.tsx +6 -3
  41. package/src/getActionFromState.tsx +35 -1
  42. package/src/types.tsx +18 -5
  43. package/src/useNavigationBuilder.tsx +25 -21
package/src/types.tsx CHANGED
@@ -6,6 +6,7 @@ import type {
6
6
  ParamListBase,
7
7
  PartialState,
8
8
  Route,
9
+ Router,
9
10
  } from '@react-navigation/routers';
10
11
  import type * as React from 'react';
11
12
 
@@ -108,11 +109,15 @@ export type DefaultNavigatorOptions<
108
109
  }) => React.ReactElement;
109
110
 
110
111
  /**
111
- A function returning a state, which may be set after modifying the routes name.
112
+ * A function returning overrides for the underlying router used by the navigator.
113
+ * The overrides will be shallow merged into the original router.
114
+ * It receives the original router as an argument to the function.
115
+ *
116
+ * This must be a pure function and cannot reference outside dynamic variables.
112
117
  */
113
- UNSTABLE_getStateForRouteNamesChange?: (
114
- state: NavigationState
115
- ) => PartialState<NavigationState> | undefined;
118
+ UNSTABLE_router?: <Action extends NavigationAction>(
119
+ original: Router<State, Action>
120
+ ) => Partial<Router<State, Action>>;
116
121
  } & (NavigatorID extends string
117
122
  ? {
118
123
  /**
@@ -271,7 +276,11 @@ type NavigationHelpersCommon<
271
276
  /**
272
277
  * Navigate to a route in current navigation tree.
273
278
  *
274
- * @param options Object with `name` for the route to navigate to, and a `params` object.
279
+ * @param options.name Name of the route to navigate to.
280
+ * @param [options.params] Params object for the route.
281
+ * @param [options.path] Path to associate the route with (e.g. for deep links).
282
+ * @param [options.merge] Whether to merge the params onto the route.
283
+ * @param [options.pop] Whether to pop routes in a stack to go back to the matching route.
275
284
  */
276
285
  navigate<RouteName extends keyof ParamList>(
277
286
  options: {
@@ -280,6 +289,7 @@ type NavigationHelpersCommon<
280
289
  params: ParamList[Screen];
281
290
  path?: string;
282
291
  merge?: boolean;
292
+ pop?: boolean;
283
293
  };
284
294
  }[RouteName]
285
295
  ): void;
@@ -950,6 +960,7 @@ export type NavigatorScreenParams<ParamList extends {}> =
950
960
  screen?: never;
951
961
  params?: never;
952
962
  initial?: never;
963
+ pop?: never;
953
964
  path?: string;
954
965
  state: PartialState<NavigationState> | NavigationState | undefined;
955
966
  }
@@ -960,6 +971,7 @@ export type NavigatorScreenParams<ParamList extends {}> =
960
971
  params?: ParamList[RouteName];
961
972
  initial?: boolean;
962
973
  path?: string;
974
+ pop?: boolean;
963
975
  state?: never;
964
976
  }
965
977
  : {
@@ -967,6 +979,7 @@ export type NavigatorScreenParams<ParamList extends {}> =
967
979
  params: ParamList[RouteName];
968
980
  initial?: boolean;
969
981
  path?: string;
982
+ pop?: boolean;
970
983
  state?: never;
971
984
  };
972
985
  }[keyof ParamList];
@@ -254,7 +254,7 @@ export function useNavigationBuilder<
254
254
  ScreenOptions extends {},
255
255
  EventMap extends Record<string, any>,
256
256
  >(
257
- createRouter: RouterFactory<State, any, RouterOptions>,
257
+ createRouter: RouterFactory<State, NavigationAction, RouterOptions>,
258
258
  options: DefaultNavigatorOptions<
259
259
  ParamListBase,
260
260
  string | undefined,
@@ -277,6 +277,7 @@ export function useNavigationBuilder<
277
277
  screenOptions,
278
278
  screenLayout,
279
279
  screenListeners,
280
+ UNSTABLE_router,
280
281
  ...rest
281
282
  } = options;
282
283
 
@@ -298,7 +299,18 @@ export function useNavigationBuilder<
298
299
  );
299
300
  }
300
301
 
301
- return createRouter(rest as unknown as RouterOptions);
302
+ const original = createRouter(rest as unknown as RouterOptions);
303
+
304
+ if (UNSTABLE_router != null) {
305
+ const overrides = UNSTABLE_router(original);
306
+
307
+ return {
308
+ ...original,
309
+ ...overrides,
310
+ };
311
+ }
312
+
313
+ return original;
302
314
  });
303
315
 
304
316
  const screens = routeConfigs.reduce<
@@ -488,26 +500,17 @@ export function useNavigationBuilder<
488
500
  !isArrayEqual(state.routeNames, routeNames) ||
489
501
  !isRecordEqual(routeKeyList, previousRouteKeyList)
490
502
  ) {
491
- const navigatorStateForNextRouteNamesChange =
492
- options.UNSTABLE_getStateForRouteNamesChange?.(state);
493
503
  // When the list of route names change, the router should handle it to remove invalid routes
494
- nextState = navigatorStateForNextRouteNamesChange
495
- ? // @ts-expect-error this is ok
496
- router.getRehydratedState(navigatorStateForNextRouteNamesChange, {
497
- routeNames,
498
- routeParamList,
499
- routeGetIdList,
500
- })
501
- : router.getStateForRouteNamesChange(state, {
502
- routeNames,
503
- routeParamList,
504
- routeGetIdList,
505
- routeKeyChanges: Object.keys(routeKeyList).filter(
506
- (name) =>
507
- name in previousRouteKeyList &&
508
- routeKeyList[name] !== previousRouteKeyList[name]
509
- ),
510
- });
504
+ nextState = router.getStateForRouteNamesChange(state, {
505
+ routeNames,
506
+ routeParamList,
507
+ routeGetIdList,
508
+ routeKeyChanges: Object.keys(routeKeyList).filter(
509
+ (name) =>
510
+ name in previousRouteKeyList &&
511
+ routeKeyList[name] !== previousRouteKeyList[name]
512
+ ),
513
+ });
511
514
  }
512
515
 
513
516
  const previousNestedParamsRef = React.useRef(route?.params);
@@ -538,6 +541,7 @@ export function useNavigationBuilder<
538
541
  name: route.params.screen,
539
542
  params: route.params.params,
540
543
  path: route.params.path,
544
+ pop: route.params.pop,
541
545
  });
542
546
  }
543
547