Skip to content

Commit

Permalink
feat(tab): tab context
Browse files Browse the repository at this point in the history
  • Loading branch information
macgeargear committed Sep 13, 2024
1 parent 59e2a08 commit 2015db1
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/lib/components/Tabs/tabsContext.ts
Original file line number Diff line number Diff line change
@@ -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<string>;
tabs: Writable<Tab[]>;
registerTab: (tab: Tab) => void;
selectTab: (value: string) => void;
}

const createTabs = (): TabsContext => {
const activeTab = writable<string>('');
const tabs = writable<Tab[]>([]);

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<TabsContext>('tabs', tabs);
return tabs;
};

export const getTabsContext = (): TabsContext => {
return getContext<TabsContext>('tabs');
};

0 comments on commit 2015db1

Please sign in to comment.