Skip to content

Commit d5d0774

Browse files
committed
fix(tabs): respect stencil lifecycle order for tab selection
1 parent 3b80473 commit d5d0774

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

core/src/components/tab-bar/tab-bar.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ export class TabBar implements ComponentInterface {
6565
*/
6666
@Event() ionTabBarLoaded!: EventEmitter<void>;
6767

68-
componentWillLoad() {
68+
componentDidLoad() {
69+
this.ionTabBarLoaded.emit();
70+
// Emit the initial selected tab after the component is fully loaded
71+
// This ensures all child components (ion-tab-button) are ready
6972
this.selectedTabChanged();
7073
}
7174

@@ -90,10 +93,6 @@ export class TabBar implements ComponentInterface {
9093
}
9194
}
9295

93-
componentDidLoad() {
94-
this.ionTabBarLoaded.emit();
95-
}
96-
9796
render() {
9897
const { color, translucent, keyboardVisible } = this;
9998
const mode = getIonMode(this);

core/src/components/tabs/tabs.tsx

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,18 @@ export class Tabs implements NavOutlet {
6565
this.ionNavWillLoad.emit();
6666
}
6767

68-
componentWillRender() {
68+
componentDidLoad() {
69+
this.updateTabBar();
70+
}
71+
72+
componentDidUpdate() {
73+
this.updateTabBar();
74+
}
75+
76+
private updateTabBar() {
6977
const tabBar = this.el.querySelector('ion-tab-bar');
7078
if (tabBar) {
71-
let tab = this.selectedTab ? this.selectedTab.tab : undefined;
72-
73-
// Fallback: if no selectedTab is set but we're using router mode,
74-
// determine the active tab from the current URL. This works around
75-
// timing issues in React Router integration where setRouteId may not
76-
// be called in time for the initial render.
77-
// TODO(FW-6724): Remove this with React Router upgrade
78-
if (!tab && this.useRouter && typeof window !== 'undefined') {
79-
const currentPath = window.location.pathname;
80-
const tabButtons = this.el.querySelectorAll('ion-tab-button');
81-
82-
// Look for a tab button that matches the current path pattern
83-
for (const tabButton of tabButtons) {
84-
const tabId = tabButton.getAttribute('tab');
85-
if (tabId && currentPath.includes(tabId)) {
86-
tab = tabId;
87-
break;
88-
}
89-
}
90-
}
91-
79+
const tab = this.selectedTab ? this.selectedTab.tab : undefined;
9280
tabBar.selectedTab = tab;
9381
}
9482
}
@@ -162,6 +150,7 @@ export class Tabs implements NavOutlet {
162150
this.selectedTab = selectedTab;
163151
this.ionTabsWillChange.emit({ tab: selectedTab.tab });
164152
selectedTab.active = true;
153+
this.updateTabBar();
165154
return Promise.resolve();
166155
}
167156

0 commit comments

Comments
 (0)