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
13 changes: 12 additions & 1 deletion packages/angular/src/common/directives/navigation/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,19 @@ export abstract class IonTabs implements AfterViewInit, AfterContentInit, AfterC

/**
* @internal
*
* `outlet` is the `#outlet` template reference of the inner
* `ion-router-outlet`. When `ion-tabs` renders after the router has
* already activated the tab route, the outlet activates the first tab
* while `IonTabs` is still in its first change detection, before the
* `outlet` view query has resolved. Assigning it here makes sure
* `outlet` is set when `ionTabsWillChange` fires.
*/
onStackWillChange({ enteringView, tabSwitch }: StackWillChangeEvent): void {
onStackWillChange({ enteringView, tabSwitch }: StackWillChangeEvent, outlet?: any): void {
if (outlet) {
this.outlet = outlet;
}

const stackId = enteringView.stackId;
if (tabSwitch && stackId !== undefined) {
this.ionTabsWillChange.emit({ tab: stackId });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { IonRouterOutlet } from './ion-router-outlet';
*ngIf="tabs.length === 0"
#outlet
tabs="true"
(stackWillChange)="onStackWillChange($event)"
(stackWillChange)="onStackWillChange($event, outlet)"
(stackDidChange)="onStackDidChange($event)"
></ion-router-outlet>
<ng-content *ngIf="tabs.length > 0" select="ion-tab"></ng-content>
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/standalone/navigation/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { IonRouterOutlet } from './router-outlet';
*ngIf="tabs.length === 0"
#outlet
tabs="true"
(stackWillChange)="onStackWillChange($event)"
(stackWillChange)="onStackWillChange($event, outlet)"
(stackDidChange)="onStackDidChange($event)"
></ion-router-outlet>
<ng-content *ngIf="tabs.length > 0" select="ion-tab"></ng-content>
Expand Down
10 changes: 10 additions & 0 deletions packages/angular/test/base/e2e/src/standalone/tabs.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ test.describe('Tabs', () => {
});
});

// Issue: https://github.com/ionic-team/ionic-framework/issues/31674
test.describe('Rendered after the tab route activates', () => {
test('should have the outlet defined on the first ionTabsWillChange', async ({ page }) => {
await page.goto('/standalone/tabs-outlet-ready');

await expect(page.locator('app-tab-one')).toBeVisible();
await expect(page.locator('#outlet-defined span')).toHaveText('true');
});
});

test.describe('Without IonRouterOutlet', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/standalone/tabs-basic');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ export const routes: Routes = [
{ path: 'tab-three', loadComponent: () => import('../tabs/tab3.component').then(c => c.TabThreeComponent) }
]
},
{ path: 'tabs-outlet-ready', redirectTo: '/standalone/tabs-outlet-ready/tab-one', pathMatch: 'full' },
{
path: 'tabs-outlet-ready',
loadComponent: () => import('../tabs-outlet-ready/tabs-outlet-ready.component').then(c => c.TabsOutletReadyComponent),
children: [
{ path: 'tab-one', loadComponent: () => import('../tabs/tab1.component').then(c => c.TabOneComponent) }
]
},
{ path: 'tabs-basic', loadComponent: () => import('../tabs-basic/tabs-basic.component').then(c => c.TabsBasicComponent) },
{ path: 'tabs-search-params', redirectTo: '/standalone/tabs-search-params/tab1?foo=bar', pathMatch: 'full' },
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { AfterViewInit, ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { IonLabel, IonTabBar, IonTabButton, IonTabs } from '@ionic/angular';

/**
* Renders `ion-tabs` behind an `@if` that becomes true after the page has
* rendered, so the router has already activated the tab route by the time
* `ion-tabs` is created. The inner outlet then activates the tab while
* `IonTabs` is still in its first change detection, and the first
* `ionTabsWillChange` fires before its `outlet` view query has resolved.
*
* Issue: https://github.com/ionic-team/ionic-framework/issues/31674
*/
@Component({
selector: 'app-tabs-outlet-ready',
// Angular 22 defaults to OnPush, and a tick stops at a clean OnPush view.
changeDetection: ChangeDetectionStrategy.Default,
template: `
@if (showTabs()) {
<ion-tabs #tabs (ionTabsWillChange)="onTabsWillChange(tabs)">
<ion-tab-bar slot="bottom">
<ion-tab-button tab="tab-one">
<ion-label>Tab One</ion-label>
</ion-tab-button>
</ion-tab-bar>
</ion-tabs>
}
<div id="test">
<p id="outlet-defined">
Outlet defined on first ionTabsWillChange: <span>{{ outletDefined() }}</span>
</p>
</div>
`,
standalone: true,
imports: [IonLabel, IonTabBar, IonTabButton, IonTabs],
})
export class TabsOutletReadyComponent implements AfterViewInit {
showTabs = signal(false);

outletDefined = signal<boolean | undefined>(undefined);

ngAfterViewInit() {
setTimeout(() => this.showTabs.set(true));
}

onTabsWillChange(tabs: IonTabs) {
// Only record the first event.
if (this.outletDefined() !== undefined) {
return;
}

this.outletDefined.set(tabs.outlet?.nativeEl !== undefined);
}
}