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

Accurate sizes: Pass parent alignment context to images #1701

Conversation

joemcgill
Copy link
Member

@joemcgill joemcgill commented Nov 25, 2024

Summary

Related #1511.
Fixes #1725

This PR adds functionality to pass layout info from a parent block to an image block. For example, if an image is constrained by a group block, even if the image block has alignment set to "full", it will not stretch wider than the default content width unless the parent group block is also aligned to "full".

Relevant technical choices

  1. A new context value, 'max_alignment' is added to available context.
  2. By default 'max_alignment' context is 'full', but group and columns blocks own alignment can be passed to their children as context instead during rendering.
  3. core/image and core/cover blocks are filtered to add 'max_alignment' to their 'uses_context' values
  4. During block rendering, auto_sizes_filter_image_tag is used to modify the rendered block markup using block context.
  5. auto_sizes_calculate_better_sizes() is used to calculate better sizes values based on the image characteristics and the 'max_alignment' constraint.

Base automatically changed from update/separate-file-structure to feature/1511-incorporate-layout-constraints-from-ancestors November 26, 2024 04:19
@mukeshpanchal27 mukeshpanchal27 changed the title Auto-sizes: Pass parent alignment context to images Accurate sizes: Pass parent alignment context to images Nov 26, 2024
@mukeshpanchal27 mukeshpanchal27 added [Type] Enhancement A suggestion for improvement of an existing feature [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Nov 26, 2024
@joemcgill joemcgill added [Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) and removed [Plugin] Modern Image Formats Issues for the Modern Image Formats plugin (formerly WebP Uploads) labels Nov 26, 2024
@mukeshpanchal27 mukeshpanchal27 added the no milestone PRs that do not have a defined milestone for release label Nov 28, 2024
@mukeshpanchal27 mukeshpanchal27 added this to the auto-sizes n.e.x.t milestone Nov 28, 2024
@mukeshpanchal27 mukeshpanchal27 removed the no milestone PRs that do not have a defined milestone for release label Nov 28, 2024
@mukeshpanchal27 mukeshpanchal27 marked this pull request as ready for review November 28, 2024 06:08
Copy link

github-actions bot commented Nov 28, 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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: joemcgill <[email protected]>
Co-authored-by: mukeshpanchal27 <[email protected]>
Co-authored-by: felixarntz <[email protected]>
Co-authored-by: westonruter <[email protected]>

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

@joemcgill joemcgill marked this pull request as draft December 2, 2024 16:20
@joemcgill
Copy link
Member Author

I think this still needs some iteration before it's ready for review, so I'm marking it as a draft currently.

* See https://github.com/WordPress/gutenberg/blob/938720602082dc50a1746bd2e33faa3d3a6096d4/packages/block-library/src/cover/style.scss#L82-L87.
*/
if ( 'core/cover' === $block_name ) {
$image_width = $image_width * 0.5;
Copy link
Member Author

Choose a reason for hiding this comment

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

Good idea! I think that instead we could pass this as the function as the $size or $resize_width value for auto_sizes_calculate_better_sizes() and avoid needing to add another argument to the function signature. Alternately, we could consider allowing arbitrary values to be passed to the $max_alignment value, which then overrides any alignment value, which may come in handy whenever we want to pass a maximum width value based on things like columns or grid values.

Also, small nitpick, but this calculation isn't the same as what is used for the cover block, instead it calculates 0.5 * $content-width, which is a CSS variable that is defined in this package. So it should always end up being '420px'.

Copy link
Member Author

Choose a reason for hiding this comment

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

Went with the $size idea and took advantage of the fact that you can use an array for $size when passing to wp_get_attachment_image_src(). See, d3a60d0.

Comment on lines 371 to 372
array( 'left', 'sizes="(max-width: 540px) 100vw, 540px' ),
array( 'right', 'sizes="(max-width: 540px) 100vw, 540px' ),
Copy link
Member Author

Choose a reason for hiding this comment

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

@joemcgill joemcgill marked this pull request as ready for review December 12, 2024 00:56
@mukeshpanchal27
Copy link
Member

@felixarntz @westonruter The PR is ready for review.

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Overall looks good to me.

$sizes = sprintf( '(max-width: %1$s) 100vw, %1$s', $width );
}
$alignment = auto_sizes_get_layout_width( 'default' );
$layout_width = sprintf( '%1$spx', min( (int) $alignment, $image_width ) );
Copy link
Member

Choose a reason for hiding this comment

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

The return value of auto_sizes_get_layout_width() seems like it could be a non-pixel value, like 100vw. Is that a problem?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. This is a concern that I've been thinking about and plan to address in a follow-up PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Documented in #1511 (comment)

Copy link
Member

@westonruter westonruter left a comment

Choose a reason for hiding this comment

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

LGTM

@joemcgill joemcgill merged commit 59e86f8 into feature/1511-incorporate-layout-constraints-from-ancestors Dec 12, 2024
14 checks passed
@joemcgill joemcgill deleted the update/pass-alignment-by-context branch December 12, 2024 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Plugin] Enhanced Responsive Images Issues for the Enhanced Responsive Images plugin (formerly Auto Sizes) [Type] Enhancement A suggestion for improvement of an existing feature
Projects
Status: Done 😃
Development

Successfully merging this pull request may close these issues.

4 participants