@@ -85,6 +85,15 @@ export class TabButton implements ComponentInterface, AnchorInterface {
85
85
*/
86
86
@Event ( ) ionTabButtonClick ! : EventEmitter < TabButtonClickEventDetail > ;
87
87
88
+ @Listen ( 'ionTabBarLoaded' , { target : 'window' } )
89
+ onTabBarLoaded ( ev : Event ) {
90
+ // Only respond to events from our direct parent tab-bar
91
+ const parent = this . el . parentElement ;
92
+ if ( parent && parent . tagName === 'ION-TAB-BAR' && ev . target === parent ) {
93
+ this . syncWithTabBar ( ) ;
94
+ }
95
+ }
96
+
88
97
@Listen ( 'ionTabBarChanged' , { target : 'window' } )
89
98
onTabBarChanged ( ev : CustomEvent < TabBarChangedEventDetail > ) {
90
99
const dispatchedFrom = ev . target as HTMLElement ;
@@ -103,12 +112,16 @@ export class TabButton implements ComponentInterface, AnchorInterface {
103
112
if ( this . layout === undefined ) {
104
113
this . layout = config . get ( 'tabButtonLayout' , 'icon-top' ) ;
105
114
}
115
+ }
106
116
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
117
+ private syncWithTabBar ( ) {
109
118
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 ;
119
+ if ( tabBar && tabBar . tagName === 'ION-TAB-BAR' ) {
120
+ // Check both the property and attribute to handle all initialization scenarios
121
+ const selectedTab = tabBar . selectedTab ?? tabBar . getAttribute ( 'selected-tab' ) ;
122
+ if ( selectedTab !== null && selectedTab !== undefined ) {
123
+ this . selected = this . tab === selectedTab ;
124
+ }
112
125
}
113
126
}
114
127
0 commit comments