@ionic/react-router 9.0.15-dev.11796226497.1324df76 → 9.0.15-dev.11796247574.1ecbe59d

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/dist/index.js CHANGED
@@ -1,8 +1,7 @@
1
1
  import React, { useRef, useState, useEffect, useCallback } from 'react';
2
- import { Route, matchPath as matchPath$1, Routes, Navigate, UNSAFE_RouteContext, matchRoutes, useLocation, useNavigate, BrowserRouter, useNavigationType, HashRouter } from 'react-router-dom';
2
+ import { Route, matchPath as matchPath$1, Routes, Navigate, UNSAFE_RouteContext, matchRoutes, useLocation, useNavigate, BrowserRouter, useNavigationType, MemoryRouter, HashRouter } from 'react-router-dom';
3
3
  import { IonRoute, ViewStacks, generateId, ViewLifeCycleManager, StackContext, createDebugLogger, RouteManagerContext, getConfig, LocationHistory, NavManager } from '@ionic/react';
4
4
  import { LIFECYCLE_WILL_LEAVE, LIFECYCLE_DID_LEAVE, LIFECYCLE_WILL_ENTER, LIFECYCLE_DID_ENTER } from '@ionic/core/components';
5
- import { MemoryRouter, useLocation as useLocation$1, useNavigationType as useNavigationType$1 } from 'react-router';
6
5
 
