Skip to content

Commit

Permalink
Global Styles: Fix display of color palettes for border panel (#38798)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw authored Mar 18, 2022
1 parent a42bba4 commit 1f07e88
Showing 1 changed file with 32 additions and 26 deletions.
58 changes: 32 additions & 26 deletions packages/edit-site/src/components/global-styles/border-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
__experimentalBorderRadiusControl as BorderRadiusControl,
__experimentalBorderStyleControl as BorderStyleControl,
__experimentalColorGradientControl as ColorGradientControl,
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
} from '@wordpress/block-editor';
import {
__experimentalToolsPanel as ToolsPanel,
Expand All @@ -17,14 +17,15 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { getSupportedGlobalStylesPanels, useSetting, useStyle } from './hooks';
import {
getSupportedGlobalStylesPanels,
useColorsPerOrigin,
useSetting,
useStyle,
} from './hooks';

const MIN_BORDER_WIDTH = 0;

// Defining empty array here instead of inline avoids unnecessary re-renders of
// color control.
const EMPTY_ARRAY = [];

export function useHasBorderPanel( name ) {
const controls = [
useHasBorderColorControl( name ),
Expand Down Expand Up @@ -100,13 +101,32 @@ export default function BorderPanel( { name } ) {
const showBorderStyle = useHasBorderStyleControl( name );
const [ borderStyle, setBorderStyle ] = useStyle( 'border.style', name );

// When we set a border color or width ensure we have a style so the user
// can see a visible border.
const handleOnChangeWithStyle = ( setStyle ) => ( value ) => {
if ( !! value && ! borderStyle ) {
setBorderStyle( 'solid' );
}

setStyle( value || undefined );
};

// Border color.
const showBorderColor = useHasBorderColorControl( name );
const [ borderColor, setBorderColor ] = useStyle( 'border.color', name );
const [ colors = EMPTY_ARRAY ] = useSetting( 'color.palette' );
const disableCustomColors = ! useSetting( 'color.custom' )[ 0 ];
const disableCustomGradients = ! useSetting( 'color.customGradient' )[ 0 ];

const borderColorSettings = [
{
label: __( 'Color' ),
colors: useColorsPerOrigin( name ),
colorValue: borderColor,
onColorChange: handleOnChangeWithStyle( setBorderColor ),
clearable: false,
},
];

// Border radius.
const showBorderRadius = useHasBorderRadiusControl( name );
const [ borderRadiusValues, setBorderRadius ] = useStyle(
Expand All @@ -128,16 +148,6 @@ export default function BorderPanel( { name } ) {
setBorderWidth( undefined );
};

// When we set a border color or width ensure we have a style so the user
// can see a visible border.
const handleOnChangeWithStyle = ( setStyle ) => ( value ) => {
if ( !! value && ! borderStyle ) {
setBorderStyle( 'solid' );
}

setStyle( value || undefined );
};

return (
<ToolsPanel label={ __( 'Border' ) } resetAll={ resetAll }>
{ showBorderWidth && (
Expand Down Expand Up @@ -178,17 +188,13 @@ export default function BorderPanel( { name } ) {
onDeselect={ createResetCallback( setBorderColor ) }
isShownByDefault={ true }
>
<ColorGradientControl
label={ __( 'Color' ) }
colorValue={ borderColor }
colors={ colors }
gradients={ undefined }
<ColorGradientSettingsDropdown
__experimentalHasMultipleOrigins
__experimentalIsRenderedInSidebar
disableCustomColors={ disableCustomColors }
disableCustomGradients={ disableCustomGradients }
onColorChange={ handleOnChangeWithStyle(
setBorderColor
) }
clearable={ false }
enableAlpha
settings={ borderColorSettings }
/>
</ToolsPanelItem>
) }
Expand Down

0 comments on commit 1f07e88

Please sign in to comment.