Skip to content

Conversation

@pbearne
Copy link
Contributor

@pbearne pbearne commented Dec 3, 2024

Added inline CSS and JavaScript to display dominant colors and transparency in the WordPress admin area for attachment previews. This enhancement allows the media library to utilize dominant color and transparency data, providing visual feedback on image properties. Updated version to 1.1.2 to reflect these changes.

Summary

Fixes #

Relevant technical choices

Added inline CSS and JavaScript to display dominant colors and transparency in the WordPress admin area for attachment previews. This enhancement allows the media library to utilize dominant color and transparency data, providing visual feedback on image properties. Updated version to 1.1.2 to reflect these changes.
@github-actions
Copy link

github-actions bot commented Dec 3, 2024

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 props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @[email protected].

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.

Unlinked contributors: [email protected].

Co-authored-by: pbearne <[email protected]>
Co-authored-by: westonruter <[email protected]>
Co-authored-by: joemcgill <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: mxbclang <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: adamsilverstein <[email protected]>
Co-authored-by: estlacksensory <[email protected]>
Co-authored-by: spacedmonkey <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

pbearne and others added 3 commits December 3, 2024 13:38
Update version numbers and associated comments from 1.1.2 to 1.1.3 for consistency across the plugin files. This change ensures that the codebase reflects the correct version for recent updates or bug fixes included in this release.
@pbearne pbearne linked an issue Dec 3, 2024 that may be closed by this pull request
Paul Bearne added 2 commits December 3, 2024 13:49
Corrected inconsistent indentation and spacing to improve code readability and maintainability in hooks.php. These changes ensure a cleaner code style without altering the functionality or logic.
Corrected inconsistent indentation and spacing to improve code readability and maintainability in hooks.php. These changes ensure a cleaner code style without altering the functionality or logic.
@pbearne pbearne added this to the dominant-color-images n.e.x.t milestone Dec 3, 2024
@pbearne pbearne added the [Plugin] Image Placeholders Issues for the Image Placeholders plugin (formerly Dominant Color Images) label Dec 3, 2024
This commit specifies return types for several functions in the dominant-color-images plugin. The `dominant_color_admin_inline_style` and `dominant_color_admin_script` functions now explicitly return void, and the `dominant_color_prepare_attachment_for_js` function now returns an array. Adding these return types improves code clarity and aligns with current PHP best practices.
@pbearne pbearne added the [Type] Enhancement A suggestion for improvement of an existing feature label Dec 3, 2024
Paul Bearne added 3 commits December 3, 2024 13:58
The @return void annotations were removed from the PHPDoc comments in two functions within the hooks.php file. These annotations were redundant, as the functions' return types are already declared with "void" in the function signatures. This cleanup enhances the readability and maintains consistency across the codebase.
Updated the function docblock type hints for arrays to use generics, enhancing code clarity and consistency. This change does not affect functionality but improves the readability and maintenance of the codebase.
Updated the function docblock type hints for arrays to use generics, enhancing code clarity and consistency. This change does not affect functionality but improves the readability and maintenance of the codebase.
Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

Thanks @pbearne. This looks mostly good to go for me. Left a few suggestions

