diff --git a/packages/react-router/src/ReactRouter/StackManager.tsx b/packages/react-router/src/ReactRouter/StackManager.tsx index 8ab592787fa..7d9240a08fd 100644 --- a/packages/react-router/src/ReactRouter/StackManager.tsx +++ b/packages/react-router/src/ReactRouter/StackManager.tsx @@ -631,6 +631,12 @@ export class StackManager extends React.PureComponent { 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) { @@ -868,6 +874,46 @@ export class StackManager extends React.PureComponent { } } + /** + * 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`. diff --git a/packages/react-router/test/base/src/App.tsx b/packages/react-router/test/base/src/App.tsx index 983e79bb6cf..bceb4542426 100644 --- a/packages/react-router/test/base/src/App.tsx +++ b/packages/react-router/test/base/src/App.tsx @@ -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 ( @@ -141,6 +142,7 @@ const App: React.FC = () => { /> } /> } /> + } /> diff --git a/packages/react-router/test/base/src/pages/swipe-preserved-page/SwipePreservedPage.tsx b/packages/react-router/test/base/src/pages/swipe-preserved-page/SwipePreservedPage.tsx new file mode 100644 index 00000000000..a1da97b9af4 --- /dev/null +++ b/packages/react-router/test/base/src/pages/swipe-preserved-page/SwipePreservedPage.tsx @@ -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 ( + + + + Step 1 + + + + + Go Step 1 -> Step 2 -> Home -> Settings -> Step 1. Only Step 1 should be visible at the end; Step + 2 must be hidden. + + router.push('/swipe-preserved-page/onboarding/step2', 'forward', 'push')} + > + Next + + + + ); +}; + +const Step2: React.FC = () => { + const router = useIonRouter(); + return ( + + + + Step 2 + + + + router.push('/swipe-preserved-page/home', 'forward', 'push')}> + Finish + + + + ); +}; + +const HomePage: React.FC = () => { + const router = useIonRouter(); + return ( + + + + Home + + + + router.push('/swipe-preserved-page/settings', 'forward', 'push')}> + Settings + + + + ); +}; + +const SettingsPage: React.FC = () => { + const router = useIonRouter(); + return ( + + + + Settings + + + + router.push('/swipe-preserved-page/onboarding/step1', 'forward', 'push')} + > + Back to Step 1 + + + + ); +}; + +const SwipePreservedPage: React.FC = () => { + return ( + + + } /> + } /> + + } + /> + } /> + } /> + + ); +}; + +export default SwipePreservedPage; diff --git a/packages/react-router/test/base/tests/e2e/playwright/swipe-preserved-page.spec.ts b/packages/react-router/test/base/tests/e2e/playwright/swipe-preserved-page.spec.ts new file mode 100644 index 00000000000..b11f2b38ee3 --- /dev/null +++ b/packages/react-router/test/base/tests/e2e/playwright/swipe-preserved-page.spec.ts @@ -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'); + }); +});