Added inline script to cleanup dominant color CSS variable on image load#2448
Added inline script to cleanup dominant color CSS variable on image load#2448Dhruval-678 wants to merge 5 commits into
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @Dogway. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #2448 +/- ##
==========================================
- Coverage 69.33% 69.28% -0.06%
==========================================
Files 90 90
Lines 7749 7755 +6
==========================================
Hits 5373 5373
- Misses 2376 2382 +6
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| { capture: true }, | ||
| ); | ||
| JS; | ||
| wp_register_script( 'dominant-color-cleanup', false, array(), DOMINANT_COLOR_IMAGES_VERSION, true ); |
There was a problem hiding this comment.
| wp_register_script( 'dominant-color-cleanup', false, array(), DOMINANT_COLOR_IMAGES_VERSION, true ); | |
| wp_register_script( 'dominant-color-cleanup', false, array(), null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
IMO we don’t need to update the script version here. Versioning isn’t really necessary, and we can just pass null instead.
This is similar to how scripts are registered in the View Transitions plugin.
WDYT @westonruter
There was a problem hiding this comment.
Correct. It is an inline script, so the version is not used.
There was a problem hiding this comment.
The sniff should be updated to not earn when null is used for a script with a false src, but that's out of scope here.
There was a problem hiding this comment.
@mukeshpanchal27
Requested changes has been applied.
Kindly have a look.
| document.addEventListener( | ||
| "load", | ||
| (event) => { | ||
| if ( | ||
| event.target instanceof HTMLImageElement && | ||
| event.target.hasAttribute("data-dominant-color") | ||
| ) { | ||
| event.target.style.removeProperty("--dominant-color"); | ||
| } | ||
| }, | ||
| { capture: true }, | ||
| ); | ||
| JS; |
There was a problem hiding this comment.
We can indent this properly once we upgrade to PHP 7.4: #2340
There was a problem hiding this comment.
This has now been merged: #2469
Please indent all lines of the nowdoc.
There was a problem hiding this comment.
Or actually, shouldn't this actually be made available in a JS file? Then we'll get linting performed on it and we can get it minified. The JS file can be loaded with file_get_contents().
| { capture: true }, | ||
| ); | ||
| JS; | ||
| wp_register_script( 'dominant-color-cleanup', false, array(), null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
There was a problem hiding this comment.
Let's be explicit about the args:
| wp_register_script( 'dominant-color-cleanup', false, array(), null, true ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion | |
| wp_register_script( 'dominant-color-cleanup', false, array(), null, array( 'in_footer' => true ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
There was a problem hiding this comment.
Screen.Recording.2026-05-18.at.12.18.50.PM.movIt looks like the background color disappears after the image loads. I’m pretty sure some non tech folks will raise this concern in the future 😄 Is there a way to detect whether the image has |
Summary
Fixes #2432
Changes
Added inline script to cleanup dominant color CSS variable on image load.
This script removes the --dominant-color CSS variable from images after they load,
preventing issues with dynamically loaded or lazily-loaded images.
Use of AI Tools
Clude code