$response['hasTransparencyClass'] = '';
if ( isset( $meta['has_transparency'] ) ) {
$response['hasTransparency'] = $meta['has_transparency'];
$response['hasTransparencyClass'] = $meta['has_transparency'] ? 'has-transparency' : 'not-transparent';
Copy link
Member

Choose a reason for hiding this comment

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

This could be inferred on the front end and not returned as part of the response.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I belive we need this

Copy link
Member

Choose a reason for hiding this comment

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

It's fine to create the class on the server and pass it as part of the response, but just to clarify what I meant, in the script that gets output in dominant_color_admin_script(), you could do something like this (untested):

	<script>
		(function() {
			var s = jQuery( '#tmpl-attachment' )
			var transparencyClassName = 'true' === data.hasTransparency ? 'has-transparency' : 'not-transparent';
			var n = s[0].innerText.replace( '{{ data.orientation }}"', '{{ data.orientation }} {{ transparencyClassName }}"	data-dominant-color="{{ data.dominantColor }}" data-has-transparency="{{ data.hasTransparency }}" style="--dominant-color: #{{ data.dominantColor }};"')
			s.replaceWith( '<script type="text/html" id="tmpl-attachment">' + n  );
		}());
	</script>

Also, it looks like only the has-transparency class is ever referenced by the custom CSS that gets added here, so if could be simplified to:

var transparencyClassName = 'true' === data.hasTransparency ? 'has-transparency' : '';

or if this remains in PHP:

$response['hasTransparencyClass'] = $meta['has_transparency'] ? 'has-transparency' : '';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made the change to use JS for the class

Updated the dominant color and transparency properties in hooks.php to use proper sanitization functions. This change helps prevent potential issues with unsanitized data affecting the media library display. Also, adjusted version references for an upcoming release.
pbearne and others added 6 commits December 6, 2024 10:54
Reorganize the logic for assigning the transparency class name by moving it from the server-side to the client-side script. This change improves code clarity by handling conditional class name assignments directly within the client-side context, thereby simplifying the server-side response handling.
@pbearne pbearne requested a review from westonruter December 9, 2024 14:40
@westonruter
Copy link
Member

I'm seeing the dominant color on the frontend:

<img data-dominant-color="070606" data-has-transparency="false" style="--dominant-color: #070606;" fetchpriority="high" decoding="async" width="1024" height="403" src="http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-1024x403.png" alt="" class="wp-image-402 not-transparent" srcset="http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-1024x403.png 1024w, http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-300x118.png 300w, http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02-768x302.png 768w, http://localhost:8888/wp-content/uploads/2024/12/Screenshot-2024-12-09-12.43.02.png 1294w" sizes="(max-width: 1024px) 100vw, 1024px">

But on the Media screen and in the Media modal, I'm not seeing anything added:

<div class="not-transparent attachment-preview js--select-attachment type-image subtype-png landscape" data-dominant-color="" data-has-transparency="false" style="--dominant-color: #;">

Note the dominant color is missing from the underlying data. In looking at the Ajax response, I see:

{
    "id": 402,
    "title": "Screenshot 2024-12-09 12.43.02",
    "filename": "Screenshot-2024-12-09-12.43.02.png",
    "url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02.png",
    "link": "http:\/\/localhost:8888\/screenshot-2024-12-09-12-43-02\/",
    "alt": "",
    "author": "1",
    "description": "",
    "caption": "",
    "name": "screenshot-2024-12-09-12-43-02",
    "status": "inherit",
    "uploadedTo": 0,
    "date": 1734116876000,
    "modified": 1734116876000,
    "menuOrder": 0,
    "mime": "image\/png",
    "type": "image",
    "subtype": "png",
    "icon": "http:\/\/localhost:8888\/wp-includes\/images\/media\/default.svg",
    "dateFormatted": "December 13, 2024",
    "nonces": {
        "update": "6ec61bcfb7",
        "delete": "92edbf45a7",
        "edit": "dccdfde8bb"
    },
    "editLink": "http:\/\/localhost:8888\/wp-admin\/post.php?post=402&action=edit",
    "meta": false,
    "authorName": "admin",
    "authorLink": "http:\/\/localhost:8888\/wp-admin\/profile.php",
    "filesizeInBytes": 92142,
    "filesizeHumanReadable": "90 KB",
    "context": "",
    "height": 509,
    "width": 1294,
    "orientation": "landscape",
    "sizes": {
        "thumbnail": {
            "height": 150,
            "width": 150,
            "url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02-150x150.png",
            "orientation": "landscape"
        },
        "medium": {
            "height": 118,
            "width": 300,
            "url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02-300x118.png",
            "orientation": "landscape"
        },
        "large": {
            "height": 238,
            "width": 604,
            "url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02-1024x403.png",
            "orientation": "landscape"
        },
        "full": {
            "url": "http:\/\/localhost:8888\/wp-content\/uploads\/2024\/12\/Screenshot-2024-12-09-12.43.02.png",
            "height": 509,
            "width": 1294,
            "orientation": "landscape"
        }
    },
    "compat": {
        "item": "",
        "meta": ""
    },
    "dominantColor": "",
    "hasTransparency": false
}

@westonruter
Copy link
Member

In looking at the underlying attachment meta, the dominant_color does not start with #"

{
    "dominant_color": "070606",
    "has_transparency": false
}

@westonruter
Copy link
Member

westonruter commented Dec 13, 2024

Seems like this is now working!

Before:

Screen.recording.2024-12-13.11.52.07.webm

image

After:

Screen.recording.2024-12-13.11.53.06.webm

image

@westonruter westonruter changed the title Enhance admin UI with dominant color support. Enhance admin media UI with dominant color support Dec 13, 2024
Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

Looking good to me. Thanks @pbearne!

@joemcgill joemcgill merged commit 3258112 into WordPress:trunk Dec 13, 2024
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Image Placeholders Issues for the Image Placeholders plugin (formerly Dominant Color Images) [Type] Enhancement A suggestion for improvement of an existing feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use dominant color as background color in media modal

4 participants