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

Border panel: Update to display multiple palette origins #36753

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { every, isEmpty } from 'lodash';
* WordPress dependencies
*/
import { PanelBody, ColorIndicator } from '@wordpress/components';
import { sprintf, __, _x } from '@wordpress/i18n';
import { useMemo } from '@wordpress/element';
import { sprintf, __ } from '@wordpress/i18n';

/**
* Internal dependencies
Expand All @@ -18,6 +17,8 @@ import ColorGradientControl from './control';
import { getColorObjectByColorValue } from '../colors';
import { __experimentalGetGradientObjectByGradientValue } from '../gradients';
import useSetting from '../use-setting';
import useCommonSingleMultipleSelects from './use-common-single-multiple-selects';
import useMultipleOriginColorsAndGradients from './use-multiple-origin-colors-and-gradients';

// translators: first %s: The type of color or gradient (e.g. background, overlay...), second %s: the color name or value (e.g. red or #ff0000)
const colorIndicatorAriaLabel = __( '(%s: color %s)' );
Expand Down Expand Up @@ -152,13 +153,6 @@ export const PanelColorGradientSettingsInner = ( {
);
};

function useCommonSingleMultipleSelects() {
return {
disableCustomColors: ! useSetting( 'color.custom' ),
disableCustomGradients: ! useSetting( 'color.customGradient' ),
};
}

const PanelColorGradientSettingsSingleSelect = ( props ) => {
const colorGradientSettings = useCommonSingleMultipleSelects();
colorGradientSettings.colors = useSetting( 'color.palette' );
Expand All @@ -171,89 +165,7 @@ const PanelColorGradientSettingsSingleSelect = ( props ) => {
};

const PanelColorGradientSettingsMultipleSelect = ( props ) => {
const colorGradientSettings = useCommonSingleMultipleSelects();
const customColors = useSetting( 'color.palette.custom' );
const themeColors = useSetting( 'color.palette.theme' );
const defaultColors = useSetting( 'color.palette.default' );
const shouldDisplayDefaultColors = useSetting( 'color.defaultPalette' );

colorGradientSettings.colors = useMemo( () => {
const result = [];
if ( themeColors && themeColors.length ) {
result.push( {
name: _x(
'Theme',
'Indicates this palette comes from the theme.'
),
colors: themeColors,
} );
}
if (
shouldDisplayDefaultColors &&
defaultColors &&
defaultColors.length
) {
result.push( {
name: _x(
'Default',
'Indicates this palette comes from WordPress.'
),
colors: defaultColors,
} );
}
if ( customColors && customColors.length ) {
result.push( {
name: _x(
'Custom',
'Indicates this palette comes from the theme.'
),
colors: customColors,
} );
}
return result;
}, [ defaultColors, themeColors, customColors ] );

const customGradients = useSetting( 'color.gradients.custom' );
const themeGradients = useSetting( 'color.gradients.theme' );
const defaultGradients = useSetting( 'color.gradients.default' );
const shouldDisplayDefaultGradients = useSetting(
'color.defaultGradients'
);
colorGradientSettings.gradients = useMemo( () => {
const result = [];
if ( themeGradients && themeGradients.length ) {
result.push( {
name: _x(
'Theme',
'Indicates this palette comes from the theme.'
),
gradients: themeGradients,
} );
}
if (
shouldDisplayDefaultGradients &&
defaultGradients &&
defaultGradients.length
) {
result.push( {
name: _x(
'Default',
'Indicates this palette comes from WordPress.'
),
gradients: defaultGradients,
} );
}
if ( customGradients && customGradients.length ) {
result.push( {
name: _x(
'Custom',
'Indicates this palette is created by the user.'
),
gradients: customGradients,
} );
}
return result;
}, [ customGradients, themeGradients, defaultGradients ] );
const colorGradientSettings = useMultipleOriginColorsAndGradients();
return (
<PanelColorGradientSettingsInner
{ ...{ ...colorGradientSettings, ...props } }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Internal dependencies
*/
import useSetting from '../use-setting';

