Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions plugins/dominant-color-images/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 n.e.x.t
*/
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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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 file_get_contents().

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

+1 ☝️

wp_register_script( 'dominant-color-cleanup', false, array(), null, array( 'in_footer' => true ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
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.
*
Expand Down
Loading