import type { Dispatch, SetStateAction } from "react"; import { createContext, useContext, useEffect, useState } from "react"; import { Button } from "@syntax/Button"; import clsx from "clsx"; type TabType = { label: string; value: string; }; type TabsContextType = { selectedTab: string; selectTab: Dispatch>; tabs: TabType[]; addTab: (tab: TabType) => void; }; const TabsContext = createContext({ selectedTab: "", selectTab: () => {}, tabs: [], addTab: () => {}, }); export function Tabs({ defaultSelectedTab = "", children, }: { defaultSelectedTab?: string; children: React.ReactNode; }) { const [selectedTab, selectTab] = useState(defaultSelectedTab); const [tabs, setTabs] = useState([]); const addTab = (tab: TabType) => setTabs((prevTabs) => { // Append to the end of the array and make sure it's unique if (prevTabs.some((t) => t.value === tab.value)) { return prevTabs; } return [...prevTabs, tab]; }); return (
    {tabs.map((tab) => (
  • selectTab(tab.value)} />
  • ))}
{children}
); } export function TabItem({ tab, isSelected, select }: { tab: TabType; isSelected: boolean; select: () => void }) { return ( ); } export function TabContent({ label, value, children }: { label: string; value: string; children: React.ReactNode }) { const { addTab, selectedTab } = useContext(TabsContext); useEffect(() => { addTab({ label, value }); }, []); return (
*:first-of-type]:!mt-0", "[&>*:last-of-type]:!mb-0", selectedTab !== value && "hidden")}> {children}
); }