7
6
  const IonRouteInner = ({ path, index, caseSensitive, element }) => {
8
7
  return React.createElement(Route, { path: path, index: index, caseSensitive: caseSensitive, element: element });
@@ -646,14 +645,16 @@ const getFallbackParamsFromViewItems = (allViewItems, currentOutletId, currentPa
646
645
  return params;
647
646
  };
648
647
  /**
649
- * Builds the matches array for RouteContext.
648
+ * Builds the matches array for RouteContext. The leaf `pathname` uses the absolute base,
649
+ * because the raw match is relative for a route without a leading slash, which breaks
650
+ * relative links, and includes a splat's tail, which loops an index `<Navigate>`.
650
651
  */
651
- const buildContextMatches = (parentMatches, combinedParams, routeMatch, routeInfoPathname, absolutePathnameBase, viewItem, routeElement, componentElement) => {
652
+ const buildContextMatches = (parentMatches, combinedParams, absolutePathnameBase, viewItem, routeElement, componentElement) => {
652
653
  return [
653
654
  ...parentMatches,
654
655
  {
655
656
  params: combinedParams,
656
- pathname: routeMatch?.pathname || routeInfoPathname,
657
+ pathname: absolutePathnameBase,
657
658
  pathnameBase: absolutePathnameBase,
658
659
  route: {
659
660
  id: viewItem.id,
@@ -1061,7 +1062,7 @@ class ReactRouterViewStack extends ViewStacks {
1061
1062
  const combinedParams = { ...accumulatedParentParams, ...(routeMatch?.params ?? {}) };
1062
1063
  const parentPathnameBase = parentMatches.length > 0 ? parentMatches[parentMatches.length - 1].pathnameBase : '/';
1063
1064
  const absolutePathnameBase = computeAbsolutePathnameBase(routeElement, routeMatch, parentPathnameBase, routeInfo.pathname);
1064
- const contextMatches = buildContextMatches(parentMatches, combinedParams, routeMatch, routeInfo.pathname, absolutePathnameBase, viewItem, routeElement, componentElement);
1065
+ const contextMatches = buildContextMatches(parentMatches, combinedParams, absolutePathnameBase, viewItem, routeElement, componentElement);
1065
1066
  const routeContextValue = parentContext
1066
1067
  ? { ...parentContext, matches: contextMatches }
1067
1068
  : { outlet: null, matches: contextMatches, isDataRoute: false };
@@ -1959,15 +1960,8 @@ class StackManager extends React.PureComponent {
1959
1960
  return;
1960
1961
  }
1961
1962
  this.lastTransition = currentTransition;
1962
- // A page-less leaving view (e.g. a redirect) has nothing to animate out,
1963
- // so transition the page shown before it instead. Look it up once and reuse
1964
- // it for cleanup: applySkipAnimationIfNeeded may hide it, and the fallback
1965
- // lookup only matches visible pages.
1966
- const pageBehindPagelessView = this.findPageBehindPagelessView(routeInfo, enteringViewItem, leavingViewItem);
1967
- const pageBehindIsVisible = pageBehindPagelessView?.ionPageElement && isViewVisible(pageBehindPagelessView.ionPageElement);
1968
- const transitionLeavingViewItem = pageBehindIsVisible ? pageBehindPagelessView : leavingViewItem;
1969
- const shouldSkipAnimation = this.applySkipAnimationIfNeeded(enteringViewItem, transitionLeavingViewItem);
1970
- this.transitionPage(routeInfo, enteringViewItem, transitionLeavingViewItem, undefined, false, shouldSkipAnimation);
1963
+ const shouldSkipAnimation = this.applySkipAnimationIfNeeded(enteringViewItem, leavingViewItem);
1964
+ this.transitionPage(routeInfo, enteringViewItem, leavingViewItem, undefined, false, shouldSkipAnimation);
1971
1965
  if (shouldUnmountLeavingViewItem && leavingViewItem && enteringViewItem !== leavingViewItem) {
1972
1966
  // For replace actions, skip setting mount=false here. handleLeavingViewUnmount
1973
1967
  // sets it only after its container-to-container guard passes, avoiding zombie state.
@@ -1989,8 +1983,7 @@ class StackManager extends React.PureComponent {
1989
1983
  }
1990
1984
  this.handleLeavingViewUnmount(routeInfo, enteringViewItem, leavingViewItem);
1991
1985
  }
1992
- this.cleanupPageBehindPagelessView(routeInfo, pageBehindPagelessView);
1993
- // Clean up orphaned sibling views after replace actions or push-to-container navigations
1986
+ // Clean up orphaned sibling views after replace actions (redirects)
1994
1987
  this.cleanupOrphanedSiblingViews(routeInfo, enteringViewItem, leavingViewItem);
1995
1988
  // On a fresh push, browser forward history is invalidated. Any views we
1996
1989
  // previously preserved on pop (to support forward navigation) are now
@@ -2020,8 +2013,17 @@ class StackManager extends React.PureComponent {
2020
2013
  this.preservedViewItems.delete(viewItem);
2021
2014
  continue;
2022
2015
  }
2016
+ viewItem.mount = false;
2023
2017
  this.preservedViewItems.delete(viewItem);
2024
- this.scheduleViewUnmount(viewItem);
2018
+ const viewToUnmount = viewItem;
2019
+ setTimeout(() => {
2020
+ // Skip if a follow-up transition re-entered the view (mount flipped back to true).
2021
+ if (viewToUnmount.mount) {
2022
+ return;
2023
+ }
2024
+ this.context.unMountViewItem(viewToUnmount);
2025
+ this.forceUpdate();
2026
+ }, VIEW_UNMOUNT_DELAY_MS);
2025
2027
  }
2026
2028
  }
2027
2029
  /**
@@ -2033,7 +2035,16 @@ class StackManager extends React.PureComponent {
2033
2035
  return;
2034
2036
  }
2035
2037
  if (!leavingViewItem.ionPageElement) {
2036
- this.scheduleViewUnmount(leavingViewItem);
2038
+ leavingViewItem.mount = false;
2039
+ const viewToUnmount = leavingViewItem;
2040
+ setTimeout(() => {
2041
+ // Skip if a follow-up transition re-entered the view (mount flipped back to true).
2042
+ if (viewToUnmount.mount) {
2043
+ return;
2044
+ }
2045
+ this.context.unMountViewItem(viewToUnmount);
2046
+ this.forceUpdate();
2047
+ }, VIEW_UNMOUNT_DELAY_MS);
2037
2048
  return;
2038
2049
  }
2039
2050
  const enteringRoutePath = enteringViewItem.reactElement?.props?.path;
@@ -2049,106 +2060,35 @@ class StackManager extends React.PureComponent {
2049
2060
  if (isEnteringContainerRoute && !isLeavingSpecificRoute) {
2050
2061
  return;
2051
2062
  }
2052
- this.scheduleViewUnmount(leavingViewItem);
2053
- }
2054
- /**
2055
- * Marks a view unmounted and removes it after VIEW_UNMOUNT_DELAY_MS, unless a
2056
- * follow-up transition re-enters it first (mount flips back to true).
2057
- */
2058
- scheduleViewUnmount(viewItem) {
2059
- viewItem.mount = false;
2063
+ leavingViewItem.mount = false;
2064
+ const viewToUnmount = leavingViewItem;
2060
2065
  setTimeout(() => {
2061
- if (viewItem.mount) {
2066
+ // Skip if a follow-up transition re-entered the view (mount flipped back to true).
2067
+ if (viewToUnmount.mount) {
2062
2068
  return;
2063
2069
  }
2064
- this.context.unMountViewItem(viewItem);
2070
+ this.context.unMountViewItem(viewToUnmount);
2065
2071
  this.forceUpdate();
2066
2072
  }, VIEW_UNMOUNT_DELAY_MS);
2067
2073
  }
2068
2074
  /**
2069
- * Returns the ids of this outlet's views that a location history entry still
2070
- * resolves to. Those views are reachable with back.
2071
- */
2072
- getViewIdsInHistory() {
2073
- const viewIds = new Set();
2074
- for (const pathname of this.context.getLocationHistoryPathnames()) {
2075
- const viewItem = this.context.findViewItemByPathname(pathname, this.id);
2076
- if (viewItem) {
2077
- viewIds.add(viewItem.id);
2078
- }
2079
- }
2080
- return viewIds;
2081
- }
2082
- /**
2083
- * When the leaving view renders no IonPage (a redirect, or a component that
2084
- * navigates on mount), returns the page shown before it.
2085
- */
2086
- findPageBehindPagelessView(routeInfo, enteringViewItem, leavingViewItem) {
2087
- if (!leavingViewItem ||
2088
- leavingViewItem.ionPageElement ||
2089
- !routeInfo.prevRouteLastPathname ||
2090
- (routeInfo.routeAction !== 'push' && routeInfo.routeAction !== 'replace')) {
2091
- return undefined;
2092
- }
2093
- const isCandidate = (viewItem) => !!viewItem &&
2094
- viewItem !== enteringViewItem &&
2095
- viewItem !== leavingViewItem &&
2096
- viewItem.mount &&
2097
- !!viewItem.ionPageElement;
2098
- const pageBehind = this.context.findViewItemByPathname(routeInfo.prevRouteLastPathname, this.id);
2099
- if (isCandidate(pageBehind)) {
2100
- return pageBehind;
2101
- }
2102
- // In a chain of page-less redirects the previous pathname is another hop,
2103
- // often already unmounted. The page the user was on is the one still
2104
- // showing in this outlet.
2105
- return this.context
2106
- .getViewItemsForOutlet(this.id)
2107
- .find((viewItem) => isCandidate(viewItem) &&
2108
- isViewVisible(viewItem.ionPageElement) &&
2109
- !viewItem.reactElement?.props?.path?.endsWith('/*'));
2110
- }
2111
- /**
2112
- * Unmounts the page shown before a page-less view (a redirect, or a component
2113
- * that navigates on mount) unless back can still reach it, since the replace
2114
- * only unmounts the page-less view itself.
2115
- */
2116
- cleanupPageBehindPagelessView(routeInfo, pageBehindPagelessView) {
2117
- if (routeInfo.routeAction !== 'replace') {
2118
- return;
2119
- }
2120
- if (!pageBehindPagelessView || this.getViewIdsInHistory().has(pageBehindPagelessView.id)) {
2121
- return;
2122
- }
2123
- // Container views (e.g. /tabs/*) host their own nested outlets; leave them alone.
2124
- const pageBehindPagelessViewPath = pageBehindPagelessView.reactElement?.props?.path;
2125
- if (pageBehindPagelessViewPath?.endsWith('/*')) {
2126
- return;
2127
- }
2128
- // No explicit hide: the transition already animates this page out, and
2129
- // hiding it now would cut that animation short.
2130
- this.scheduleViewUnmount(pageBehindPagelessView);
2131
- }
2132
- /**
2133
- * Cleans up orphaned sibling views after replace actions or push-to-container
2134
- * navigations. A view that the location history still resolves to is
2135
- * reachable with back, so it is kept.
2075
+ * Cleans up orphaned sibling views after replace actions or push-to-container navigations.
2136
2076
  */
2137
2077
  cleanupOrphanedSiblingViews(routeInfo, enteringViewItem, leavingViewItem) {
2138
2078
  const enteringRoutePath = enteringViewItem.reactElement?.props?.path;
2139
2079
  if (!enteringRoutePath) {
2140
2080
  return;
2141
2081
  }
2142
- // A push into a container (e.g. a menu link to /tabs/*) can leave behind
2143
- // siblings that an earlier pop already took out of history
2144
- const isPushToContainer = routeInfo.routeAction === 'push' && routeInfo.routeDirection === 'none' && enteringRoutePath.endsWith('/*');
2145
- if (routeInfo.routeAction !== 'replace' && !isPushToContainer) {
2082
+ const leavingRoutePath = leavingViewItem?.reactElement?.props?.path;
2083
+ const isContainerRoute = (path) => path?.endsWith('/*');
2084
+ const isReplaceAction = routeInfo.routeAction === 'replace';
2085
+ const isPushToContainer = routeInfo.routeAction === 'push' && routeInfo.routeDirection === 'none' && isContainerRoute(enteringRoutePath);
2086
+ if (!isReplaceAction && !isPushToContainer) {
2146
2087
  return;
2147
2088
  }
2148
- const leavingRoutePath = leavingViewItem?.reactElement?.props?.path;
2149
2089
  // Skip cleanup for tab switches
2150
2090
  const isSameView = enteringViewItem === leavingViewItem;
2151
- const isSameContainerRoute = enteringRoutePath.endsWith('/*') && leavingRoutePath === enteringRoutePath;
2091
+ const isSameContainerRoute = isContainerRoute(enteringRoutePath) && leavingRoutePath === enteringRoutePath;
2152
2092
  const isNavigatingWithinContainer = isPushToContainer &&
2153
2093
  !leavingViewItem &&
2154
2094
  routeInfo.prevRouteLastPathname?.startsWith(enteringRoutePath.replace(/\/\*$/, ''));
@@ -2156,9 +2096,6 @@ class StackManager extends React.PureComponent {
2156
2096
  return;
2157
2097
  }
2158
2098
  const allViewsInOutlet = this.context.getViewItemsForOutlet(this.id);
2159
- // Views still in history are reachable with back, so they are not orphaned
2160
- // even when they are siblings of the entering route.
2161
- const viewIdsInHistory = this.getViewIdsInHistory();
2162
2099
  const areSiblingRoutes = (path1, path2) => {
2163
2100
  const path1IsRelative = !path1.startsWith('/');
2164
2101
  const path2IsRelative = !path2.startsWith('/');
@@ -2194,18 +2131,30 @@ class StackManager extends React.PureComponent {
2194
2131
  (leavingViewItem && viewItem.id === leavingViewItem.id) ||
2195
2132
  !viewItem.mount ||
2196
2133
  !viewRoutePath ||
2197
- viewIdsInHistory.has(viewItem.id) ||
2198
2134
  // Don't clean up container routes when entering a container route
2199
2135
  // (e.g., /tabs/* and /settings/* coexist for tab switching)
2200
2136
  (viewRoutePath.endsWith('/*') && enteringRoutePath.endsWith('/*'));
2201
2137
  if (shouldSkip) {
2202
2138
  continue;
2203
2139
  }
2140
+ const isOrphanedSpecificRoute = !viewRoutePath.endsWith('/*');
2204
2141
  // Clean up sibling non-container routes that are no longer reachable.
2205
- const isSpecificRoute = !viewRoutePath.endsWith('/*');
2206
- if (isSpecificRoute && areSiblingRoutes(enteringRoutePath, viewRoutePath)) {
2142
+ let shouldCleanup = false;
2143
+ if ((isReplaceAction || isPushToContainer) && isOrphanedSpecificRoute) {
2144
+ shouldCleanup = areSiblingRoutes(enteringRoutePath, viewRoutePath);
2145
+ }
2146
+ if (shouldCleanup) {
2207
2147
  hideIonPageElement(viewItem.ionPageElement);
2208
- this.scheduleViewUnmount(viewItem);
2148
+ viewItem.mount = false;
2149
+ const viewToRemove = viewItem;
2150
+ setTimeout(() => {
2151
+ // Skip if a follow-up transition re-entered the view (mount flipped back to true).
2152
+ if (viewToRemove.mount) {
2153
+ return;
2154
+ }
2155
+ this.context.unMountViewItem(viewToRemove);
2156
+ this.forceUpdate();
2157
+ }, VIEW_UNMOUNT_DELAY_MS);
2209
2158
  }
2210
2159
  }
2211
2160
  }
@@ -3814,7 +3763,6 @@ const IonRouter = ({ children, registerHistoryListener }) => {
3814
3763
  findViewItemByPathname: viewStack.current.findViewItemByPathname,
3815
3764
  getChildrenToRender: viewStack.current.getChildrenToRender,
3816
3765
  getViewItemsForOutlet: viewStack.current.getViewItemsForOutlet.bind(viewStack.current),
3817
- getLocationHistoryPathnames: () => locationHistory.current.getPathnames(),
3818
3766
  goBack: () => handleNavigateBack(),
3819
3767
  createViewItem: viewStack.current.createViewItem,
3820
3768
  findViewItemByRouteInfo: viewStack.current.findViewItemByRouteInfo,
@@ -3827,6 +3775,18 @@ const IonRouter = ({ children, registerHistoryListener }) => {
3827
3775
  };
3828
3776
  IonRouter.displayName = 'IonRouter';
3829
3777
 
3778
+ /**
3779
+ * Defaults React Router 7's `useTransitions` to `false`, since a transition can defer the
3780
+ * commit past Ionic's page-visible signal. React Router 6 ignores the prop.
3781
+ *
3782
+ * Uses `??` instead of spreading a default first, because an explicit
3783
+ * `useTransitions={undefined}` would override it and React Router reads that as on.
3784
+ */
3785
+ const withTransitionDefaults = (props) => {
3786
+ const { useTransitions } = props;
3787
+ return { ...props, useTransitions: useTransitions ?? false };
3788
+ };
3789
+
3830
3790
  /**
3831
3791
  * `IonReactRouter` facilitates the integration of Ionic's specific
3832
3792
  * navigation and UI management with the standard React Router mechanisms,
@@ -3871,7 +3831,7 @@ const RouterContent$2 = ({ children }) => {
3871
3831
  return React.createElement(IonRouter, { registerHistoryListener: registerHistoryListener }, children);
3872
3832
  };
3873
3833
  const IonReactRouter = ({ children, ...browserRouterProps }) => {
3874
- return (React.createElement(BrowserRouter, { ...browserRouterProps },
3834
+ return (React.createElement(BrowserRouter, { ...withTransitionDefaults(browserRouterProps) },
3875
3835
  React.createElement(RouterContent$2, null, children)));
3876
3836
  };
3877
3837
 
@@ -3881,8 +3841,8 @@ const IonReactRouter = ({ children, ...browserRouterProps }) => {
3881
3841
  * isn't available or desirable.
3882
3842
  */
3883
3843
  const RouterContent$1 = ({ children }) => {
3884
- const location = useLocation$1();
3885
- const navigationType = useNavigationType$1();
3844
+ const location = useLocation();
3845
+ const navigationType = useNavigationType();
3886
3846
  const historyListenHandler = useRef();
3887
3847
  const registerHistoryListener = useCallback((cb) => {
3888
3848
  historyListenHandler.current = cb;
@@ -3910,7 +3870,7 @@ const RouterContent$1 = ({ children }) => {
3910
3870
  return React.createElement(IonRouter, { registerHistoryListener: registerHistoryListener }, children);
3911
3871
  };
3912
3872
  const IonReactMemoryRouter = ({ children, ...routerProps }) => {
3913
- return (React.createElement(MemoryRouter, { ...routerProps },
3873
+ return (React.createElement(MemoryRouter, { ...withTransitionDefaults(routerProps) },
3914
3874
  React.createElement(RouterContent$1, null, children)));
3915
3875
  };
3916
3876
 
@@ -3948,7 +3908,7 @@ const RouterContent = ({ children }) => {
3948
3908
  return React.createElement(IonRouter, { registerHistoryListener: registerHistoryListener }, children);
3949
3909
  };
3950
3910
  const IonReactHashRouter = ({ children, ...routerProps }) => {
3951
- return (React.createElement(HashRouter, { ...routerProps },
3911
+ return (React.createElement(HashRouter, { ...withTransitionDefaults(routerProps) },
3952
3912
  React.createElement(RouterContent, null, children)));
3953
3913
  };
3954
3914