-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
ColorPalette
: Use computed color when css variable is passed to ColorPicker
#47181
Conversation
ColorPalette
: Use computed color when css variable is passed to ColorPicker
Flaky tests detected in 8302689. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3930044210
|
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.
Ooh, thank you for the fix!
I confirmed this is working in Chrome/Firefox/Safari, and it should stay that way given that the spec says it returns "resolved values" 🤞
Can we also fix the other issue (#41459) in this PR or are you planning to do it separately?
gutenberg/packages/components/src/color-palette/index.tsx
Lines 313 to 324 in 8e299b5
style={ | |
showTransparentBackground( value ) | |
? { color: '#000' } | |
: { | |
background: value, | |
color: | |
colordColor.contrast() > | |
colordColor.contrast( '#000' ) | |
? '#fff' | |
: '#000', | |
} | |
} |
const getComputedBackgroundColorValueFromRef = ( | ||
value: string | undefined, | ||
ref: RefObject< HTMLElement > | null | ||
) => { | ||
let computedValue = value; |
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.
Nit: The naming is kind of misleading to me. It only returns a "computed value from ref" when the value is a CSS var. Maybe something like normalizeColorValue()
or getValueForColord()
? And we can probably remove the let computedValue
if we do an early return like:
if ( ! currentValueIsCssVariable ) {
return value;
}
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.
I have changed the function name.
Also, I think I could have simplified the function content without using let
👍
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.
Awesome, I like how these small changes improve readability 😊
8302689
to
a07c06f
Compare
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.
🚢
Thank you @t-hamano for working on this 🙏 it looks like a little improvement but it fixes a really annoying bug! |
Closes #46492
What?
This PR passes the computed color to the
ColorPicker
component if the value of theColorPalette
component is a CSS variable. This will ensure that the correct color value is set in the input field when the custom color picker is opened.Why?
The
ColorPicker
component doesn't know what actual color the CSS variable points to. Therefore, the color input field of theColorPicker
component displays#000000
as a fallback.How?
I used the following approach to pass the computed value to the
ColorPicker
component:ref
in the custom color picker area and reference its DOM node.getComputedBackgroundColorValueFromRef()
function to read the background-color from that DOM node. Therefore,ColorPalette
itself doesn't directly reference CSS variables; it expects CSS variables to be correctly defined by the consumer or theme. I also created this function because it may help resolve the following issue:ColorPalette
: reviewcolord
support when using CSS variables #44904Testing Instructions
In the block editor
Enable Empty Theme and update theme.json as follows:
/test/emptytheme/theme.json
The definition of the
settings.custom
property creates the following CSS variable, which is referenced as the value of thesettings.color.palette
:In the storybook
Screenshots or screencast
2b2ac6156dbe07a7e5974fcbedb596a8.mp4