@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.
99 lines (96 loc) • 4.13 kB
JavaScript
;
'use client';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useTabsTab = useTabsTab;
var React = _interopRequireWildcard(require("react"));
var _mergeReactProps = require("../../utils/mergeReactProps");
var _useEnhancedEffect = require("../../utils/useEnhancedEffect");
var _useForkRef = require("../../utils/useForkRef");
var _useBaseUiId = require("../../utils/useBaseUiId");
var _useButton = require("../../use-button");
var _useCompositeItem = require("../../composite/item/useCompositeItem");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function useTabsTab(parameters) {
const {
activateOnFocus,
disabled = false,
getTabPanelIdByTabValueOrIndex,
highlightedTabIndex,
id: idParam,
onTabActivation,
rootRef: externalRef,
selectedTabValue,
setHighlightedTabIndex,
value: valueParam
} = parameters;
const id = (0, _useBaseUiId.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
} = (0, _useCompositeItem.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
(0, _useEnhancedEffect.useEnhancedEffect)(() => {
if (activateOnFocus && selected && index > -1 && highlightedTabIndex !== index) {
setHighlightedTabIndex(index);
}
}, [activateOnFocus, highlightedTabIndex, index, selected, setHighlightedTabIndex]);
const {
getButtonProps,
buttonRef
} = (0, _useButton.useButton)({
disabled,
focusableWhenDisabled: true,
type: 'button'
});
const handleRef = (0, _useForkRef.useForkRef)(compositeItemRef, buttonRef, externalRef);
const tabPanelId = index > -1 ? getTabPanelIdByTabValueOrIndex(valueParam, index) : undefined;
const getRootProps = React.useCallback((externalProps = {}) => {
return (0, _mergeReactProps.mergeReactProps)(externalProps, (0, _mergeReactProps.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);
}
}
}, (0, _mergeReactProps.mergeReactProps)(getItemProps(), getButtonProps())));
}, [activateOnFocus, getButtonProps, getItemProps, handleRef, id, onTabActivation, selected, selectedTabValue, tabPanelId, tabValue]);
return {
getRootProps,
index,
rootRef: handleRef,
selected
};
}