Skip to content

Commit

Permalink
Components: replace TabPanel with Tabs in the editor's `ColorPane…
Browse files Browse the repository at this point in the history
…l` (#56878)

* replace `TabPanel` with `Tabs`

* implement initial tab to track current value

* focusable false

* defer unlock call
  • Loading branch information
chad1008 authored Dec 8, 2023
1 parent d87092c commit 11862cf
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions packages/block-editor/src/components/global-styles/color-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import {
__experimentalHStack as HStack,
__experimentalZStack as ZStack,
__experimentalDropdownContentWrapper as DropdownContentWrapper,
TabPanel,
ColorIndicator,
Flex,
FlexItem,
Dropdown,
Button,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { useCallback } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
Expand All @@ -29,6 +29,7 @@ import ColorGradientControl from '../colors-gradients/control';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
import { setImmutably } from '../../utils/object';
import { unlock } from '../../lock-unlock';

export function useHasColorPanel( settings ) {
const hasTextPanel = useHasTextPanel( settings );
Expand Down Expand Up @@ -203,12 +204,11 @@ function ColorPanelDropdown( {
colorGradientControlSettings,
panelId,
} ) {
const tabConfigs = tabs.map( ( { key, label: tabLabel } ) => {
return {
name: key,
title: tabLabel,
};
} );
const currentTab = tabs.find( ( tab ) => tab.userValue !== undefined );
// Unlocking `Tabs` too early causes the `unlock` method to receive an empty
// object, due to circular dependencies.
// See https://github.com/WordPress/gutenberg/issues/52692
const { Tabs } = unlock( componentsPrivateApis );

return (
<ToolsPanelItem
Expand Down Expand Up @@ -258,26 +258,35 @@ function ColorPanelDropdown( {
/>
) }
{ tabs.length > 1 && (
<TabPanel tabs={ tabConfigs }>
{ ( tab ) => {
const selectedTab = tabs.find(
( t ) => t.key === tab.name
);

if ( ! selectedTab ) {
return null;
}

<Tabs initialTabId={ currentTab?.key }>
<Tabs.TabList>
{ tabs.map( ( tab ) => (
<Tabs.Tab
key={ tab.key }
id={ tab.key }
>
{ tab.label }
</Tabs.Tab>
) ) }
</Tabs.TabList>

{ tabs.map( ( tab ) => {
return (
<ColorPanelTab
{ ...selectedTab }
colorGradientControlSettings={
colorGradientControlSettings
}
/>
<Tabs.TabPanel
key={ tab.key }
id={ tab.key }
focusable={ false }
>
<ColorPanelTab
{ ...tab }
colorGradientControlSettings={
colorGradientControlSettings
}
/>
</Tabs.TabPanel>
);
} }
</TabPanel>
} ) }
</Tabs>
) }
</div>
</DropdownContentWrapper>
Expand Down

1 comment on commit 11862cf

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 11862cf.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/7142596930
📝 Reported issues:

Please sign in to comment.