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

Global Styles: Fix display of color palettes for border panel #38798

Merged
merged 2 commits into from
Mar 18, 2022
Merged
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
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