diff --git a/src/lib/components/Tabs/tabsContext.ts b/src/lib/components/Tabs/tabsContext.ts new file mode 100644 index 0000000..f76abd0 --- /dev/null +++ b/src/lib/components/Tabs/tabsContext.ts @@ -0,0 +1,44 @@ +import { writable, get, type Writable } from 'svelte/store'; +import { setContext, getContext } from 'svelte'; + +interface Tab { + value: string; +} + +export interface TabsContext { + activeTab: Writable; + tabs: Writable; + registerTab: (tab: Tab) => void; + selectTab: (value: string) => void; +} + +const createTabs = (): TabsContext => { + const activeTab = writable(''); + const tabs = writable([]); + + const registerTab = (tab: Tab) => { + tabs.update((currTabs) => [...currTabs, tab]); + if (get(activeTab) === null) activeTab.set(tab.value); + }; + + const selectTab = (value: string) => { + activeTab.set(value); + }; + + return { + activeTab, + tabs, + registerTab, + selectTab + }; +}; + +export const setTabsContext = (): TabsContext => { + const tabs = createTabs(); + setContext('tabs', tabs); + return tabs; +}; + +export const getTabsContext = (): TabsContext => { + return getContext('tabs'); +};