File size: 1,271 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as Ariakit from '@ariakit/react';
import warning from '@wordpress/warning';
import clsx from 'clsx';
import { forwardRef } from 'react';
import { useTabsContext } from './context';
import styles from './style.module.scss';
import type { TabPanelProps } from './types';

export const TabPanel = forwardRef<
	HTMLDivElement,
	Omit< React.ComponentPropsWithoutRef< 'div' >, 'id' > & TabPanelProps
>( function TabPanel( { children, tabId, focusable = true, ...otherProps }, ref ) {
	const context = useTabsContext();
	const selectedId = Ariakit.useStoreState( context?.store, 'selectedId' );
	if ( ! context ) {
		warning( '`Tabs.TabPanel` must be wrapped in a `Tabs` component.' );
		return null;
	}
	const { store, instanceId } = context;
	const instancedTabId = `${ instanceId }-${ tabId }`;

	return (
		<Ariakit.TabPanel
			ref={ ref }
			store={ store }
			// For TabPanel, the id passed here is the id attribute of the DOM
			// element.
			// `tabId` is the id of the tab that controls this panel.
			id={ `${ instancedTabId }-view` }
			tabId={ instancedTabId }
			focusable={ focusable }
			{ ...otherProps }
			className={ clsx( styles.tabpanel, otherProps.className ) }
		>
			{ selectedId === instancedTabId && children }
		</Ariakit.TabPanel>
	);
} );