@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.
93 lines (90 loc) • 3.1 kB
JavaScript
'use client';
import * as React from 'react';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useBaseUiId } from '../../utils/useBaseUiId.js';
import { useButton } from '../../use-button/index.js';
import { useCompositeItem } from '../../composite/item/useCompositeItem.js';
function useTabsTab(parameters) {
const {
activateOnFocus,
disabled = false,
getTabPanelIdByTabValueOrIndex,
highlightedTabIndex,
id: idParam,
onTabActivation,
rootRef: externalRef,
selectedTabValue,
setHighlightedTabIndex,
value: valueParam
} = parameters;
const id = useBaseUiId(idParam);
const tabMetadata = React.useMemo(() => ({
disabled,
id,
value: valueParam
}), [disabled, id, valueParam]);
const {
getItemProps,
ref: compositeItemRef,
index
// hook is used instead of the CompositeItem component
// because the index is needed for Tab internals
} = useCompositeItem({
metadata: tabMetadata
});
const tabValue = valueParam ?? index;
// the `selected` state isn't set on the server (it relies on effects to be calculated),
// so we fall back to checking the `value` param with the selectedTabValue from the TabsContext
const selected = React.useMemo(() => {
if (valueParam === undefined) {
return index < 0 ? false : index === selectedTabValue;
}
return valueParam === selectedTabValue;
}, [index, selectedTabValue, valueParam]);
// when activateOnFocus is `true`, ensure the active item in Composite's roving
// focus group matches the selected Tab
useEnhancedEffect(() => {
if (activateOnFocus && selected && index > -1 && highlightedTabIndex !== index) {
setHighlightedTabIndex(index);
}
}, [activateOnFocus, highlightedTabIndex, index, selected, setHighlightedTabIndex]);
const {
getButtonProps,
buttonRef
} = useButton({
disabled,
focusableWhenDisabled: true,
type: 'button'
});
const handleRef = useForkRef(compositeItemRef, buttonRef, externalRef);
const tabPanelId = index > -1 ? getTabPanelIdByTabValueOrIndex(valueParam, index) : undefined;
const getRootProps = React.useCallback((externalProps = {}) => {
return mergeReactProps(externalProps, mergeReactProps({
role: 'tab',
'aria-controls': tabPanelId,
'aria-selected': selected,
id,
ref: handleRef,
onClick(event) {
onTabActivation(tabValue, event.nativeEvent);
},
onFocus(event) {
if (!activateOnFocus) {
return;
}
if (selectedTabValue !== tabValue) {
onTabActivation(tabValue, event.nativeEvent);
}
}
}, mergeReactProps(getItemProps(), getButtonProps())));
}, [activateOnFocus, getButtonProps, getItemProps, handleRef, id, onTabActivation, selected, selectedTabValue, tabPanelId, tabValue]);
return {
getRootProps,
index,
rootRef: handleRef,
selected
};
}
export { useTabsTab };