/
tabpanel.tsx
48 lines (42 loc) · 1.11 KB
/
tabpanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* External dependencies
*/
/**
* WordPress dependencies
*/
import { forwardRef } from '@wordpress/element';
/**
* Internal dependencies
*/
import type { TabPanelProps } from './types';
import { TabPanel as StyledTabPanel } from './styles';
import warning from '@wordpress/warning';
import { useTabsContext } from './context';
import type { WordPressComponentProps } from '../context';
export const TabPanel = forwardRef<
HTMLDivElement,
Omit< WordPressComponentProps< TabPanelProps, 'div', false >, 'id' >
>( function TabPanel(
{ children, tabId, focusable = true, ...otherProps },
ref
) {
const context = useTabsContext();
if ( ! context ) {
warning( '`Tabs.TabPanel` must be wrapped in a `Tabs` component.' );
return null;
}
const { store, instanceId } = context;
const instancedTabId = `${ instanceId }-${ tabId }`;
const selectedId = store.useState( ( state ) => state.selectedId );
return (
<StyledTabPanel
ref={ ref }
store={ store }
id={ instancedTabId }
focusable={ focusable }
{ ...otherProps }
>
{ selectedId === instancedTabId && children }
</StyledTabPanel>
);
} );