@ionic/react-router 9.0.15-dev.11796247574.1ecbe59d → 9.0.15-dev.11796248397.1d85e35e

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,7 +1,8 @@
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, MemoryRouter, HashRouter } from 'react-router-dom';
2
+ import { Route, matchPath as matchPath$1, Routes, Navigate, UNSAFE_RouteContext, matchRoutes, useLocation, useNavigate, BrowserRouter, useNavigationType, 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';
5
6
 
6
7
  const IonRouteInner = ({ path, index, caseSensitive, element }) => {
7
8
  return React.createElement(Route, { path: path, index: index, caseSensitive: caseSensitive, element: element });
@@ -645,16 +646,14 @@ const getFallbackParamsFromViewItems = (allViewItems, currentOutletId, currentPa
645
646
  return params;
646
647
  };
647
648
  /**
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>`.
649
+ * Builds the matches array for RouteContext.
651
650
  */
652
- const buildContextMatches = (parentMatches, combinedParams, absolutePathnameBase, viewItem, routeElement, componentElement) => {
651
+ const buildContextMatches = (parentMatches, combinedParams, routeMatch, routeInfoPathname, absolutePathnameBase, viewItem, routeElement, componentElement) => {
653
652
  return [
654
653
  ...parentMatches,
655
654
  {
656
655
  params: combinedParams,
657
- pathname: absolutePathnameBase,
656
+ pathname: routeMatch?.pathname || routeInfoPathname,
658
657
  pathnameBase: absolutePathnameBase,
659
658
  route: {
660
659
  id: viewItem.id,
@@ -1062,7 +1061,7 @@ class ReactRouterViewStack extends ViewStacks {
1062
1061
  const combinedParams = { ...accumulatedParentParams, ...(routeMatch?.params ?? {}) };
1063
1062
  const parentPathnameBase = parentMatches.length > 0 ? parentMatches[parentMatches.length - 1].pathnameBase : '/';
1064
1063
  const absolutePathnameBase = computeAbsolutePathnameBase(routeElement, routeMatch, parentPathnameBase, routeInfo.pathname);
1065
- const contextMatches = buildContextMatches(parentMatches, combinedParams, absolutePathnameBase, viewItem, routeElement, componentElement);
1064
+ const contextMatches = buildContextMatches(parentMatches, combinedParams, routeMatch, routeInfo.pathname, absolutePathnameBase, viewItem, routeElement, componentElement);
1066
1065
  const routeContextValue = parentContext
1067
1066
  ? { ...parentContext, matches: contextMatches }
1068
1067
  : { outlet: null, matches: contextMatches, isDataRoute: false };
@@ -1960,8 +1959,15 @@ class StackManager extends React.PureComponent {
1960
1959
  return;
1961
1960
  }
1962
1961
  this.lastTransition = currentTransition;
1963
- const shouldSkipAnimation = this.applySkipAnimationIfNeeded(enteringViewItem, leavingViewItem);
1964
- this.transitionPage(routeInfo, enteringViewItem, leavingViewItem, undefined, false, shouldSkipAnimation);
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);
1965
1971
  if (shouldUnmountLeavingViewItem && leavingViewItem && enteringViewItem !== leavingViewItem) {
1966
1972
  // For replace actions, skip setting mount=false here. handleLeavingViewUnmount
1967
1973
  // sets it only after its container-to-container guard passes, avoiding zombie state.
@@ -1983,7 +1989,8 @@ class StackManager extends React.PureComponent {
1983
1989
  }
1984
1990
  this.handleLeavingViewUnmount(routeInfo, enteringViewItem, leavingViewItem);
1985
1991
  }
1986
- // Clean up orphaned sibling views after replace actions (redirects)
1992
+ this.cleanupPageBehindPagelessView(routeInfo, pageBehindPagelessView);
1993
+ // Clean up orphaned sibling views after replace actions or push-to-container navigations
1987
1994
  this.cleanupOrphanedSiblingViews(routeInfo, enteringViewItem, leavingViewItem);
1988
1995
  // On a fresh push, browser forward history is invalidated. Any views we
1989
1996
  // previously preserved on pop (to support forward navigation) are now
@@ -2013,17 +2020,8 @@ class StackManager extends React.PureComponent {
2013
2020
  this.preservedViewItems.delete(viewItem);
2014
2021
  continue;
2015
2022
  }
2016
- viewItem.mount = false;
2017
2023
  this.preservedViewItems.delete(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);
2024
+ this.scheduleViewUnmount(viewItem);
2027
2025
  }
2028
2026
  }
2029
2027
  /**
@@ -2035,16 +2033,7 @@ class StackManager extends React.PureComponent {
2035
2033
  return;
2036
2034
  }
2037
2035
  if (!leavingViewItem.ionPageElement) {
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);
2036
+ this.scheduleViewUnmount(leavingViewItem);
2048
2037
  return;
2049
2038
  }
2050
2039
  const enteringRoutePath = enteringViewItem.reactElement?.props?.path;
@@ -2060,35 +2049,106 @@ class StackManager extends React.PureComponent {
2060
2049
  if (isEnteringContainerRoute && !isLeavingSpecificRoute) {
2061
2050
  return;
2062
2051
  }
2063
- leavingViewItem.mount = false;
2064
- const viewToUnmount = leavingViewItem;
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;
2065
2060
  setTimeout(() => {
2066
- // Skip if a follow-up transition re-entered the view (mount flipped back to true).
2067
- if (viewToUnmount.mount) {
2061
+ if (viewItem.mount) {
2068
2062
  return;
2069
2063
  }
2070
- this.context.unMountViewItem(viewToUnmount);
2064
+ this.context.unMountViewItem(viewItem);
2071
2065
  this.forceUpdate();
2072
2066
  }, VIEW_UNMOUNT_DELAY_MS);
2073
2067
  }
2074
2068
  /**
2075
- * Cleans up orphaned sibling views after replace actions or push-to-container navigations.
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.
2076
2136
  */
2077
2137
  cleanupOrphanedSiblingViews(routeInfo, enteringViewItem, leavingViewItem) {
2078
2138
  const enteringRoutePath = enteringViewItem.reactElement?.props?.path;
2079
2139
  if (!enteringRoutePath) {
2080
2140
  return;
2081
2141
  }
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) {
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) {
2087
2146
  return;
2088
2147
  }
2148
+ const leavingRoutePath = leavingViewItem?.reactElement?.props?.path;
2089
2149
  // Skip cleanup for tab switches
2090
2150
  const isSameView = enteringViewItem === leavingViewItem;
2091
- const isSameContainerRoute = isContainerRoute(enteringRoutePath) && leavingRoutePath === enteringRoutePath;
2151
+ const isSameContainerRoute = enteringRoutePath.endsWith('/*') && leavingRoutePath === enteringRoutePath;
2092
2152
  const isNavigatingWithinContainer = isPushToContainer &&
2093
2153
  !leavingViewItem &&
2094
2154
  routeInfo.prevRouteLastPathname?.startsWith(enteringRoutePath.replace(/\/\*$/, ''));
@@ -2096,6 +2156,9 @@ class StackManager extends React.PureComponent {
2096
2156
  return;
2097
2157
  }
2098
2158
  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();
2099
2162
  const areSiblingRoutes = (path1, path2) => {
2100
2163
  const path1IsRelative = !path1.startsWith('/');
2101
2164
  const path2IsRelative = !path2.startsWith('/');
@@ -2131,30 +2194,18 @@ class StackManager extends React.PureComponent {
2131
2194
  (leavingViewItem && viewItem.id === leavingViewItem.id) ||
2132
2195
  !viewItem.mount ||
2133
2196
  !viewRoutePath ||
2197
+ viewIdsInHistory.has(viewItem.id) ||
2134
2198
  // Don't clean up container routes when entering a container route
2135
2199
  // (e.g., /tabs/* and /settings/* coexist for tab switching)
2136
2200
  (viewRoutePath.endsWith('/*') && enteringRoutePath.endsWith('/*'));
2137
2201
  if (shouldSkip) {
2138
2202
  continue;
2139
2203
  }
2140
- const isOrphanedSpecificRoute = !viewRoutePath.endsWith('/*');
2141
2204
  // Clean up sibling non-container routes that are no longer reachable.
2142
- let shouldCleanup = false;
2143
- if ((isReplaceAction || isPushToContainer) && isOrphanedSpecificRoute) {
2144
- shouldCleanup = areSiblingRoutes(enteringRoutePath, viewRoutePath);
2145
- }
2146
- if (shouldCleanup) {
2205
+ const isSpecificRoute = !viewRoutePath.endsWith('/*');
2206
+ if (isSpecificRoute && areSiblingRoutes(enteringRoutePath, viewRoutePath)) {
2147
2207
  hideIonPageElement(viewItem.ionPageElement);
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);
2208
+ this.scheduleViewUnmount(viewItem);
2158
2209
  }
2159
2210
  }
2160
2211
  }
@@ -3763,6 +3814,7 @@ const IonRouter = ({ children, registerHistoryListener }) => {
3763
3814
  findViewItemByPathname: viewStack.current.findViewItemByPathname,
3764
3815
  getChildrenToRender: viewStack.current.getChildrenToRender,
3765
3816
  getViewItemsForOutlet: viewStack.current.getViewItemsForOutlet.bind(viewStack.current),
3817
+ getLocationHistoryPathnames: () => locationHistory.current.getPathnames(),
3766
3818
  goBack: () => handleNavigateBack(),
3767
3819
  createViewItem: viewStack.current.createViewItem,
3768
3820
  findViewItemByRouteInfo: viewStack.current.findViewItemByRouteInfo,
@@ -3775,18 +3827,6 @@ const IonRouter = ({ children, registerHistoryListener }) => {
3775
3827
  };
3776
3828
  IonRouter.displayName = 'IonRouter';
3777
3829
 
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
-
3790
3830
  /**
3791
3831
  * `IonReactRouter` facilitates the integration of Ionic's specific
3792
3832
  * navigation and UI management with the standard React Router mechanisms,
@@ -3831,7 +3871,7 @@ const RouterContent$2 = ({ children }) => {
3831
3871
  return React.createElement(IonRouter, { registerHistoryListener: registerHistoryListener }, children);
3832
3872
  };
3833
3873
  const IonReactRouter = ({ children, ...browserRouterProps }) => {
3834
- return (React.createElement(BrowserRouter, { ...withTransitionDefaults(browserRouterProps) },
3874
+ return (React.createElement(BrowserRouter, { ...browserRouterProps },
3835
3875
  React.createElement(RouterContent$2, null, children)));
3836
3876
  };
3837
3877
 
@@ -3841,8 +3881,8 @@ const IonReactRouter = ({ children, ...browserRouterProps }) => {
3841
3881
  * isn't available or desirable.
3842
3882
  */
3843
3883
  const RouterContent$1 = ({ children }) => {
3844
- const location = useLocation();
3845
- const navigationType = useNavigationType();
3884
+ const location = useLocation$1();
3885
+ const navigationType = useNavigationType$1();
3846
3886
  const historyListenHandler = useRef();
3847
3887
  const registerHistoryListener = useCallback((cb) => {
3848
3888
  historyListenHandler.current = cb;
@@ -3870,7 +3910,7 @@ const RouterContent$1 = ({ children }) => {
3870
3910
  return React.createElement(IonRouter, { registerHistoryListener: registerHistoryListener }, children);
3871
3911
  };
3872
3912
  const IonReactMemoryRouter = ({ children, ...routerProps }) => {
3873
- return (React.createElement(MemoryRouter, { ...withTransitionDefaults(routerProps) },
3913
+ return (React.createElement(MemoryRouter, { ...routerProps },
3874
3914
  React.createElement(RouterContent$1, null, children)));
3875
3915
  };
3876
3916
 
@@ -3908,7 +3948,7 @@ const RouterContent = ({ children }) => {
3908
3948
  return React.createElement(IonRouter, { registerHistoryListener: registerHistoryListener }, children);
3909
3949
  };
3910
3950
  const IonReactHashRouter = ({ children, ...routerProps }) => {
3911
- return (React.createElement(HashRouter, { ...withTransitionDefaults(routerProps) },
3951
+ return (React.createElement(HashRouter, { ...routerProps },
3912
3952
  React.createElement(RouterContent, null, children)));
3913
3953
  };
3914
3954