Skip to content
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

Merged
merged 6 commits into from
Jan 8, 2020
Merged

Conversation

ellatrix
Copy link
Member

@ellatrix ellatrix commented Jan 7, 2020

Description

See #18148.

This PR removes the <ColorDetector /> component in favour of passing a reference of the target element directly to useColors.

  • Removes an additional div element that is rendered for each paragraph.
  • Avoids needing to query the DOM for the target node.
  • Avoids the use of withFallbackStyles.
  • I also swapped out the detected color references for state, so the warnings are displayed immediately instead of delayed to the next rerender.

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:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR. .

@@ -56,8 +59,8 @@ function HeadingEdit( {
</InspectorControls>
{ InspectorControlsColorPanel }
<TextColor>
<ColorDetector querySelector='[contenteditable="true"]' />
Copy link
Member Author

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.

Copy link
Contributor

@epiqueras epiqueras left a 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(
Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@ellatrix ellatrix Jan 7, 2020

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?

Copy link
Contributor

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(
Copy link
Member Author

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.

@ellatrix
Copy link
Member Author

ellatrix commented Jan 7, 2020

@epiqueras @jorgefilipecosta I'm not sure if this correctly works in master. When I select a light colour (with light computed background), there's no contrast warning. When I select a black background colour (with a dark computed text colour), the contrast warning only shows on the next block update (e.g. typing some text).

@epiqueras
Copy link
Contributor

I'm not sure if this correctly works in master.

Something has regressed.

@ellatrix ellatrix requested a review from epiqueras January 7, 2020 15:50
@ellatrix ellatrix added the [Type] Code Quality Issues or PRs that relate to code quality label Jan 7, 2020
@epiqueras
Copy link
Contributor

@ellatrix Found it!

https://github.com/WordPress/gutenberg/pull/19046/files

backgroundColorNode.parentNode === Node.ELEMENT_NODE is always evaluating to false.

cc @jorgefilipecosta

@ellatrix ellatrix mentioned this pull request Jan 8, 2020
6 tasks
@ellatrix ellatrix added the [Type] Performance Related to performance efforts label Jan 8, 2020
@ellatrix ellatrix force-pushed the try/use-colors-ref branch from dbd22a5 to d764558 Compare January 8, 2020 09:53
@ellatrix
Copy link
Member Author

ellatrix commented Jan 8, 2020

@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.

Copy link
Contributor

@epiqueras epiqueras left a 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.

@ellatrix
Copy link
Member Author

ellatrix commented Jan 8, 2020

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?

@epiqueras
Copy link
Contributor

The goal is to refactor all blocks with colors to use this hook.

#18547 (comment)

@ellatrix
Copy link
Member Author

ellatrix commented Jan 8, 2020

@epiqueras Done in 606137a.

@ellatrix ellatrix requested a review from epiqueras January 8, 2020 14:54
@epiqueras
Copy link
Contributor

targetRef is not descriptive enough, hence why I suggested nesting the 3 new options under colorDetector.

@ellatrix
Copy link
Member Author

ellatrix commented Jan 8, 2020

@epiqueras Makes sense. Done now.

@ellatrix ellatrix merged commit 7c2b50c into master Jan 8, 2020
@ellatrix ellatrix deleted the try/use-colors-ref branch January 8, 2020 17:23
@ellatrix
Copy link
Member Author

ellatrix commented Jan 8, 2020

Thanks for the review!

@epiqueras
Copy link
Contributor

Thanks for the fixes!

@ellatrix ellatrix added this to the Gutenberg 7.3 milestone Jan 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Code Quality Issues or PRs that relate to code quality [Type] Performance Related to performance efforts
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants