@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 (119 loc) • 4.12 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { CompositeList } from '../../composite/list/CompositeList.js';
import { useDirection } from '../../direction-provider/DirectionContext.js';
import { useTabsRoot } from './useTabsRoot.js';
import { TabsRootContext } from './TabsRootContext.js';
import { tabsStyleHookMapping } from './styleHooks.js';
import { jsx as _jsx } from "react/jsx-runtime";
/**
* Groups the tabs and the corresponding panels.
* Renders a `<div>` element.
*
* Documentation: [Base UI Tabs](https://base-ui.com/react/components/tabs)
*/
const TabsRoot = /*#__PURE__*/React.forwardRef(function TabsRoot(props, forwardedRef) {
const {
className,
defaultValue = 0,
onValueChange: onValueChangeProp,
orientation = 'horizontal',
render,
value: valueProp,
...other
} = props;
const direction = useDirection();
const {
getTabElementBySelectedValue,
getTabIdByPanelValueOrIndex,
getTabPanelIdByTabValueOrIndex,
onValueChange,
setTabMap,
setTabPanelMap,
tabActivationDirection,
tabPanelRefs,
value
} = useTabsRoot({
value: valueProp,
defaultValue,
onValueChange: onValueChangeProp
});
const tabsContextValue = React.useMemo(() => ({
direction,
getTabElementBySelectedValue,
getTabIdByPanelValueOrIndex,
getTabPanelIdByTabValueOrIndex,
onValueChange,
orientation,
setTabMap,
tabActivationDirection,
value
}), [direction, getTabElementBySelectedValue, getTabIdByPanelValueOrIndex, getTabPanelIdByTabValueOrIndex, onValueChange, orientation, setTabMap, tabActivationDirection, value]);
const state = {
orientation,
tabActivationDirection
};
const {
renderElement
} = useComponentRenderer({
render: render ?? 'div',
className,
state,
extraProps: other,
ref: forwardedRef,
customStyleHookMapping: tabsStyleHookMapping
});
return /*#__PURE__*/_jsx(TabsRootContext.Provider, {
value: tabsContextValue,
children: /*#__PURE__*/_jsx(CompositeList, {
elementsRef: tabPanelRefs,
onMapChange: setTabPanelMap,
children: renderElement()
})
});
});
export { TabsRoot };
process.env.NODE_ENV !== "production" ? TabsRoot.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @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]),
/**
* The default value. Use when the component is not controlled.
* When the value is `null`, no Tab will be selected.
* @default 0
*/
defaultValue: PropTypes.any,
/**
* Callback invoked when new value is being set.
*/
onValueChange: PropTypes.func,
/**
* The component orientation (layout flow direction).
* @default 'horizontal'
*/
orientation: PropTypes.oneOf(['horizontal', 'vertical']),
/**
* 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]),
/**
* The value of the currently selected `Tab`. Use when the component is controlled.
* When the value is `null`, no Tab will be selected.
*/
value: PropTypes.any
} : void 0;