'use client'; import {mergeProps} from 'react-aria/mergeProps'; import {useTab, useTabList, useTabPanel, type AriaTabListOptions} from 'react-aria/useTabList'; import { useTabListState, type TabListState, type TabListStateOptions } from 'react-stately/useTabListState'; import {useFocusRing} from 'react-aria/useFocusRing'; import {useObjectRef} from 'react-aria/useObjectRef'; import {useHover} from 'react-aria/useHover'; import {CollectionBuilder, createLeafComponent} from 'react-aria/CollectionBuilder'; import {Collection} from 'react-aria-components/Collection'; import {ItemNode} from 'react-aria/private/collections/BaseCollection'; import type {Collection as ICollection, Key, Node} from '@react-types/shared'; import {createContext, Fragment, useContext, useRef} from 'react'; import type {ForwardedRef, ReactNode} from 'react'; import './Tabs.css'; let TabListStateContext = createContext | null>(null); export interface TabProps { id?: Key; title?: ReactNode; children?: ReactNode; } export const Tab = createLeafComponent( ItemNode, function Tab(_props: TabProps, forwardedRef: ForwardedRef, item: Node) { let state = useContext(TabListStateContext)!; let ref = useObjectRef(forwardedRef); let {tabProps, isSelected, isDisabled, isPressed} = useTab({key: item.key}, state, ref); let {hoverProps, isHovered} = useHover({isDisabled}); let {focusProps, isFocusVisible} = useFocusRing(); return (
{item.props.title}
); } ); export type TabsProps = AriaTabListOptions & TabListStateOptions; export function Tabs(props: TabsProps) { return ( }> {collection => } ); } function TabsInner({ collection, ...props }: AriaTabListOptions & Omit, 'children'> & { collection: ICollection>; }) { let state = useTabListState({...props, collection, children: undefined}); let ref = useRef(null); /*- begin highlight -*/ let {tabListProps} = useTabList(props, state, ref); /*- end highlight -*/ let orientation = props.orientation || 'horizontal'; return (
{[...state.collection].map(item => ( {item.render!(item)} ))}
{/* Re-mount the panel when the selection changes so DOM state isn't shared between tabs. */}
); } function TabPanel({state, ...props}: {state: TabListState}) { let ref = useRef(null); let {tabPanelProps} = useTabPanel(props, state, ref); let {focusProps, isFocusVisible} = useFocusRing(); return (
{state.selectedItem?.props.children}
); }