@@ -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
0 commit comments