-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[Mobile] - Fix regression with the Color hook and ColorPanel #49917
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
98dee61
Mobile - Update ColorPanel: Move it to the global styles folder and u…
7c6a8e4
Mobile - ColorSettings Palette: Avoid resetting color/gradient change…
7473e6d
Mobile - CustomGradientPicker - Update code to get the correct gradie…
d3326ef
Mobile - ColorPalette - Add accessibilityLabel with the color value
e890b42
Mobile - Segmented Control - Add optional chaining
d74891e
Mobile - Paragraph block - Add tests for color customization
5200b43
Mobile - Heading block - Add tests for color customization
29cad05
Mobile - Buttons block - Add tests for color customization
fec3766
Mobile - Integration tests - Add getGlobalStyles - To pass theme data…
d034df2
Mobile - Paragraph block - Test setting a theme text color and for th…
6e8f955
Mobile - ColorPalette - Use color name as the accessibility label
4a7a4c0
Mobile - CustomGradientPicker - Simplify code by destructuring gradie…
3994388
Mobile - ColorPalette - Add optional chaining for allColors and gradi…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.