Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/react-router/src/ReactRouter/StackManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,12 @@ export class StackManager extends React.PureComponent<StackManagerProps> {

const shouldSkipAnimation = this.applySkipAnimationIfNeeded(enteringViewItem, leavingViewItem);

// When the transition has no leaving page to hide (the leaving view
// lookup missed), hide any other visible view in this outlet so a
// preserved or stale page does not stay on screen. See
// hideOtherVisibleViews.
this.hideOtherVisibleViews(enteringViewItem, leavingViewItem);

this.transitionPage(routeInfo, enteringViewItem, leavingViewItem, undefined, false, shouldSkipAnimation);

if (shouldUnmountLeavingViewItem && leavingViewItem && enteringViewItem !== leavingViewItem) {
Expand Down Expand Up @@ -868,6 +874,46 @@ export class StackManager extends React.PureComponent<StackManagerProps> {
}
}

/**
* Hides every other visible view in this outlet when the transition has no
* leaving page to hide.
*
* This covers the cases where the leaving view lookup misses: re-entering
* an outlet whose preserved page is still visible after the swipe-back
* preservation in handleOutOfScopeOutlet (#31708), and a redirect that
* lands before the intermediate route's page registers (#31709 path B).
* In both cases the entering view is ready and the transition hides
* nothing, so any other visible view would stay on screen.
*
* Mirrors the hide-all-others fallback in handleWaitingForIonPage (the
* Navigate branch and the ionPageWaitTimeout path), except the leaving
* view is excluded here: transitionPage hides it once the entering view
* is visible, and hiding it early would break the transition animation.
*/
private hideOtherVisibleViews(enteringViewItem: ViewItem, leavingViewItem: ViewItem | undefined): void {
if (leavingViewItem?.ionPageElement) {
return;
}

const enteringRoutePath = enteringViewItem.reactElement?.props?.path as string | undefined;
const allViewsInOutlet = this.context.getViewItemsForOutlet(this.id);
for (const viewItem of allViewsInOutlet) {
if (viewItem === enteringViewItem || viewItem === leavingViewItem) {
continue;
}
if (!viewItem.mount || !viewItem.ionPageElement || !isViewVisible(viewItem.ionPageElement)) {
continue;
}
// Don't hide container routes when entering a container route
// (e.g., /tabs/* and /settings/* coexist for tab switching).
const viewRoutePath = viewItem.reactElement?.props?.path as string | undefined;
if (viewRoutePath?.endsWith('/*') && enteringRoutePath?.endsWith('/*')) {
continue;
}
hideIonPageElement(viewItem.ionPageElement);
}
}

/**
* Determines whether to skip the transition animation and, if so, immediately
* hides the leaving view with inline `visibility:hidden`.
Expand Down
2 changes: 2 additions & 0 deletions packages/react-router/test/base/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import DisabledButton from './pages/disabled-button/DisabledButton';
import SplatSibling from './pages/splat-sibling/SplatSibling';
import StaleRegister from './pages/stale-register/StaleRegister';
import { EmptyPathSibling, IndexSibling } from './pages/index-sibling/IndexSibling';
import SwipePreservedPage from './pages/swipe-preserved-page/SwipePreservedPage';

const App: React.FC = () => {
return (
Expand Down Expand Up @@ -141,6 +142,7 @@ const App: React.FC = () => {
/>
<Route path="/index-sibling/*" element={<IndexSibling />} />
<Route path="/empty-path-sibling/*" element={<EmptyPathSibling />} />
<Route path="/swipe-preserved-page/*" element={<SwipePreservedPage />} />
</IonRouterOutlet>
</IonReactRouter>
</IonApp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import {
IonButton,
IonContent,
IonHeader,
IonPage,
IonRouterOutlet,
IonTitle,
IonToolbar,
useIonRouter,
} from '@ionic/react';
import React from 'react';
import { Route } from 'react-router-dom';

import TestDescription from '../../components/TestDescription';

/**
* Regression page for ionic-team/ionic-framework#31708: in iOS mode, an
* `ionPage` child outlet can end up with two visible pages. The page that
* was active when the user left the outlet (Step 2) is never hidden, so
* when the user later returns to a different page in the same outlet
* (Step 1), the old page stays on screen and covers the new one.
*
* Each page has its own background color, matching the issue reproduction:
* Step 1 green, Step 2 red, Home blue, Settings purple. All navigation
* uses `router.push(path, 'forward')`.
*/
const Step1: React.FC = () => {
const router = useIonRouter();
return (
<IonPage data-pageid="swipe-step-1" style={{ background: 'green' }}>
<IonHeader>
<IonToolbar>
<IonTitle>Step 1</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<TestDescription>
Go Step 1 -&gt; Step 2 -&gt; Home -&gt; Settings -&gt; Step 1. Only Step 1 should be visible at the end; Step
2 must be hidden.
</TestDescription>
<IonButton
id="go-to-step2"
onClick={() => router.push('/swipe-preserved-page/onboarding/step2', 'forward', 'push')}
>
Next
</IonButton>
</IonContent>
</IonPage>
);
};

const Step2: React.FC = () => {
const router = useIonRouter();
return (
<IonPage data-pageid="swipe-step-2" style={{ background: 'red' }}>
<IonHeader>
<IonToolbar>
<IonTitle>Step 2</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton id="go-to-home" onClick={() => router.push('/swipe-preserved-page/home', 'forward', 'push')}>
Finish
</IonButton>
</IonContent>
</IonPage>
);
};

const HomePage: React.FC = () => {
const router = useIonRouter();
return (
<IonPage data-pageid="swipe-home" style={{ background: 'blue' }}>
<IonHeader>
<IonToolbar>
<IonTitle>Home</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton id="go-to-settings" onClick={() => router.push('/swipe-preserved-page/settings', 'forward', 'push')}>
Settings
</IonButton>
</IonContent>
</IonPage>
);
};

const SettingsPage: React.FC = () => {
const router = useIonRouter();
return (
<IonPage data-pageid="swipe-settings" style={{ background: 'purple' }}>
<IonHeader>
<IonToolbar>
<IonTitle>Settings</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonButton
id="back-to-step1"
onClick={() => router.push('/swipe-preserved-page/onboarding/step1', 'forward', 'push')}
>
Back to Step 1
</IonButton>
</IonContent>
</IonPage>
);
};

const SwipePreservedPage: React.FC = () => {
return (
<IonRouterOutlet>
<Route
path="onboarding/*"
element={
<IonRouterOutlet ionPage>
<Route path="step1" element={<Step1 />} />
<Route path="step2" element={<Step2 />} />
</IonRouterOutlet>
}
/>
<Route path="home" element={<HomePage />} />
<Route path="settings" element={<SettingsPage />} />
</IonRouterOutlet>
);
};

export default SwipePreservedPage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { test, expect } from '@playwright/test';
import { ionPageHidden, ionPageVisible } from './utils/test-utils';

const IOS_MODE = 'ionic:mode=ios';

/**
* Regression test for ionic-team/ionic-framework#31708: in iOS mode, an
* `ionPage` child outlet can end up with two visible pages. The page that
* was active when the user left the outlet (Step 2) is never hidden, so
* when the user later returns to a different page in the same outlet
* (Step 1), the old page stays on screen and covers the new one.
*
* The extra Settings step matters: going straight from Home back to Step 1
* leaves `prevRouteLastPathname` at `/onboarding/step2`, so Step 2 is found
* as the leaving view and hidden. Any two navigations outside the outlet
* before returning trigger the bug.
*/
test.describe('swipe-preserved page stays visible', () => {
test('only the current page is visible when re-entering the outlet at a different page', async ({ page }) => {
await page.goto(`/swipe-preserved-page/onboarding/step1?${IOS_MODE}`);
await ionPageVisible(page, 'swipe-step-1');

await page.locator('#go-to-step2').click();
await ionPageVisible(page, 'swipe-step-2');

await page.locator('#go-to-home').click();
await ionPageVisible(page, 'swipe-home');

await page.locator('#go-to-settings').click();
await ionPageVisible(page, 'swipe-settings');

await page.locator('#back-to-step1').click();
await ionPageVisible(page, 'swipe-step-1');

await expect(page).toHaveURL(/\/swipe-preserved-page\/onboarding\/step1$/);
await ionPageHidden(page, 'swipe-step-2');
});
});