"use client"; import { Tab, TabGroup, TabList, TabPanel, TabPanels } from "@headlessui/react"; import { clsx } from "clsx"; import { createContext, FC, Fragment, PropsWithChildren, useContext, } from "react"; import { IconProps } from "../index.js"; type StyledTabProps = { name: string; icon?: FC; }; type StyledTabButtonType = StyledTabProps & { isSelected: boolean; }; type StyledTabTheme = "default" | "primary"; const ThemeContext = createContext("default"); type ListProps = PropsWithChildren<{ theme?: StyledTabTheme; stretch?: boolean; }>; type StyledTabType = FC & { TabStyle: string; List: FC; ListDiv: FC; Button: FC; Group: typeof TabGroup; Panels: typeof Tab.Panels; Panel: typeof Tab.Panel; }; const tabStyle = "group flex flex-1 rounded-md focus:outline-none focus-visible:ring-offset-gray-100 hover:bg-gray-300 mx-px cursor-pointer"; const StyledTabInner: FC = ({ name, isSelected, icon: Icon, }) => { const theme = useContext(ThemeContext); return ( {Icon && ( )} {name} ); }; //The really annoying thing is that if button isn't in this component, then clicking it won't work. //So we have to include button here, don't try to move into StyledTabInner export const StyledTab: StyledTabType = ({ name, icon: Icon }) => ( {({ selected }) => ( )} ); const StyledTabButton: FC = (props) => (
); function tabListStyle(stretch: boolean) { return clsx( "flex py-0.5 px-0.5 rounded-md bg-gray-100 hover:bg-gray-200", stretch ? "w-full" : "w-fit" ); } StyledTab.List = function StyledTabList({ children, theme = "default", stretch = false, }) { return ( {children} ); }; StyledTab.ListDiv = function StyledTabListDiv({ children, theme = "default", stretch = false, }) { return (
{children}
); }; StyledTab.TabStyle = tabStyle; StyledTab.Group = TabGroup; StyledTab.Button = StyledTabButton; StyledTab.Panels = TabPanels; StyledTab.Panel = TabPanel;