export default function useCommonSingleMultipleSelects() {
return {
disableCustomColors: ! useSetting( 'color.custom' ),
disableCustomGradients: ! useSetting( 'color.customGradient' ),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { _x } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import useSetting from '../use-setting';
import useCommonSingleMultipleSelects from './use-common-single-multiple-selects';

/**
* Retrieves color and gradient related settings.
*
* The arrays for colors and gradients are made up of color palettes from each
* origin i.e. "Core", "Theme", and "User".
*
* @return {Object} Color and gradient related settings.
*/
export default function useMultipleOriginColorsAndGradients() {
const colorGradientSettings = useCommonSingleMultipleSelects();
const customColors = useSetting( 'color.palette.custom' );
const themeColors = useSetting( 'color.palette.theme' );
const defaultColors = useSetting( 'color.palette.default' );
const shouldDisplayDefaultColors = useSetting( 'color.defaultPalette' );

colorGradientSettings.colors = useMemo( () => {
const result = [];
if ( themeColors && themeColors.length ) {
result.push( {
name: _x(
'Theme',
'Indicates this palette comes from the theme.'
),
colors: themeColors,
} );
}
if (
shouldDisplayDefaultColors &&
defaultColors &&
defaultColors.length
) {
result.push( {
name: _x(
'Default',
'Indicates this palette comes from WordPress.'
),
colors: defaultColors,
} );
}
if ( customColors && customColors.length ) {
result.push( {
name: _x(
'Custom',
'Indicates this palette comes from the theme.'
),
colors: customColors,
} );
}
return result;
}, [ defaultColors, themeColors, customColors ] );

const customGradients = useSetting( 'color.gradients.custom' );
const themeGradients = useSetting( 'color.gradients.theme' );
const defaultGradients = useSetting( 'color.gradients.default' );
const shouldDisplayDefaultGradients = useSetting(
'color.defaultGradients'
);
colorGradientSettings.gradients = useMemo( () => {
const result = [];
if ( themeGradients && themeGradients.length ) {
result.push( {
name: _x(
'Theme',
'Indicates this palette comes from the theme.'
),
gradients: themeGradients,
} );
}
if (
shouldDisplayDefaultGradients &&
defaultGradients &&
defaultGradients.length
) {
result.push( {
name: _x(
'Default',
'Indicates this palette comes from WordPress.'
),
gradients: defaultGradients,
} );
}
if ( customGradients && customGradients.length ) {
result.push( {
name: _x(
'Custom',
'Indicates this palette is created by the user.'
),
gradients: customGradients,
} );
}
return result;
}, [ customGradients, themeGradients, defaultGradients ] );

return colorGradientSettings;
}
23 changes: 14 additions & 9 deletions packages/block-editor/src/hooks/border-color.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useState } from '@wordpress/element';
* Internal dependencies
*/
import ColorGradientControl from '../components/colors-gradients/control';
import useMultipleOriginColorsAndGradients from '../components/colors-gradients/use-multiple-origin-colors-and-gradients';
import {
getColorClassName,
getColorObjectByColorValue,
Expand Down Expand Up @@ -46,13 +47,15 @@ export function BorderColorEdit( props ) {
attributes: { borderColor, style },
setAttributes,
} = props;
const colors = useSetting( 'color.palette' ) || EMPTY_ARRAY;
const disableCustomColors = ! useSetting( 'color.custom' );
const disableCustomGradients = ! useSetting( 'color.customGradient' );
const colorGradientSettings = useMultipleOriginColorsAndGradients();
const availableColors = colorGradientSettings.colors.reduce(
( colors, origin ) => colors.concat( origin.colors ),
[]
);
const [ colorValue, setColorValue ] = useState(
() =>
getColorObjectByAttributeValues(
colors,
availableColors,
borderColor,
style?.border?.color
)?.color
Expand All @@ -61,7 +64,10 @@ export function BorderColorEdit( props ) {
const onChangeColor = ( value ) => {
setColorValue( value );

const colorObject = getColorObjectByColorValue( colors, value );
const colorObject = getColorObjectByColorValue(
availableColors,
value
);
const newStyle = {
...style,
border: {
Expand All @@ -83,11 +89,10 @@ export function BorderColorEdit( props ) {
<ColorGradientControl
label={ __( 'Color' ) }
colorValue={ colorValue }
colors={ colors }
gradients={ undefined }
disableCustomColors={ disableCustomColors }
disableCustomGradients={ disableCustomGradients }
onColorChange={ onChangeColor }
clearable={ false }
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this clearable prop was only added to #33743 because the control could be reset via the ToolsPanel menu. Given this PR is for before that lands perhaps it should be omitted here.

It's only a small thing. I don't feel strongly either way.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't feel strongly about it either. For consistency with the color panel, it'd make sense to omit it? In manual testing, you can select a theme or default color and then deselect it by clicking the circle to effectively clear the color setting, so I don't think it's a big issue if we leave it in, though.

__experimentalHasMultipleOrigins
{ ...colorGradientSettings }
/>
);
}
Expand Down