UNPKG

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