import { createContext, useCallback, useContext, useId, useState, type ButtonHTMLAttributes, type HTMLAttributes, type ReactNode, type Ref, } from 'react'; import { classes } from '../internal/classes'; import { useRovingFocus } from '../internal/useRovingFocus'; import styles from './Tabs.module.css'; export type TabsOrientation = 'horizontal' | 'vertical'; export type TabsActivation = 'automatic' | 'manual'; interface TabsContextValue { value: string; setValue: (value: string) => void; orientation: TabsOrientation; activation: TabsActivation; baseId: string; } const TabsContext = createContext(null); function useTabs(component: string): TabsContextValue { const context = useContext(TabsContext); if (!context) throw new Error(`${component} must be rendered inside `); return context; } export interface TabsProps extends Omit, 'defaultValue' | 'onChange'> { /** Controlled selected tab. */ value?: string; /** Initially selected tab when uncontrolled. */ defaultValue?: string; onValueChange?: (value: string) => void; /** Layout and arrow-key axis. @default 'horizontal' */ orientation?: TabsOrientation; /** * `automatic` selects a tab when it receives focus (arrow keys); `manual` * moves focus only and selects on Enter, Space or click. * @default 'automatic' */ activation?: TabsActivation; ref?: Ref; children: ReactNode; } /** * Tabbed sections following the WAI-ARIA Tabs pattern. Compose with * `Tabs.List`, `Tabs.Tab` and `Tabs.Panel`; ids and ARIA relationships are * wired automatically. One tab stop: arrows move between tabs, Home and End * jump to the ends, disabled tabs are skipped. State on the DOM: * `data-orientation`, `data-state="active|inactive"`. * * @status stable * @category Navigation * @accessibility The WAI-ARIA Tabs pattern: `tablist` with `aria-orientation`, `tab`s carrying `aria-selected` and `aria-controls`, `tabpanel`s labelled by their tab. One tab stop: arrows move (and select with automatic activation), Home/End jump, disabled tabs are skipped. * @do Use `activation="manual"` when switching a tab is expensive. * Name the `Tabs.List` with `aria-label`. * @dont Use tabs for sequential steps; use a stepper pattern. * Put a link inside a tab. */ export function Tabs({ value, defaultValue, onValueChange, orientation = 'horizontal', activation = 'automatic', className, ref, children, ...rest }: TabsProps) { const [uncontrolled, setUncontrolled] = useState(defaultValue ?? ''); const controlled = value !== undefined; const current = controlled ? value : uncontrolled; const setValue = useCallback( (next: string) => { if (!controlled) setUncontrolled(next); onValueChange?.(next); }, [controlled, onValueChange], ); const baseId = useId(); return (
{children}
); } interface TabsListProps extends HTMLAttributes { ref?: Ref; children: ReactNode; } export function TabsList({ className, ref, onKeyDown, children, ...rest }: TabsListProps) { const { orientation, activation, setValue } = useTabs('Tabs.List'); const rove = useRovingFocus({ orientation, selector: '[role="tab"]', onFocusItem: (item) => { if (activation === 'automatic' && item.dataset.value !== undefined) setValue(item.dataset.value); }, }); return (
{ onKeyDown?.(event); if (!event.defaultPrevented) rove(event); }} > {children}
); } interface TabsTabProps extends Omit, 'value'> { /** Identifies the tab; the panel with the same value belongs to it. */ value: string; ref?: Ref; children: ReactNode; } export function TabsTab({ value, disabled, className, onClick, ref, children, ...rest }: TabsTabProps) { const tabs = useTabs('Tabs.Tab'); const selected = tabs.value === value; return ( ); } interface TabsPanelProps extends HTMLAttributes { value: string; ref?: Ref; children?: ReactNode; } export function TabsPanel({ value, className, ref, children, ...rest }: TabsPanelProps) { const tabs = useTabs('Tabs.Panel'); const selected = tabs.value === value; return ( ); } Tabs.displayName = 'Tabs'; TabsList.displayName = 'Tabs.List'; TabsTab.displayName = 'Tabs.Tab'; TabsPanel.displayName = 'Tabs.Panel'; Tabs.List = TabsList; Tabs.Tab = TabsTab; Tabs.Panel = TabsPanel;