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

Use Colors: Add text color detection support. #18547

Merged
merged 4 commits into from
Dec 2, 2019

Conversation

epiqueras
Copy link
Contributor

Follows #18237

Description

This PR adds support for detecting text colors using the true option introduced for useColors contrast checking by #18237.

How has this been tested?

All uses of useColors were verified to continue to work as expected.

Types of Changes

New Feature: useColors contrast checking now supports text color detection.

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

@epiqueras epiqueras added [Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience labels Nov 15, 2019
@epiqueras epiqueras added this to the Future milestone Nov 15, 2019
@epiqueras epiqueras self-assigned this Nov 15, 2019
@@ -56,7 +56,7 @@ function HeadingEdit( {
</InspectorControls>
{ InspectorControlsColorPanel }
<TextColor>
<BackgroundColorDetector querySelector='[contenteditable="true"]' />
<ColorDetector querySelector='[contenteditable="true"]' />
Copy link
Member

Choose a reason for hiding this comment

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

In the button block, the background and color elements are not the same. Would it make sense to have a colorSelector and backgroundSelector? To allow the detector to work when background and color use different elements?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I was just about to ask you that 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done!

@youknowriad
Copy link
Contributor

Drive by comment unrelated with the current PR.

Can we add a README for this hook, I feel it will make it way easier for folks to understand and even contribute to the hook. It's also very valuable when we'll open this hook for third-party block authors;

@epiqueras
Copy link
Contributor Author

Definitely. I also want to try making an interactive MDX guide for it in the storybook, under "Block Utilities".

@epiqueras
Copy link
Contributor Author

@jorgefilipecosta This should be ready to go now 😄

@jorgefilipecosta jorgefilipecosta force-pushed the add/text-color-detection-to-use-colors-hook branch from 53fb172 to a537bc0 Compare November 25, 2019 14:45
Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

I rebased the useColors on paragraph branch #18148 above this branch as a way to test this PR and I noticed some issues that I left as comments.

@@ -29,7 +29,7 @@ function HeadingEdit( {
onReplace,
className,
} ) {
const { TextColor, InspectorControlsColorPanel, BackgroundColorDetector } = __experimentalUseColors(
const { TextColor, InspectorControlsColorPanel, ColorDetector } = __experimentalUseColors(
[ { name: 'textColor', property: 'color' } ],
{
contrastCheckers: { backgroundColor: true },
Copy link
Member

Choose a reason for hiding this comment

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

Should we also add textColor: true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would show the warning when the inherited text color does not have enough contrast.

Is that something we want? Considering this block does not control background color, an inherited text color without enough contrast would also trigger a warning in the parent level and should probably be fixed there.

Copy link
Member

@jorgefilipecosta jorgefilipecosta Nov 25, 2019

Choose a reason for hiding this comment

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

In the paragraph, we need color detection on the text and background color. I'm not sure if we should show a warning if no color was selected at all. Maybe we should have a condition to not show a warning if the user did not use a background or text color?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then it becomes kind of confusing.

Why do I see a warning sometimes, and sometimes I don't?

I think we should just always show the warning now. So I should add textColor: true, what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yes I guess we can add it 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done 😄 9ae645f

}
return (
( needsBackgroundColor || needsColor ) &&
withFallbackStyles(
Copy link
Member

@jorgefilipecosta jorgefilipecosta Nov 25, 2019

Choose a reason for hiding this comment

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

We need to re-execute the color detection when a color changes. On the paragraph block, we want to automatically detect background and text color contrastCheckers: { backgroundColor: true, textColor: true, fontSize: fontSize.size },, we can also change both colors. The contrast warnings don't update when a color challenges but if we go to the code editor and switch back the visual editor to force the component to remount the warnings appear as expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This seems to be working after your last rebase?

Copy link
Member

Choose a reason for hiding this comment

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

No, I think the warnings still don't appear when expected:
Nov-26-2019 09-04-09

We can see the warning appears when it should not, and disappears when it should appear. If I type something and the warning should appear, it appears after I type. Not sure why typing interferes with this, but we should make sure the color computation does not happen on each character typed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was happening because setting a ref does not re-render the component, so the contrast checker used a stale value until the component had any sort of update.

Fixed: 45c81d4.

Copy link
Member

@jorgefilipecosta jorgefilipecosta left a comment

Choose a reason for hiding this comment

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

I verified the problems I noticed were solved, nice work @epiqueras 👍

@epiqueras epiqueras merged commit 2fd13b6 into master Dec 2, 2019
@epiqueras
Copy link
Contributor Author

Thanks for all the thorough reviews 😄

@youknowriad youknowriad deleted the add/text-color-detection-to-use-colors-hook branch December 3, 2019 08:52
@youknowriad youknowriad modified the milestones: Future, Gutenberg 7.1 Dec 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Feature] Extensibility The ability to extend blocks or the editing experience
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants