@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.
120 lines (118 loc) • 4.06 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { CompositeRoot } from '../../composite/root/CompositeRoot.js';
import { tabsStyleHookMapping } from '../root/styleHooks.js';
import { useTabsRootContext } from '../root/TabsRootContext.js';
import { useTabsList } from './useTabsList.js';
import { TabsListContext } from './TabsListContext.js';
/**
* Groups the individual tab buttons.
* Renders a `<div>` element.
*
* Documentation: [Base UI Tabs](https://base-ui.com/react/components/tabs)
*/
import { jsx as _jsx } from "react/jsx-runtime";
const TabsList = /*#__PURE__*/React.forwardRef(function TabsList(props, forwardedRef) {
const {
activateOnFocus = true,
className,
loop = true,
render,
...other
} = props;
const {
direction,
getTabElementBySelectedValue,
onValueChange,
orientation,
value,
setTabMap,
tabActivationDirection
} = useTabsRootContext();
const [highlightedTabIndex, setHighlightedTabIndex] = React.useState(0);
const tabsListRef = React.useRef(null);
const {
getRootProps,
onTabActivation
} = useTabsList({
getTabElementBySelectedValue,
onValueChange,
orientation,
rootRef: forwardedRef,
setTabMap,
tabsListRef,
value
});
const state = React.useMemo(() => ({
orientation,
tabActivationDirection
}), [orientation, tabActivationDirection]);
const {
renderElement
} = useComponentRenderer({
propGetter: getRootProps,
render: render ?? 'div',
className,
state,
extraProps: other,
customStyleHookMapping: tabsStyleHookMapping
});
const tabsListContextValue = React.useMemo(() => ({
activateOnFocus,
highlightedTabIndex,
onTabActivation,
setHighlightedTabIndex,
tabsListRef,
value
}), [activateOnFocus, highlightedTabIndex, onTabActivation, setHighlightedTabIndex, tabsListRef, value]);
return /*#__PURE__*/_jsx(TabsListContext.Provider, {
value: tabsListContextValue,
children: /*#__PURE__*/_jsx(CompositeRoot, {
highlightedIndex: highlightedTabIndex,
enableHomeAndEndKeys: true,
loop: loop,
direction: direction,
orientation: orientation,
onHighlightedIndexChange: setHighlightedTabIndex,
onMapChange: setTabMap,
render: renderElement()
})
});
});
export { TabsList };
process.env.NODE_ENV !== "production" ? TabsList.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* Whether to automatically change the active tab on arrow key focus.
* Otherwise, tabs will be activated using Enter or Spacebar key press.
* @default true
*/
activateOnFocus: PropTypes.bool,
/**
* @ignore
*/
children: PropTypes.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* Whether to loop keyboard focus back to the first item
* when the end of the list is reached while using the arrow keys.
* @default true
*/
loop: PropTypes.bool,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
} : void 0;