@@ -85,6 +85,12 @@ export class TabButton implements ComponentInterface, AnchorInterface {
85
85
*/
86
86
@Event ( ) ionTabButtonClick ! : EventEmitter < TabButtonClickEventDetail > ;
87
87
88
+ @Listen ( 'ionTabBarLoaded' , { target : 'parent' } )
89
+ onTabBarLoaded ( ) {
90
+ // When the parent tab-bar loads, sync our selection state
91
+ this . syncWithTabBar ( ) ;
92
+ }
93
+
88
94
@Listen ( 'ionTabBarChanged' , { target : 'window' } )
89
95
onTabBarChanged ( ev : CustomEvent < TabBarChangedEventDetail > ) {
90
96
const dispatchedFrom = ev . target as HTMLElement ;
@@ -103,12 +109,16 @@ export class TabButton implements ComponentInterface, AnchorInterface {
103
109
if ( this . layout === undefined ) {
104
110
this . layout = config . get ( 'tabButtonLayout' , 'icon-top' ) ;
105
111
}
112
+ }
106
113
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 ( ) {
109
115
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
+ }
112
122
}
113
123
}
114
124
0 commit comments