import { computed, inject, onMounted, provide, Ref, ref } from 'vue'; import { useRoute } from 'vue-router'; export type Tab = { id: string; title: string; }; export function useTabsProvider({ activeTabProp, disableHashMode, updateActiveTabProp, }: { activeTabProp: Ref; updateActiveTabProp: (tab: string) => void; disableHashMode: Ref; }) { const route = useRoute(); const tabs = ref([]); const activeTab = ref(''); provide('tabs', tabs); provide( 'disable-hash-mode', computed(() => disableHashMode.value), ); provide( 'active-tab', computed({ get: () => activeTab.value, set: (value) => { activeTab.value = value; updateActiveTabProp(value); }, }), ); onMounted(() => { if (activeTabProp.value) { activeTab.value = activeTabProp.value; return; } const hashTab = route.hash.replace(/^#/, ''); if (hashTab) { activeTab.value = hashTab; return; } activeTab.value = tabs.value[0].id; }); } export function useTabsClient() { const tabs = inject>('tabs'); const disableHashMode = inject>('disable-hash-mode'); const activeTab = inject>('active-tab'); if (activeTab === undefined || tabs === undefined || disableHashMode === undefined) { throw new Error('Please use this "useTabsClient" composition inside a component running "useTabsProvider".'); } return { activeTab, tabs, disableHashMode }; }