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