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

Fix button block contrast checker #41294

Merged
merged 8 commits into from
Mar 12, 2025
Merged

Conversation

talldan
Copy link
Contributor

@talldan talldan commented Jan 24, 2025

Proposed changes:

Fixes an issue with the Jetpack Button block's contrast checking. Currently the contrast checker only works when the user has set both the background and text color.

If the user changes only one of the text or background color in a way that causes a contrast issue, the checker doesn't display a warning.

Some history - this functionality was previously handled by a higher order component - applyFallbackStyles / withFallbackStyles. #41139 removed the HOC because it had a few issues:

  • It added a wrapping <div> around the block which breaks a range of styles
  • It didn't check the correct element for background colors
  • It didn't update when the block's css changes (e.g if inline styles or a class are added).

This PR introduces a small hook to replace the applyFallbackStyles / withFallbackStyles to keep the contrast checker working.

Other information:

  • Have you written new tests for your changes, if applicable?
  • Have you checked the E2E test CI results, and verified that your changes do not break them?
  • Have you tested your changes on WordPress.com, if applicable (if so, you'll see a generated comment below with a script to run)?

Does this pull request change what data or activity we track or use?

No

Testing instructions:

  1. Add a contact form block
  2. Select the button in the form
  3. Change only the text color to something that conflicts with the button background (e.g. if there's a black background, set the text to black). Or the opposite, change only the background color.
  4. Observe the contrast checker should be displayed. (in trunk it doesn't show).

@talldan talldan self-assigned this Jan 24, 2025
Copy link
Contributor

github-actions bot commented Jan 24, 2025

Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.

  • To test on WoA, go to the Plugins menu on a WoA dev site. Click on the "Upload" button and follow the upgrade flow to be able to upload, install, and activate the Jetpack Beta plugin. Once the plugin is active, go to Jetpack > Jetpack Beta, select your plugin (Jetpack), and enable the fix/button-fallback-contrast-checking branch.
  • To test on Simple, run the following command on your sandbox:
bin/jetpack-downloader test jetpack fix/button-fallback-contrast-checking

Interested in more tips and information?

  • In your local development environment, use the jetpack rsync command to sync your changes to a WoA dev blog.
  • Read more about our development workflow here: PCYsg-eg0-p2
  • Figure out when your changes will be shipped to customers here: PCYsg-eg5-p2

Copy link
Contributor

github-actions bot commented Jan 24, 2025

Thank you for your PR!

When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:

  • ✅ Include a description of your PR changes.
  • ✅ Add a "[Status]" label (In Progress, Needs Review, ...).
  • ✅ Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).
  • ✅ Add testing instructions.
  • ✅ Specify whether this PR includes any changes to data or privacy.
  • ✅ Add changelog entries to affected projects

This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖


Follow this PR Review Process:

  1. Ensure all required checks appearing at the bottom of this PR are passing.
  2. Make sure to test your changes on all platforms that it applies to. You're responsible for the quality of the code you ship.
  3. You can use GitHub's Reviewers functionality to request a review.
  4. When it's reviewed and merged, you will be pinged in Slack to deploy the changes to WordPress.com simple once the build is done.

If you have questions about anything, reach out in #jetpack-developers for guidance!


Jetpack plugin:

The Jetpack plugin has different release cadences depending on the platform:

  • WordPress.com Simple releases happen as soon as you deploy your changes after merging this PR (PCYsg-Jjm-p2).
  • WoA releases happen weekly.
  • Releases to self-hosted sites happen monthly:
    • Scheduled release: April 1, 2025
    • Code freeze: March 30, 2025

If you have any questions about the release process, please ask in the #jetpack-releases channel on Slack.

@github-actions github-actions bot added [Block] Button [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Status] In Progress [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 24, 2025
@talldan talldan added [Status] Needs Team Review Obsolete. Use Needs Review instead. and removed [Status] Needs Author Reply We need more details from you. This label will be auto-added until the PR meets all requirements. labels Jan 24, 2025
@talldan talldan added the [Type] Bug When a feature is broken and / or not performing as intended label Jan 24, 2025
@talldan talldan force-pushed the fix/button-fallback-contrast-checking branch from bea3f18 to 4ca3ad4 Compare February 7, 2025 01:51
Copy link
Contributor

github-actions bot commented Feb 7, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/extensions/blocks/button/edit.js 9/10 (90.00%) 1.11% 0 💚

1 file is newly checked for coverage.

File Coverage
projects/plugins/jetpack/extensions/blocks/button/use-fallback-colors.js 7/12 (58.33%) 💚

Full summary · PHP report · JS report

@talldan talldan marked this pull request as ready for review February 7, 2025 02:00
Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

Thanks for squashing this bug @talldan 👍

I had to rebase this branch for it to build locally for me but once it did everything tested as advertised.

✅ Warning displayed when poorly contrasting colors were chosen
✅ Warning worked regardless of when the combination was user selections, inherited values etc.
✅ Warning was removed when color combination improved or reset to valid values

With the push to improve test coverage in Jetpack, could we add some tests for the hook here as well?

I also left a tiny optimization suggestion as an inline comment. Other than that I think this is looking pretty good.

Screen.Recording.2025-02-28.at.5.18.17.pm.mp4

Comment on lines 9 to 10
const fallbackBackgroundColor = getComputedStyle( node ).backgroundColor;
const fallbackTextColor = getComputedStyle( node ).color;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can make a small optimization here by avoiding repeated calls to getComputedStyle( node ).

@talldan talldan force-pushed the fix/button-fallback-contrast-checking branch from 4ca3ad4 to ba59b5a Compare March 10, 2025 03:40
Copy link

jp-launch-control bot commented Mar 10, 2025

Code Coverage Summary

Coverage changed in 1 file.

File Coverage Δ% Δ Uncovered
projects/plugins/jetpack/extensions/blocks/button/edit.js 9/10 (90.00%) 1.11% 0 💚

1 file is newly checked for coverage.

File Coverage
projects/plugins/jetpack/extensions/blocks/button/use-fallback-colors.js 14/15 (93.33%) 💚

Full summary · PHP report · JS report

@talldan
Copy link
Contributor Author

talldan commented Mar 10, 2025

Thanks for the review. Feedback should all be addressed 👍

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

Nice work @talldan

This is testing well for me. I couldn't break the contrast checker and the tests all pass.

LGTM 🚢

Screenshot 2025-03-10 at 7 46 11 pm

@talldan talldan force-pushed the fix/button-fallback-contrast-checking branch from 0a96096 to b59172b Compare March 12, 2025 03:37
@talldan talldan merged commit d300b7c into trunk Mar 12, 2025
65 checks passed
@talldan talldan deleted the fix/button-fallback-contrast-checking branch March 12, 2025 04:13
@github-actions github-actions bot added this to the jetpack/14.5 milestone Mar 12, 2025
@github-actions github-actions bot removed [Status] In Progress [Status] Needs Team Review Obsolete. Use Needs Review instead. labels Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Button [Plugin] Jetpack Issues about the Jetpack plugin. https://wordpress.org/plugins/jetpack/ [Tests] Includes Tests [Type] Bug When a feature is broken and / or not performing as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants