import { useRef } from 'react'; import { AriaTabListProps, AriaTabPanelProps, useTab, useTabList, useTabPanel, } from 'react-aria'; import { Item, Node, TabListState, useTabListState } from 'react-stately'; import cn from '~/utils/cn'; export interface TabsProps extends AriaTabListProps { label: string; className?: string; } function Tabs({ label, className, ...props }: TabsProps) { const state = useTabListState(props); const ref = useRef(null); const { tabListProps } = useTabList(props, state, ref); return (
{[...state.collection].map((item) => ( ))}
); } export interface TabsTabProps { item: Node; state: TabListState; } function Tab({ item, state }: TabsTabProps) { const { key, rendered } = item; const ref = useRef(null); const { tabProps } = useTab({ key }, state, ref); return (
{rendered}
); } export interface TabsPanelProps extends AriaTabPanelProps { state: TabListState; } function TabsPanel({ state, ...props }: TabsPanelProps) { const ref = useRef(null); const { tabPanelProps } = useTabPanel(props, state, ref); return (
{state.selectedItem?.props.children}
); } export default Object.assign(Tabs, { Item });