@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.
96 lines (90 loc) • 3.98 kB
JavaScript
;
'use client';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.useTabsRoot = useTabsRoot;
var React = _interopRequireWildcard(require("react"));
var _useControlled = require("../../utils/useControlled");
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 useTabsRoot(parameters) {
const {
value: valueProp,
defaultValue,
onValueChange: onValueChangeProp
} = parameters;
const tabPanelRefs = React.useRef([]);
const [value, setValue] = (0, _useControlled.useControlled)({
controlled: valueProp,
default: defaultValue,
name: 'Tabs',
state: 'value'
});
const [tabPanelMap, setTabPanelMap] = React.useState(() => new Map());
const [tabMap, setTabMap] = React.useState(() => new Map());
const [tabActivationDirection, setTabActivationDirection] = React.useState('none');
const onValueChange = React.useCallback((newValue, activationDirection, event) => {
setValue(newValue);
setTabActivationDirection(activationDirection);
onValueChangeProp?.(newValue, event);
}, [onValueChangeProp, setValue]);
// get the `id` attribute of <Tabs.Panel> to set as the value of `aria-controls` on <Tabs.Tab>
const getTabPanelIdByTabValueOrIndex = React.useCallback((tabValue, index) => {
if (tabValue === undefined && index < 0) {
return undefined;
}
for (const tabPanelMetadata of tabPanelMap.values()) {
// find by tabValue
if (tabValue !== undefined && tabPanelMetadata && tabValue === tabPanelMetadata?.value) {
return tabPanelMetadata.id;
}
// find by index
if (tabValue === undefined && tabPanelMetadata?.index && tabPanelMetadata?.index === index) {
return tabPanelMetadata.id;
}
}
return undefined;
}, [tabPanelMap]);
// get the `id` attribute of <Tabs.Tab> to set as the value of `aria-labelledby` on <Tabs.Panel>
const getTabIdByPanelValueOrIndex = React.useCallback((tabPanelValue, index) => {
if (tabPanelValue === undefined && index < 0) {
return undefined;
}
for (const tabMetadata of tabMap.values()) {
// find by tabPanelValue
if (tabPanelValue !== undefined && index > -1 && tabPanelValue === (tabMetadata?.value ?? tabMetadata?.index ?? undefined)) {
return tabMetadata?.id;
}
// find by index
if (tabPanelValue === undefined && index > -1 && index === (tabMetadata?.value ?? tabMetadata?.index ?? undefined)) {
return tabMetadata?.id;
}
}
return undefined;
}, [tabMap]);
// used in `useActivationDirectionDetector` for setting data-activation-direction
const getTabElementBySelectedValue = React.useCallback(selectedValue => {
if (selectedValue === undefined) {
return null;
}
for (const [tabElement, tabMetadata] of tabMap.entries()) {
if (tabMetadata != null && selectedValue === (tabMetadata.value ?? tabMetadata.index)) {
return tabElement;
}
}
return null;
}, [tabMap]);
return {
getTabElementBySelectedValue,
getTabIdByPanelValueOrIndex,
getTabPanelIdByTabValueOrIndex,
onValueChange,
setTabMap,
setTabPanelMap,
tabActivationDirection,
tabMap,
tabPanelRefs,
value
};
}