UNPKG

@base-ui-components/react

Version:

Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.

50 lines (49 loc) 1.52 kB
'use client'; import * as React from 'react'; import { mergeReactProps } from '../../utils/mergeReactProps.js'; import { useBaseUiId } from '../../utils/useBaseUiId.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { useCompositeListItem } from '../../composite/list/useCompositeListItem.js'; function useTabsPanel(parameters) { const { getTabIdByPanelValueOrIndex, rootRef: externalRef, selectedValue, value: valueParam } = parameters; const id = useBaseUiId(); const metadata = React.useMemo(() => ({ id, value: valueParam }), [id, valueParam]); const { ref: listItemRef, index } = useCompositeListItem({ metadata }); const tabPanelValue = valueParam ?? index; const panelRef = React.useRef(null); const handleRef = useForkRef(panelRef, listItemRef, externalRef); const hidden = tabPanelValue !== selectedValue; const correspondingTabId = React.useMemo(() => { return getTabIdByPanelValueOrIndex(valueParam, index); }, [getTabIdByPanelValueOrIndex, index, valueParam]); const getRootProps = React.useCallback((externalProps = {}) => { return mergeReactProps(externalProps, { 'aria-labelledby': correspondingTabId, hidden, id: id ?? undefined, role: 'tabpanel', tabIndex: hidden ? -1 : 0, ref: handleRef, 'data-index': index }); }, [correspondingTabId, handleRef, hidden, id, index]); return { hidden, getRootProps, rootRef: handleRef }; } export { useTabsPanel };