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

Color Block Support: Switch to ToolsPanel for displaying UI #34027

Merged
merged 2 commits into from Feb 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -67,6 +67,10 @@ const BlockInspector = ( { showNoBlockSelectedMessage = true } ) => {
<div className="block-editor-block-inspector">
<MultiSelectionInspector />
<InspectorControls.Slot />
<InspectorControls.Slot
__experimentalGroup="color"
label={ __( 'Color' ) }
/>
<InspectorControls.Slot
__experimentalGroup="typography"
label={ __( 'Typography' ) }
Expand Down Expand Up @@ -139,6 +143,11 @@ const BlockInspectorSingleBlock = ( {
</div>
) }
<InspectorControls.Slot />
<InspectorControls.Slot
__experimentalGroup="color"
label={ __( 'Color' ) }
className="color-block-support-panel__inner-wrapper"
Copy link
Contributor

Choose a reason for hiding this comment

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

This class doesn't seem to follow our naming guidelines. We should start with the package name followed by the component name and a modified, so something like block-editor-block-inspector__color-slot

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can create a PR to tidy this up.

/>
<InspectorControls.Slot
__experimentalGroup="typography"
label={ __( 'Typography' ) }
Expand Down
@@ -0,0 +1,85 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import {
Button,
ColorIndicator,
Dropdown,
FlexItem,
__experimentalHStack as HStack,
__experimentalToolsPanelItem as ToolsPanelItem,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import ColorGradientControl from './control';
import useMultipleOriginColorsAndGradients from './use-multiple-origin-colors-and-gradients';

export default function ToolsPanelColorDropdown( {
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
settings,
enableAlpha,
...otherProps
} ) {
const colorGradientSettings = useMultipleOriginColorsAndGradients();
const controlSettings = {
...colorGradientSettings,
clearable: false,
enableAlpha,
label: settings.label,
onColorChange: settings.onColorChange,
onGradientChange: settings.onGradientChange,
colorValue: settings.colorValue,
gradientValue: settings.gradientValue,
};
const selectedColor = settings.gradientValue ?? settings.colorValue;

return (
<ToolsPanelItem
hasValue={ settings.hasValue }
label={ settings.label }
onDeselect={ settings.onDeselect }
isShownByDefault={ settings.isShownByDefault }
resetAllFilter={ settings.resetAllFilter }
{ ...otherProps }
className="block-editor-tools-panel-color-gradient-settings__item"
>
<Dropdown
className="block-editor-tools-panel-color-dropdown"
contentClassName="block-editor-panel-color-gradient-settings__dropdown-content"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is reusing a classname from another component potentially creating style conflicts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. To maintain the visual consistency, are you okay with us duplicating all these styles?

renderToggle={ ( { isOpen, onToggle } ) => (
<Button
onClick={ onToggle }
aria-expanded={ isOpen }
className={ classnames(
'block-editor-panel-color-gradient-settings__dropdown',
Copy link
Contributor

Choose a reason for hiding this comment

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

This is reusing a classname from another component potentially creating style conflicts.

{ 'is-open': isOpen }
) }
>
<HStack justify="flex-start">
<ColorIndicator
className="block-editor-panel-color-gradient-settings__color-indicator"
colorValue={ selectedColor }
/>
<FlexItem>{ settings.label }</FlexItem>
</HStack>
</Button>
) }
renderContent={ () => (
<ColorGradientControl
showTitle={ false }
__experimentalHasMultipleOrigins
__experimentalIsRenderedInSidebar
enableAlpha
{ ...controlSettings }
/>
) }
/>
</ToolsPanelItem>
);
}
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.js
Expand Up @@ -51,6 +51,7 @@ export { default as __experimentalTextTransformControl } from './text-transform-
export { default as __experimentalColorGradientControl } from './colors-gradients/control';
export { default as __experimentalColorGradientSettingsDropdown } from './colors-gradients/dropdown';
export { default as __experimentalPanelColorGradientSettings } from './colors-gradients/panel-color-gradient-settings';
export { default as __experimentalToolsPanelColorDropdown } from './colors-gradients/tools-panel-color-dropdown';
export {
default as __experimentalImageEditor,
ImageEditingProvider as __experimentalImageEditingProvider,
Expand Down
Expand Up @@ -70,6 +70,8 @@ export default function BlockSupportToolsPanel( { children, group, label } ) {
panelId={ panelId }
hasInnerWrapper={ true }
shouldRenderPlaceholderItems={ true } // Required to maintain fills ordering.
__experimentalFirstVisibleItemClass="first"
__experimentalLastVisibleItemClass="last"
>
{ children }
</ToolsPanel>
Expand Down
Expand Up @@ -6,6 +6,7 @@ import { createSlotFill } from '@wordpress/components';
const InspectorControlsDefault = createSlotFill( 'InspectorControls' );
const InspectorControlsAdvanced = createSlotFill( 'InspectorAdvancedControls' );
const InspectorControlsBorder = createSlotFill( 'InspectorControlsBorder' );
const InspectorControlsColor = createSlotFill( 'InspectorControlsColor' );
const InspectorControlsDimensions = createSlotFill(
'InspectorControlsDimensions'
);
Expand All @@ -17,6 +18,7 @@ const groups = {
default: InspectorControlsDefault,
advanced: InspectorControlsAdvanced,
border: InspectorControlsBorder,
color: InspectorControlsColor,
dimensions: InspectorControlsDimensions,
typography: InspectorControlsTypography,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/border-color.js
Expand Up @@ -68,7 +68,7 @@ export function BorderColorEdit( props ) {

// Detect changes in the color attributes and update the colorValue to keep the
// UI in sync. This is necessary for situations when border controls interact with
// eachother: eg, setting the border width to zero causes the color and style
// each other: eg, setting the border width to zero causes the color and style
// selections to be cleared.
useEffect( () => {
setColorValue(
Expand Down
40 changes: 18 additions & 22 deletions packages/block-editor/src/hooks/color-panel.js
Expand Up @@ -2,13 +2,12 @@
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import PanelColorGradientSettings from '../components/colors-gradients/panel-color-gradient-settings';
import ContrastChecker from '../components/contrast-checker';
import ToolsPanelColorDropdown from '../components/colors-gradients/tools-panel-color-dropdown';
import InspectorControls from '../components/inspector-controls';
import { __unstableUseBlockRef as useBlockRef } from '../components/block-list/use-block-props/use-block-refs';

Expand All @@ -21,7 +20,6 @@ export default function ColorPanel( {
settings,
clientId,
enableContrastChecking = true,
showTitle = true,
} ) {
const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState();
const [ detectedColor, setDetectedColor ] = useState();
Expand Down Expand Up @@ -61,25 +59,23 @@ export default function ColorPanel( {
} );

return (
<InspectorControls>
<PanelColorGradientSettings
title={ __( 'Color' ) }
initialOpen={ false }
settings={ settings }
showTitle={ showTitle }
enableAlpha={ enableAlpha }
__experimentalHasMultipleOrigins
__experimentalIsRenderedInSidebar
>
{ enableContrastChecking && (
<ContrastChecker
backgroundColor={ detectedBackgroundColor }
textColor={ detectedColor }
enableAlphaChecker={ enableAlpha }
linkColor={ detectedLinkColor }
/>
) }
</PanelColorGradientSettings>
<InspectorControls __experimentalGroup="color">
{ settings.map( ( setting, index ) => (
<ToolsPanelColorDropdown
key={ index }
settings={ setting }
panelId={ clientId }
enableAlpha={ enableAlpha }
/>
) ) }
{ enableContrastChecking && (
<ContrastChecker
backgroundColor={ detectedBackgroundColor }
textColor={ detectedColor }
enableAlphaChecker={ enableAlpha }
linkColor={ detectedLinkColor }
/>
) }
</InspectorControls>
);
}