UNPKG

@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.

70 lines (68 loc) 2.22 kB
'use client'; import * as React from 'react'; import { useBaseUiId } from "../../utils/useBaseUiId.js"; import { useRenderElement } from "../../utils/useRenderElement.js"; import { useCompositeListItem } from "../../composite/list/useCompositeListItem.js"; import { tabsStateAttributesMapping } from "../root/stateAttributesMapping.js"; import { useTabsRootContext } from "../root/TabsRootContext.js"; import { TabsPanelDataAttributes } from "./TabsPanelDataAttributes.js"; /** * A panel displayed when the corresponding tab is active. * Renders a `<div>` element. * * Documentation: [Base UI Tabs](https://base-ui.com/react/components/tabs) */ export const TabsPanel = /*#__PURE__*/React.forwardRef(function TabPanel(componentProps, forwardedRef) { const { children, className, value: valueProp, render, keepMounted = false, ...elementProps } = componentProps; const { value: selectedValue, getTabIdByPanelValueOrIndex, orientation, tabActivationDirection } = useTabsRootContext(); const id = useBaseUiId(); const metadata = React.useMemo(() => ({ id, value: valueProp }), [id, valueProp]); const { ref: listItemRef, index } = useCompositeListItem({ metadata }); const tabPanelValue = valueProp ?? index; const hidden = tabPanelValue !== selectedValue; const correspondingTabId = React.useMemo(() => { return getTabIdByPanelValueOrIndex(valueProp, index); }, [getTabIdByPanelValueOrIndex, index, valueProp]); const state = React.useMemo(() => ({ hidden, orientation, tabActivationDirection }), [hidden, orientation, tabActivationDirection]); const element = useRenderElement('div', componentProps, { state, ref: [forwardedRef, listItemRef], props: [{ 'aria-labelledby': correspondingTabId, hidden, id: id ?? undefined, role: 'tabpanel', tabIndex: hidden ? -1 : 0, [TabsPanelDataAttributes.index]: index }, elementProps, { children: hidden && !keepMounted ? undefined : children }], stateAttributesMapping: tabsStateAttributesMapping }); return element; }); if (process.env.NODE_ENV !== "production") TabsPanel.displayName = "TabsPanel";