Skip to content

Commit

Permalink
Tabs: cleanup and improvements (#56224)
Browse files Browse the repository at this point in the history
  • Loading branch information
chad1008 authored Nov 17, 2023
1 parent 55285a8 commit bb8adcd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 9 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Experimental

- `Tabs`: Memoize and expose the component context ([#56224](https://github.com/WordPress/gutenberg/pull/56224)).

## 25.12.0 (2023-11-16)

### Bug Fix
Expand Down
14 changes: 12 additions & 2 deletions packages/components/src/tabs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as Ariakit from '@ariakit/react';
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { useLayoutEffect, useRef } from '@wordpress/element';
import { useLayoutEffect, useMemo, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -154,8 +154,16 @@ function Tabs( {
setSelectedId,
] );

const contextValue = useMemo(
() => ( {
store,
instanceId,
} ),
[ store, instanceId ]
);

return (
<TabsContext.Provider value={ { store, instanceId } }>
<TabsContext.Provider value={ contextValue }>
{ children }
</TabsContext.Provider>
);
Expand All @@ -164,4 +172,6 @@ function Tabs( {
Tabs.TabList = TabList;
Tabs.Tab = Tab;
Tabs.TabPanel = TabPanel;
Tabs.Context = TabsContext;

export default Tabs;
8 changes: 8 additions & 0 deletions packages/components/src/tabs/stories/index.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import Button from '../../button';
const meta: Meta< typeof Tabs > = {
title: 'Components (Experimental)/Tabs',
component: Tabs,
subcomponents: {
// @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170
'Tabs.TabList': Tabs.TabList,
// @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170
'Tabs.Tab': Tabs.Tab,
// @ts-expect-error - See https://github.com/storybookjs/storybook/issues/23170
'Tabs.TabPanel': Tabs.TabPanel,
},
parameters: {
actions: { argTypesRegex: '^on.*' },
controls: { expanded: true },
Expand Down
8 changes: 4 additions & 4 deletions packages/components/src/tabs/tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
* WordPress dependencies
*/

import { useContext, forwardRef } from '@wordpress/element';
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import type { TabProps } from './types';
import warning from '@wordpress/warning';
import { TabsContext } from './context';
import { useTabsContext } from './context';
import { Tab as StyledTab } from './styles';
import type { WordPressComponentProps } from '../context';

export const Tab = forwardRef<
HTMLButtonElement,
WordPressComponentProps< TabProps, 'button', false >
>( function Tab( { children, id, disabled, render, ...otherProps }, ref ) {
const context = useContext( TabsContext );
const context = useTabsContext();
if ( ! context ) {
warning( '`Tabs.TabList` must be wrapped in a `Tabs` component.' );
warning( '`Tabs.Tab` must be wrapped in a `Tabs` component.' );
return null;
}
const { store, instanceId } = context;
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/tabs/tabpanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* WordPress dependencies
*/

import { forwardRef, useContext } from '@wordpress/element';
import { forwardRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -15,14 +15,14 @@ import type { TabPanelProps } from './types';
import { TabPanel as StyledTabPanel } from './styles';

import warning from '@wordpress/warning';
import { TabsContext } from './context';
import { useTabsContext } from './context';
import type { WordPressComponentProps } from '../context';

export const TabPanel = forwardRef<
HTMLDivElement,
WordPressComponentProps< TabPanelProps, 'div', false >
>( function TabPanel( { children, id, focusable = true, ...otherProps }, ref ) {
const context = useContext( TabsContext );
const context = useTabsContext();
if ( ! context ) {
warning( '`Tabs.TabPanel` must be wrapped in a `Tabs` component.' );
return null;
Expand Down

0 comments on commit bb8adcd

Please sign in to comment.