diff --git a/packages/vue/src/components/IonTabBar.ts b/packages/vue/src/components/IonTabBar.ts index b6e4f41de16..25078c74f49 100644 --- a/packages/vue/src/components/IonTabBar.ts +++ b/packages/vue/src/components/IonTabBar.ts @@ -138,10 +138,9 @@ export const IonTabBar = defineComponent({ /** * If the router outlet is not defined, then the tabs are being used - * as a basic tab navigation without the router. In this case, the - * tabs will not emit the `ionTabsDidChange` and `ionTabsWillChange` - * events through the `checkActiveTab` method. Instead, we need to - * handle those events through the tab buttons. + * as a basic tab navigation without the router. History changes do + * not happen in this case, so the selected tab is updated through + * the tab buttons instead of the `checkActiveTab` method. */ if (!hasRouterOutlet) { child.component.props._onClick = ( @@ -164,11 +163,7 @@ export const IonTabBar = defineComponent({ * and updates the active tab accordingly. * * History changes only occur when the router - * outlet is present. Due to this, the - * `ionTabsDidChange` and `ionTabsWillChange` - * events are only emitted when the router - * outlet is present. A different approach must - * be taken for tabs without a router outlet. + * outlet is present. * * @param ionRouter */ @@ -277,10 +272,16 @@ export const IonTabBar = defineComponent({ (child: VNode) => isTabButton(child) && child.props?.tab === activeTab ); const tabBar = this.$refs.ionTabBar as HTMLIonTabBarElement | undefined; - const tabDidChange = activeTab !== prevActiveTab; + /** + * Without a router outlet, `IonTabs` forwards the tab change + * events from core `ion-tabs`, which emits them once the tab + * has switched. + */ + const shouldEmitTabChange = + hasRouterOutlet && activeTab !== prevActiveTab; if (tabBar) { if (activeChild) { - tabDidChange && this.$data._tabsWillChange(activeTab); + shouldEmitTabChange && this.$data._tabsWillChange(activeTab); if (hasRouterOutlet && ionRouter !== null) { ionRouter.handleSetCurrentTab(activeTab); @@ -288,7 +289,7 @@ export const IonTabBar = defineComponent({ tabBar.selectedTab = tabState.activeTab = activeTab; - tabDidChange && this.$data._tabsDidChange(activeTab); + shouldEmitTabChange && this.$data._tabsDidChange(activeTab); } else { /** * When going to a tab that does diff --git a/packages/vue/src/components/IonTabs.ts b/packages/vue/src/components/IonTabs.ts index 783a8453a15..6ef58edb1df 100644 --- a/packages/vue/src/components/IonTabs.ts +++ b/packages/vue/src/components/IonTabs.ts @@ -52,11 +52,6 @@ const isTab = (node: VNode): boolean => { export const IonTabs = /*@__PURE__*/ defineComponent({ name: "IonTabs", emits: [WILL_CHANGE, DID_CHANGE], - data() { - return { - hasRouterOutlet: false, - }; - }, setup(props, { slots, emit }) { const slottedContent: VNode[] | undefined = slots.default && slots.default(); @@ -74,6 +69,8 @@ export const IonTabs = /*@__PURE__*/ defineComponent({ ); } + const hasRouterOutlet = !!routerOutlet; + /** * Tab bar can be used as a standalone component, * so it cannot be modified directly through @@ -83,7 +80,7 @@ export const IonTabs = /*@__PURE__*/ defineComponent({ provide( "tabBarData", shallowRef({ - hasRouterOutlet: !!routerOutlet, + hasRouterOutlet, _tabsWillChange: (tab) => emit(WILL_CHANGE, { tab }), _tabsDidChange: (tab) => emit(DID_CHANGE, { tab }), }) @@ -93,9 +90,28 @@ export const IonTabs = /*@__PURE__*/ defineComponent({ props, slots, emit, + hasRouterOutlet, }; }, mounted() { + /** + * Without a router outlet, core `ion-tabs` switches tabs itself, + * including from `select()`, and emits the tab change events once + * the switch is done. Forward those, since declaring the same + * names in `emits` keeps them from reaching the app's listeners. + * The listeners are added before the element is defined so the + * events for the first tab are not missed. + */ + if (!this.hasRouterOutlet) { + const tabsEl = this.$el as HTMLIonTabsElement; + tabsEl.addEventListener(WILL_CHANGE, (ev) => + this.$emit(WILL_CHANGE, { tab: (ev as CustomEvent).detail.tab }) + ); + tabsEl.addEventListener(DID_CHANGE, (ev) => + this.$emit(DID_CHANGE, { tab: (ev as CustomEvent).detail.tab }) + ); + } + /** * `defineCustomElement` must be called in the `mounted` hook * to ensure that the custom element is defined after the diff --git a/packages/vue/test/base/src/views/TabsBasic.vue b/packages/vue/test/base/src/views/TabsBasic.vue index 9c1af36c895..63f5685048b 100644 --- a/packages/vue/test/base/src/views/TabsBasic.vue +++ b/packages/vue/test/base/src/views/TabsBasic.vue @@ -1,7 +1,14 @@