Skip to content

Commit

Permalink
[Mobile] - Fix regression with the Color hook and ColorPanel (#49917)
Browse files Browse the repository at this point in the history
* Mobile - Update ColorPanel: Move it to the global styles folder and update code to latest changes from the color hook

* Mobile - ColorSettings Palette: Avoid resetting color/gradient changes since this is now being handled by the ColorPanel component

* Mobile - CustomGradientPicker - Update code to get the correct gradient data due to the latest color hook changes

* Mobile - ColorPalette - Add accessibilityLabel with the color value

* Mobile - Segmented Control - Add optional chaining

* Mobile - Paragraph block - Add tests for color customization

* Mobile - Heading block - Add tests for color customization

* Mobile - Buttons block - Add tests for color customization

* Mobile - Integration tests - Add getGlobalStyles - To pass theme data to the editor for tests

* Mobile - Paragraph block - Test setting a theme text color and for the contrast check

* Mobile - ColorPalette - Use color name as the accessibility label

* Mobile - CustomGradientPicker - Simplify code by destructuring gradientAST when calling getGradientAstWithDefault

* Mobile - ColorPalette - Add optional chaining for allColors and gradients, in some cases these values can be undefined, depending on the theme colors/configuration. It also adds a fallback name for colors that don't have a name value
  • Loading branch information
Gerardo Pacheco authored Apr 20, 2023
1 parent a1a8729 commit 13a093a
Show file tree
Hide file tree
Showing 13 changed files with 784 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useEffect, useState, useMemo, useCallback } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { useGlobalStyles } from '@wordpress/components';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import PanelColorGradientSettings from '../colors-gradients/panel-color-gradient-settings';
import { useColorsPerOrigin, useGradientsPerOrigin } from './hooks';
import { getValueFromVariable } from './utils';
import { immutableSet } from '../../utils/object';
import ContrastChecker from '../contrast-checker';
import InspectorControls from '../inspector-controls';
import {
useHasColorPanel,
useHasTextPanel,
useHasBackgroundPanel,
} from './color-panel.js';

const ColorPanel = ( {
value,
inheritedValue = value,
onChange,
settings,
} ) => {
const colors = useColorsPerOrigin( settings );
const gradients = useGradientsPerOrigin( settings );
const globalStyles = useGlobalStyles();

const [ detectedBackgroundColor, setDetectedBackgroundColor ] = useState();
const [ detectedTextColor, setDetectedTextColor ] = useState();

const { baseGlobalStyles } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
return {
baseGlobalStyles:
getSettings()?.__experimentalGlobalStylesBaseStyles?.color,
};
} );

const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue );
const encodeColorValue = useCallback(
( colorValue ) => {
const allColors = colors.flatMap(
( { colors: originColors } ) => originColors
);
const colorObject = allColors.find(
( { color } ) => color === colorValue
);
return colorObject
? 'var:preset|color|' + colorObject.slug
: colorValue;
},
[ colors ]
);
const encodeGradientValue = useCallback(
( gradientValue ) => {
const allGradients = gradients.flatMap(
( { gradients: originGradients } ) => originGradients
);
const gradientObject = allGradients.find(
( { gradient } ) => gradient === gradientValue
);
return gradientObject
? 'var:preset|gradient|' + gradientObject.slug
: gradientValue;
},
[ gradients ]
);

// Text Color
const showTextPanel = useHasTextPanel( settings );
const textColor = decodeValue( inheritedValue?.color?.text );
const setTextColor = useCallback(
( newColor ) => {
onChange(
immutableSet(
value,
[ 'color', 'text' ],
encodeColorValue( newColor )
)
);
},
[ encodeColorValue, onChange, value ]
);
const resetTextColor = useCallback(
() => setTextColor( undefined ),
[ setTextColor ]
);

// BackgroundColor
const showBackgroundPanel = useHasBackgroundPanel( settings );
const backgroundColor = decodeValue( inheritedValue?.color?.background );
const gradient = decodeValue( inheritedValue?.color?.gradient );
const setBackgroundColor = useCallback(
( newColor ) => {
const newValue = immutableSet(
value,
[ 'color', 'background' ],
encodeColorValue( newColor )
);
newValue.color.gradient = undefined;
onChange( newValue );
},
[ encodeColorValue, onChange, value ]
);
const setGradient = useCallback(
( newGradient ) => {
const newValue = immutableSet(
value,
[ 'color', 'gradient' ],
encodeGradientValue( newGradient )
);
newValue.color.background = undefined;
onChange( newValue );
},
[ encodeGradientValue, onChange, value ]
);
const resetBackground = useCallback( () => {
const newValue = immutableSet(
value,
[ 'color', 'background' ],
undefined
);
newValue.color.gradient = undefined;
onChange( newValue );
}, [ onChange, value ] );
const currentGradients = settings?.color?.gradients;
const withoutGradientsSupport =
Array.isArray( currentGradients ) && currentGradients.length === 0;

const items = useMemo(
() =>
[
showTextPanel && {
label: __( 'Text' ),
colorValue: textColor,
onColorChange: setTextColor,
onColorCleared: resetTextColor,
},
showBackgroundPanel && {
label: __( 'Background' ),
colorValue: backgroundColor,
onColorChange: setBackgroundColor,
onColorCleared: resetBackground,
onGradientChange: ! withoutGradientsSupport
? setGradient
: undefined,
gradientValue: gradient,
},
].filter( Boolean ),
[
backgroundColor,
gradient,
resetBackground,
resetTextColor,
setBackgroundColor,
setGradient,
setTextColor,
showBackgroundPanel,
showTextPanel,
textColor,
withoutGradientsSupport,
]
);

useEffect( () => {
// The following logic is used to determine current text/background colors:
// 1. The globalStyles object is queried to determine whether a color has been
// set via a block's settings.
// 2. If a block-based theme is in use and no globalStyles exist, the theme's
// default/base colors are used.
// 3. If no globalStyles exist and a theme isn't block-based, there is no way
// to determine the default text/background color and the checker won't run.
const currentDetectedTextColor =
globalStyles?.color || baseGlobalStyles?.text;
const currentDetectedBackgroundColor =
globalStyles?.backgroundColor || baseGlobalStyles?.background;

setDetectedTextColor( currentDetectedTextColor );
setDetectedBackgroundColor( currentDetectedBackgroundColor );
}, [ globalStyles, baseGlobalStyles ] );

return (
<InspectorControls>
<PanelColorGradientSettings
title={ __( 'Color' ) }
initialOpen={ false }
settings={ items }
>
<ContrastChecker
backgroundColor={ detectedBackgroundColor }
textColor={ detectedTextColor }
/>
</PanelColorGradientSettings>
</InspectorControls>
);
};

export { useHasColorPanel };
export default ColorPanel;
63 changes: 0 additions & 63 deletions packages/block-editor/src/hooks/color-panel.native.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Buttons block color customization sets a background color 1`] = `
"<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"backgroundColor":"luminous-vivid-amber"} /--></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons block color customization sets a gradient background color 1`] = `
"<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"gradient":"light-green-cyan-to-vivid-green-cyan"} /--></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons block color customization sets a text color 1`] = `
"<!-- wp:buttons -->
<div class="wp-block-buttons"><!-- wp:button {"textColor":"pale-pink"} /--></div>
<!-- /wp:buttons -->"
`;

exports[`Buttons block justify content sets Justify items center option 1`] = `
"<!-- wp:buttons {"layout":{"type":"flex","justifyContent":"center"}} -->
<div class="wp-block-buttons"><!-- wp:button /--></div>
Expand Down
Loading

0 comments on commit 13a093a

Please sign in to comment.