-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Gradient: Fix clearing a custom gradient from throwing a React warning #37051
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
Gradient: Fix clearing a custom gradient from throwing a React warning #37051
Conversation
…icit undefined color styles to blockProps
d423a9a to
9219762
Compare
|
Size Change: +7 B (0%) Total Size: 1.1 MB
ℹ️ View Unchanged
|
| const extraStyles = {}; | ||
|
|
||
| if ( textColor ) { | ||
| extraStyles.color = getColorObjectByAttributeValues( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good fix.
However, inspecting getColorObjectByAttributeValues there's a path in which the resulting color value still can be undefined: when passing a textColor that is not present in the list of colors. This can happen upon switching themes, for example. Specific steps:
- Use TwentyTwentyTwo.
- Add a paragraph and select a preset text color from the theme.
- Switch to EmptyTheme.
At this point, the text color is undefined. I wonder if getColorObjectByAttributeValues should return null instead of undefined for such a case.
Though at the moment this is not reproducible because we have a bug in trunk by which the preset colors are serialized as inline styles and not as classes. Going to follow up #36770
|
The failing test ( |
| </BlockEditorProvider> | ||
| ); | ||
|
|
||
| expect( getStyleObj ).toHaveBeenLastCalledWith( { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is failing on trunk right now. I'm looking at it, but if you have quick thoughts, please share to fix them 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks Nik! I think this one might potentially be resolved as of #37108, which has just been merged in. I'll keep an eye on trunk just in case, and happy to follow-up if there are more issues.
Description
Fixes #36899
This PR updates the
withColorPaletteStylesfunction in the Color block support to not return emptycolorandbackgroundColorkeys. This fixes a React warning that gets thrown when clearing a custom gradient used on a block's background color. To provide extra context:The color gradient gets stored using the
backgroundshorthand. When we clear a gradient, thebackgroundstyle gets cleared, however inwithColorPaletteStylesthebackgroundColoris also being set toundefined. Unfortunately for us, React treats this undefined value as us updating the style for the component, even though it has no visual effect. We receive the following warning in the console:By explicitly ensuring that we don't set the
backgroundColorproperty, we avoid throwing the React warning. Note: this fix is preferable to updating how we store the gradient value, so that we don't have to change how the gradient is saved in post content.How has this been tested?
Screenshots
Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist:
*.native.jsfiles for terms that need renaming or removal).