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 6 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
10 changes: 6 additions & 4 deletions packages/components/src/tabs/tab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@
* 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` needs to receive context from a `Tabs` component or a `Tabs.Context.Provider`.'
);
chad1008 marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
const { store, instanceId } = context;
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/tabs/tablist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ export const TabList = forwardRef<
>( function TabList( { children, ...otherProps }, ref ) {
const context = useTabsContext();
if ( ! context ) {
warning( '`Tabs.TabList` must be wrapped in a `Tabs` component.' );
warning(
'`Tabs.TabList` needs to receive context from a `Tabs` component or a `Tabs.Context.Provider`.'
);
return null;
}
const { store } = context;
Expand Down
10 changes: 6 additions & 4 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,16 +15,18 @@ 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.' );
warning(
'`Tabs.TabPanel` needs to receive context from a `Tabs` component or a `Tabs.Context.Provider`.'
);
return null;
}
const { store, instanceId } = context;
Expand Down
Loading