-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
useColors: directly pass ref for color detecting #19474
Conversation
@@ -56,8 +59,8 @@ function HeadingEdit( { | |||
</InspectorControls> | |||
{ InspectorControlsColorPanel } | |||
<TextColor> | |||
<ColorDetector querySelector='[contenteditable="true"]' /> |
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 component should no longer be needed as the colours can be calculated in an effect.
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.
What's the best way to test the color detecting feature?
The contrast checker warnings for blocks with inherited colors.
@@ -219,26 +221,10 @@ export default function __experimentalUseColors( | |||
return ( | |||
( needsBackgroundColor || needsColor ) && | |||
withFallbackStyles( |
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 won't work, withFallbackStyles
returns a component and calls the passed-in function with its root node's ref upon mounting.
Now we can query the new target ref here.
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 know, I was still figuring out that part. Rewrote it now. Looks like we need to use state to set the colours so the component rerenders?
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.
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 meant for <ColorPanel>
to rerender. How does it know when detected colours changed if it's a ref?
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.
The detected color can't change without you doing something that updates the component and, therefore, the color panel.
There were some edge cases, but we decided they were reasonable.
@@ -216,48 +217,27 @@ export default function __experimentalUseColors( | |||
break; | |||
} | |||
} | |||
return ( | |||
( needsBackgroundColor || needsColor ) && | |||
withFallbackStyles( |
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 don't think withFallbackStyles
is need anymore since we have direct access to the element.
@epiqueras @jorgefilipecosta I'm not sure if this correctly works in |
Something has regressed. |
@ellatrix Found it! https://github.com/WordPress/gutenberg/pull/19046/files
|
dbd22a5
to
d764558
Compare
@epiqueras Fixed in in #19500. This PR is now ready. I also swapped out the references for state so the warnings are displayed instantly instead of being delayed to a next rerender. |
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.
We need the following option instead of just targetRef
:
colorDetector: {
targetRef,
backgroundColorTargetRef = targetRef,
textColorTargetRef = targetRef,
} = {},
There are cases where the text won't be a child of the element setting a background color.
Could you point me to these cases? I only see paragraph and heading using this hook. Or do you have use cases outside core? |
The goal is to refactor all blocks with colors to use this hook. |
@epiqueras Done in 606137a. |
|
@epiqueras Makes sense. Done now. |
Thanks for the review! |
Thanks for the fixes! |
Description
See #18148.
This PR removes the
<ColorDetector />
component in favour of passing a reference of the target element directly touseColors
.div
element that is rendered for each paragraph.withFallbackStyles
.How has this been tested?
Trigger the contrast warning on the paragraph and heading block by selecting a text or background color together with inherited colours (detected).
Screenshots
Types of changes
Checklist: