-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Mobile] - Fix regression with the Color hook and ColorPanel (#49917)
* 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
Showing
13 changed files
with
784 additions
and
79 deletions.
There are no files selected for viewing
207 changes: 207 additions & 0 deletions
207
packages/block-editor/src/components/global-styles/color-panel.native.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
18 changes: 18 additions & 0 deletions
18
packages/block-library/src/buttons/test/__snapshots__/edit.native.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.