-
Notifications
You must be signed in to change notification settings - Fork 158
Added inline script to cleanup dominant color CSS variable on image load #2448
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
base: trunk
Are you sure you want to change the base?
Changes from 2 commits
3022887
df10388
c62068b
181ef03
ca674e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -179,6 +179,35 @@ function dominant_color_add_inline_style(): void { | |||||
| } | ||||||
| add_action( 'wp_enqueue_scripts', 'dominant_color_add_inline_style' ); | ||||||
|
|
||||||
| /** | ||||||
| * Add 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. | ||||||
| * | ||||||
| * @since 1.2.1 | ||||||
| */ | ||||||
| function dominant_color_add_cleanup_script(): void { | ||||||
| $script = <<<'JS' | ||||||
| document.addEventListener( | ||||||
| "load", | ||||||
| (event) => { | ||||||
| if ( | ||||||
| event.target instanceof HTMLImageElement && | ||||||
| event.target.hasAttribute("data-dominant-color") | ||||||
| ) { | ||||||
| event.target.style.removeProperty("--dominant-color"); | ||||||
| } | ||||||
| }, | ||||||
| { capture: true }, | ||||||
| ); | ||||||
| JS; | ||||||
|
Comment on lines
+192
to
+204
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can indent this properly once we upgrade to PHP 7.4: #2340
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This has now been merged: #2469 Please indent all lines of the nowdoc.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 ☝️ |
||||||
| wp_register_script( 'dominant-color-cleanup', false, array(), DOMINANT_COLOR_IMAGES_VERSION, true ); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
IMO we don’t need to update the script version here. Versioning isn’t really necessary, and we can just pass This is similar to how scripts are registered in the View Transitions plugin. WDYT @westonruter
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. It is an inline script, so the version is not used.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sniff should be updated to not earn when
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mukeshpanchal27 Kindly have a look. |
||||||
| wp_enqueue_script( 'dominant-color-cleanup' ); | ||||||
| wp_add_inline_script( 'dominant-color-cleanup', $script ); | ||||||
| } | ||||||
| add_action( 'wp_enqueue_scripts', 'dominant_color_add_cleanup_script' ); | ||||||
|
|
||||||
| /** | ||||||
| * Displays the HTML generator tag for the Image Placeholders plugin. | ||||||
| * | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.