{ "name": "tabs", "displayName": "Tabs", "type": "component", "files": [ { "name": "Tabs.tsx", "content": "import {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useId,\n useRef,\n useState,\n type KeyboardEvent,\n type ReactNode,\n} from 'react';\nimport { cn } from '../../utils/cn';\nimport styles from './Tabs.module.css';\n\n// -------------------- Context --------------------\n\ninterface TabsContextValue {\n value: string;\n setValue: (v: string) => void;\n orientation: 'horizontal' | 'vertical';\n register: (\n value: string,\n disabled: boolean,\n el: HTMLButtonElement | null,\n ) => void;\n baseId: string;\n}\nconst TabsContext = createContext(null);\nconst useTabsContext = () => {\n const ctx = useContext(TabsContext);\n if (!ctx) throw new Error('Tabs.* must be used within ');\n return ctx;\n};\n\n// -------------------- Root --------------------\n\nexport interface TabsProps {\n defaultValue?: string;\n value?: string;\n onChange?: (value: string) => void;\n orientation?: 'horizontal' | 'vertical';\n className?: string;\n children: ReactNode;\n}\n\nfunction TabsRoot({\n defaultValue = '',\n value,\n onChange,\n orientation = 'horizontal',\n className,\n children,\n}: TabsProps) {\n const [internal, setInternal] = useState(defaultValue);\n const baseId = useId();\n const current = value !== undefined ? value : internal;\n\n const setValue = useCallback(\n (v: string) => {\n if (value === undefined) setInternal(v);\n onChange?.(v);\n },\n [value, onChange],\n );\n\n // Track registered tab elements for arrow-key navigation.\n const tabs = useRef<\n Map\n >(new Map());\n const register = useCallback(\n (v: string, disabled: boolean, el: HTMLButtonElement | null) => {\n if (el) tabs.current.set(v, { disabled, el });\n else tabs.current.delete(v);\n },\n [],\n );\n\n return (\n \n \n {children}\n \n \n );\n}\n\n// -------------------- List --------------------\n\nexport interface TabsListProps {\n className?: string;\n children: ReactNode;\n 'aria-label'?: string;\n}\nfunction List({ className, children, 'aria-label': ariaLabel }: TabsListProps) {\n const { orientation, baseId } = useTabsContext();\n return (\n \n {children}\n \n );\n}\n\n// -------------------- Tab --------------------\n\nexport interface TabProps {\n value: string;\n disabled?: boolean;\n className?: string;\n children: ReactNode;\n}\nfunction Tab({ value, disabled = false, className, children }: TabProps) {\n const {\n value: selected,\n setValue,\n register,\n baseId,\n orientation,\n } = useTabsContext();\n const btnRef = useRef(null);\n const isSelected = selected === value;\n const tabId = `${baseId}-tab-${value}`;\n const panelId = `${baseId}-panel-${value}`;\n\n useEffect(() => {\n register(value, disabled, btnRef.current);\n return () => register(value, disabled, null);\n }, [value, disabled, register]);\n\n const onKeyDown = (e: KeyboardEvent) => {\n const nextKeys =\n orientation === 'horizontal' ? ['ArrowRight'] : ['ArrowDown'];\n const prevKeys = orientation === 'horizontal' ? ['ArrowLeft'] : ['ArrowUp'];\n const parent = btnRef.current?.parentElement;\n if (!parent) return;\n const buttons = Array.from(\n parent.querySelectorAll(\n '[role=\"tab\"]:not([aria-disabled=\"true\"])',\n ),\n );\n if (!btnRef.current) return;\n const idx = buttons.indexOf(btnRef.current);\n if (idx === -1) return;\n if (nextKeys.includes(e.key)) {\n e.preventDefault();\n const next = buttons[(idx + 1) % buttons.length];\n next.focus();\n setValue(next.getAttribute('data-value') ?? value);\n } else if (prevKeys.includes(e.key)) {\n e.preventDefault();\n const prev = buttons[(idx - 1 + buttons.length) % buttons.length];\n prev.focus();\n setValue(prev.getAttribute('data-value') ?? value);\n } else if (e.key === 'Home') {\n e.preventDefault();\n buttons[0]?.focus();\n setValue(buttons[0]?.getAttribute('data-value') ?? value);\n } else if (e.key === 'End') {\n e.preventDefault();\n const last = buttons[buttons.length - 1];\n last?.focus();\n setValue(last?.getAttribute('data-value') ?? value);\n }\n };\n\n return (\n !disabled && setValue(value)}\n onKeyDown={onKeyDown}\n >\n {children}\n \n );\n}\n\n// -------------------- Panel --------------------\n\nexport interface TabsPanelProps {\n value: string;\n className?: string;\n children: ReactNode;\n}\nfunction Panel({ value, className, children }: TabsPanelProps) {\n const { value: selected, baseId } = useTabsContext();\n if (selected !== value) return null;\n return (\n \n {children}\n \n );\n}\n\n/**\n * Compound Tabs component: ``\n */\nexport const Tabs = Object.assign(TabsRoot, { List, Tab, Panel });\n" }, { "name": "Tabs.module.css", "content": ".root {\n display: flex;\n font-family: var(--tatva-font-family);\n}\n\n.rootHorizontal {\n flex-direction: column;\n}\n.rootVertical {\n flex-direction: row;\n gap: var(--tatva-space-4);\n}\n\n.list {\n display: flex;\n gap: var(--tatva-space-1);\n border-bottom: 1px solid var(--tatva-color-border);\n}\n\n.rootVertical .list {\n flex-direction: column;\n border-bottom: none;\n border-right: 1px solid var(--tatva-color-border);\n}\n\n.tab {\n background: transparent;\n border: none;\n padding: var(--tatva-space-2) var(--tatva-space-3);\n font-family: inherit;\n font-size: var(--tatva-font-size-md);\n color: var(--tatva-color-fg-muted);\n cursor: pointer;\n position: relative;\n border-radius: var(--tatva-radius-sm) var(--tatva-radius-sm) 0 0;\n transition:\n color var(--tatva-transition-fast),\n background-color var(--tatva-transition-fast);\n}\n\n.tab:hover:not([aria-disabled='true']) {\n color: var(--tatva-color-fg);\n background-color: var(--tatva-color-neutral-100);\n}\n\n.tab:focus-visible {\n outline: none;\n box-shadow: 0 0 0 3px var(--tatva-color-primary-200);\n border-radius: var(--tatva-radius-sm);\n}\n\n.tab[aria-disabled='true'] {\n opacity: 0.45;\n cursor: not-allowed;\n}\n\n.tabActive {\n color: var(--tatva-color-primary-700);\n font-weight: var(--tatva-font-weight-semibold);\n}\n\n.tabActive::after {\n content: '';\n position: absolute;\n left: 0;\n right: 0;\n bottom: -1px;\n height: 2px;\n background-color: var(--tatva-color-primary-600);\n transition: transform var(--tatva-transition-base);\n}\n\n.rootVertical .tabActive::after {\n left: auto;\n right: -1px;\n top: 0;\n bottom: 0;\n height: auto;\n width: 2px;\n}\n\n.panel {\n padding: var(--tatva-space-4) 0;\n outline: none;\n}\n\n.rootVertical .panel {\n flex: 1;\n padding: 0;\n}\n" }, { "name": "index.ts", "content": "export { Tabs } from './Tabs';\nexport type {\n TabsProps,\n TabsListProps,\n TabProps,\n TabsPanelProps,\n} from './Tabs';\n" } ], "registryDependencies": [], "dependencies": [], "devDependencies": [] }