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.

159 lines (157 loc) 5.24 kB
'use client'; import * as React from 'react'; import { useStableCallback } from '@base-ui-components/utils/useStableCallback'; import { useIsoLayoutEffect } from '@base-ui-components/utils/useIsoLayoutEffect'; import { CompositeRoot } from "../../composite/root/CompositeRoot.js"; import { tabsStateAttributesMapping } from "../root/stateAttributesMapping.js"; import { useTabsRootContext } from "../root/TabsRootContext.js"; import { TabsListContext } from "./TabsListContext.js"; import { EMPTY_ARRAY } from "../../utils/constants.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"; export const TabsList = /*#__PURE__*/React.forwardRef(function TabsList(componentProps, forwardedRef) { const { activateOnFocus = false, className, loopFocus = true, render, ...elementProps } = componentProps; const { getTabElementBySelectedValue, onValueChange, orientation, value, setTabMap, tabActivationDirection } = useTabsRootContext(); const [highlightedTabIndex, setHighlightedTabIndex] = React.useState(0); const [tabsListElement, setTabsListElement] = React.useState(null); const detectActivationDirection = useActivationDirectionDetector(value, // the old value orientation, tabsListElement, getTabElementBySelectedValue); const onTabActivation = useStableCallback((newValue, eventDetails) => { if (newValue !== value) { const activationDirection = detectActivationDirection(newValue); eventDetails.activationDirection = activationDirection; onValueChange(newValue, eventDetails); } }); const state = React.useMemo(() => ({ orientation, tabActivationDirection }), [orientation, tabActivationDirection]); const defaultProps = { 'aria-orientation': orientation === 'vertical' ? 'vertical' : undefined, role: 'tablist' }; const tabsListContextValue = React.useMemo(() => ({ activateOnFocus, highlightedTabIndex, onTabActivation, setHighlightedTabIndex, tabsListElement, value }), [activateOnFocus, highlightedTabIndex, onTabActivation, setHighlightedTabIndex, tabsListElement, value]); return /*#__PURE__*/_jsx(TabsListContext.Provider, { value: tabsListContextValue, children: /*#__PURE__*/_jsx(CompositeRoot, { render: render, className: className, state: state, refs: [forwardedRef, setTabsListElement], props: [defaultProps, elementProps], stateAttributesMapping: tabsStateAttributesMapping, highlightedIndex: highlightedTabIndex, enableHomeAndEndKeys: true, loopFocus: loopFocus, orientation: orientation, onHighlightedIndexChange: setHighlightedTabIndex, onMapChange: setTabMap, disabledIndices: EMPTY_ARRAY }) }); }); if (process.env.NODE_ENV !== "production") TabsList.displayName = "TabsList"; function getInset(tab, tabsList) { const { left: tabLeft, top: tabTop } = tab.getBoundingClientRect(); const { left: listLeft, top: listTop } = tabsList.getBoundingClientRect(); const left = tabLeft - listLeft; const top = tabTop - listTop; return { left, top }; } function useActivationDirectionDetector( // the old value activeTabValue, orientation, tabsListElement, getTabElement) { const [previousTabEdge, setPreviousTabEdge] = React.useState(null); useIsoLayoutEffect(() => { // Whenever orientation changes, reset the state. if (activeTabValue == null || tabsListElement == null) { setPreviousTabEdge(null); return; } const activeTab = getTabElement(activeTabValue); if (activeTab == null) { setPreviousTabEdge(null); return; } const { left, top } = getInset(activeTab, tabsListElement); setPreviousTabEdge(orientation === 'horizontal' ? left : top); }, [orientation, getTabElement, tabsListElement, activeTabValue]); return React.useCallback(newValue => { if (newValue === activeTabValue) { return 'none'; } if (newValue == null) { setPreviousTabEdge(null); return 'none'; } if (newValue != null && tabsListElement != null) { const activeTabElement = getTabElement(newValue); if (activeTabElement != null) { const { left, top } = getInset(activeTabElement, tabsListElement); if (previousTabEdge == null) { setPreviousTabEdge(orientation === 'horizontal' ? left : top); return 'none'; } if (orientation === 'horizontal') { if (left < previousTabEdge) { setPreviousTabEdge(left); return 'left'; } if (left > previousTabEdge) { setPreviousTabEdge(left); return 'right'; } } else if (top < previousTabEdge) { setPreviousTabEdge(top); return 'up'; } else if (top > previousTabEdge) { setPreviousTabEdge(top); return 'down'; } } } return 'none'; }, [getTabElement, orientation, previousTabEdge, tabsListElement, activeTabValue]); }