Skip to content

Commit c3b9598

Browse files
committed
fix(react-router): allow defaultHref navigation on direct deep-link to tab child pages
1 parent 177fb5f commit c3b9598

3 files changed

Lines changed: 20 additions & 9 deletions

File tree

packages/react-router/src/ReactRouter/IonRouter.tsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,9 @@ export const IonRouter = ({ children, registerHistoryListener }: PropsWithChildr
546546
* @param routeAnimation A custom animation builder to override the
547547
* default "back" animation.
548548
*/
549-
const handleNavigateBack = (defaultHref: string | RouteInfo = '/', routeAnimation?: AnimationBuilder) => {
549+
const handleNavigateBack = (defaultHref?: string | RouteInfo, routeAnimation?: AnimationBuilder) => {
550550
const config = getConfig();
551-
defaultHref = defaultHref ? defaultHref : config && config.get('backButtonDefaultHref' as any);
551+
defaultHref = defaultHref ?? (config && config.get('backButtonDefaultHref' as any));
552552
const routeInfo = locationHistory.current.current();
553553
// It's a linear navigation.
554554
if (routeInfo && routeInfo.pushedByRoute) {
@@ -590,17 +590,18 @@ export const IonRouter = ({ children, registerHistoryListener }: PropsWithChildr
590590
* `pushedByRoute` exists, but no corresponding previous entry in
591591
* the history stack.
592592
*/
593-
} else {
593+
} else if (defaultHref) {
594594
handleNavigate(defaultHref as string, 'pop', 'back', routeAnimation);
595595
}
596596
/**
597597
* No `pushedByRoute` (e.g., initial page load or tab root).
598-
* Tabs with no back history should not navigate.
598+
* Navigate to defaultHref so the back button works on direct
599+
* deep-link loads (e.g., loading /tab1/child directly).
600+
* Only navigate when defaultHref is explicitly set. The core
601+
* back-button component hides itself when no defaultHref is
602+
* provided, so a click here means the user set one intentionally.
599603
*/
600-
} else {
601-
if (routeInfo && routeInfo.tab) {
602-
return;
603-
}
604+
} else if (defaultHref) {
604605
handleNavigate(defaultHref as string, 'pop', 'back', routeAnimation);
605606
}
606607
};

packages/react-router/test/base/src/pages/tabs/Tabs.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const Tab1Child1 = () => {
6464
<IonHeader>
6565
<IonToolbar>
6666
<IonButtons slot="start">
67-
<IonBackButton />
67+
<IonBackButton defaultHref="/tabs/tab1" />
6868
</IonButtons>
6969
<IonTitle>Tab1</IonTitle>
7070
</IonToolbar>

packages/react-router/test/base/tests/e2e/playwright/tabs.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ test.describe('Tabs', () => {
6464
await expect(page.locator('ion-tab-button.tab-selected')).toContainText('Tab1');
6565
});
6666

67+
// Verifies that defaultHref works on direct deep-link load of a tab child page
68+
test('back button defaultHref should work on direct deep-link load of tab child page', async ({ page }) => {
69+
await page.goto(withTestingMode('/tabs/tab1/child'));
70+
await ionPageVisible(page, 'tab1child1');
71+
72+
await ionBackClick(page, 'tab1child1');
73+
await ionPageVisible(page, 'tab1');
74+
await expect(page).toHaveURL(/\/tabs\/tab1$/);
75+
});
76+
6777
// Verifies fix for https://github.com/ionic-team/ionic-framework/issues/23087
6878
test('should return to correct view and url when going back from child page after switching tabs', async ({ page }) => {
6979
await page.goto(withTestingMode('/tabs/tab1'));

0 commit comments

Comments
 (0)