UNPKG

1.59 kBJavaScriptView Raw
1'use client';
2
3import _extends from "@babel/runtime/helpers/esm/extends";
4import * as React from 'react';
5import { unstable_useId as useId, unstable_useForkRef as useForkRef } from '@mui/utils';
6import { useTabsContext } from '../Tabs';
7import { useCompoundItem } from '../useCompound';
8function tabPanelValueGenerator(otherTabPanelValues) {
9 return otherTabPanelValues.size;
10}
11
12/**
13 *
14 * Demos:
15 *
16 * - [Tabs](https://mui.com/base-ui/react-tabs/#hooks)
17 *
18 * API:
19 *
20 * - [useTabPanel API](https://mui.com/base-ui/react-tabs/hooks-api/#use-tab-panel)
21 */
22function useTabPanel(parameters) {
23 const {
24 value: valueParam,
25 id: idParam,
26 rootRef: externalRef
27 } = parameters;
28 const context = useTabsContext();
29 if (context === null) {
30 throw new Error('No TabContext provided');
31 }
32 const {
33 value: selectedTabValue,
34 getTabId
35 } = context;
36 const id = useId(idParam);
37 const ref = React.useRef(null);
38 const handleRef = useForkRef(ref, externalRef);
39 const metadata = React.useMemo(() => ({
40 id,
41 ref
42 }), [id]);
43 const {
44 id: value
45 } = useCompoundItem(valueParam ?? tabPanelValueGenerator, metadata);
46 const hidden = value !== selectedTabValue;
47 const correspondingTabId = value !== undefined ? getTabId(value) : undefined;
48 const getRootProps = (externalProps = {}) => {
49 return _extends({
50 'aria-labelledby': correspondingTabId ?? undefined,
51 hidden,
52 id: id ?? undefined
53 }, externalProps, {
54 ref: handleRef
55 });
56 };
57 return {
58 hidden,
59 getRootProps,
60 rootRef: handleRef
61 };
62}
63export { useTabPanel };
\No newline at end of file