Skip to content

Commit f4e3feb

Browse files
committed
Adding fallback for edge case
1 parent fb63784 commit f4e3feb

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ export class TabButton implements ComponentInterface, AnchorInterface {
8585
*/
8686
@Event() ionTabButtonClick!: EventEmitter<TabButtonClickEventDetail>;
8787

88+
@Listen('ionTabBarLoaded', { target: 'parent' })
89+
onTabBarLoaded() {
90+
// When the parent tab-bar loads, sync our selection state
91+
this.syncWithTabBar();
92+
}
93+
8894
@Listen('ionTabBarChanged', { target: 'window' })
8995
onTabBarChanged(ev: CustomEvent<TabBarChangedEventDetail>) {
9096
const dispatchedFrom = ev.target as HTMLElement;
@@ -103,12 +109,16 @@ export class TabButton implements ComponentInterface, AnchorInterface {
103109
if (this.layout === undefined) {
104110
this.layout = config.get('tabButtonLayout', 'icon-top');
105111
}
112+
}
106113

107-
// Check if this tab button should be initially selected based on parent tab-bar's selectedTab prop
108-
// This handles the case where selected-tab is set via HTML attribute before events fire
114+
private syncWithTabBar() {
109115
const tabBar = this.el.parentElement as HTMLIonTabBarElement | null;
110-
if (tabBar && tabBar.tagName === 'ION-TAB-BAR' && tabBar.selectedTab !== undefined) {
111-
this.selected = this.tab === tabBar.selectedTab;
116+
if (tabBar && tabBar.tagName === 'ION-TAB-BAR') {
117+
// Check both the property and attribute to handle all initialization scenarios
118+
const selectedTab = tabBar.selectedTab ?? tabBar.getAttribute('selected-tab');
119+
if (selectedTab !== null && selectedTab !== undefined) {
120+
this.selected = this.tab === selectedTab;
121+
}
112122
}
113123
}
114124

core/src/components/tabs/tabs.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,23 @@ export class Tabs implements NavOutlet {
7575

7676
private updateTabBar() {
7777
const tabBar = this.el.querySelector('ion-tab-bar');
78-
if (tabBar) {
79-
const tab = this.selectedTab ? this.selectedTab.tab : undefined;
80-
tabBar.selectedTab = tab;
78+
if (!tabBar) {
79+
return;
80+
}
81+
82+
const tab = this.selectedTab ? this.selectedTab.tab : undefined;
83+
84+
// If tabs has no selected tab but tab-bar already has a selected-tab set,
85+
// don't overwrite it. This handles cases where tab-bar is used without ion-tab elements.
86+
if (tab === undefined) {
87+
return;
8188
}
89+
90+
if (tabBar.selectedTab === tab) {
91+
return;
92+
}
93+
94+
tabBar.selectedTab = tab;
8295
}
8396

8497
/**

0 commit comments

Comments
 (0)