Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tabs: cleanup and improvements #56224

Merged
merged 7 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading