close
Skip to content

Fix: Preserve aspectRatio and scale when switching to wide/full align…#76914

Merged
mikachan merged 2 commits into
WordPress:trunkfrom
adithya-naik:fix/image-block-aspect-ratio-alignment
Apr 17, 2026
Merged

Fix: Preserve aspectRatio and scale when switching to wide/full align…#76914
mikachan merged 2 commits into
WordPress:trunkfrom
adithya-naik:fix/image-block-aspect-ratio-alignment

Conversation

@adithya-naik
Copy link
Copy Markdown
Contributor

What?

Fixes a bug where the aspectRatio and scale attributes of the Image Block
are reset to undefined when switching alignment to Wide Width or Full Width.

Fixes #76697

Why?

A useEffect in edit.js was introduced in #55954 to clear width and
height pixel dimensions when switching to wide or full alignment —
which is correct, since explicit pixel dimensions conflict with the 100%
container width those alignments apply.

However, the same effect was also clearing aspectRatio and scale. These
are CSS-based attributes (aspect-ratio and object-fit) that are fully
compatible with wide/full alignment and should not be affected by the
alignment change.

How?

Removed aspectRatio: undefined and scale: undefined from the setAttributes
call inside the useEffect that fires on wide/full alignment changes.

Only width and height (inline pixel dimensions) are now cleared,
preserving the CSS-based aspect ratio and object-fit scale behavior.

Before:

useEffect( () => {
    if ( [ 'wide', 'full' ].includes( align ) ) {
        __unstableMarkNextChangeAsNotPersistent();
        setAttributes( {
            width: undefined,
            height: undefined,
            aspectRatio: undefined,
            scale: undefined,
        } );
    }
}, [ __unstableMarkNextChangeAsNotPersistent, align, setAttributes ] );

After:

useEffect( () => {
    if ( [ 'wide', 'full' ].includes( align ) ) {
        __unstableMarkNextChangeAsNotPersistent();
        setAttributes( {
            width: undefined,
            height: undefined,
        } );
    }
}, [ __unstableMarkNextChangeAsNotPersistent, align, setAttributes ] );

Testing Instructions

  1. Add a new Post
  2. Add an Image Block and upload any image
  3. In the sidebar, open the Styles tab
  4. Set Aspect Ratio to Square - 1:1
  5. Change Alignment to Wide Width or Full Width via the block toolbar
  6. ✅ Confirm the Aspect Ratio dropdown still shows "Square - 1:1"
  7. ✅ Confirm the image visually maintains the square crop
  8. Switch between Content / Settings / Styles tabs
  9. ✅ Confirm the Aspect Ratio dropdown does not reset to "Original"
  10. Switch alignment back to None
  11. ✅ Confirm aspect ratio is still preserved (regression check)

Screenshots or screencast

As videos are larger than 10MB, uploaded into drive and gave public access
https://drive.google.com/drive/folders/1R6Stkc4lsDSjUBT6Mj2IQ7Zg2cuFOmfO?usp=sharing

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 30, 2026

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: adithya-naik <adithyanaik@git.wordpress.org>
Co-authored-by: moyerdestroyer <ryanmoyer@git.wordpress.org>
Co-authored-by: Mustafabharmal <mustafabharmal@git.wordpress.org>
Co-authored-by: eoinsav <wpeoin@git.wordpress.org>

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

@github-actions github-actions Bot added [Package] Block library /packages/block-library First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository labels Mar 30, 2026
@github-actions
Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @adithya-naik! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@adithya-naik
Copy link
Copy Markdown
Contributor Author

Hi @ajitbohra and @fabiankaegy 👋

Following up on this PR. The fix ensures that aspectRatio and scale are preserved when switching alignment, without affecting existing behavior.

All checks are passing. Happy to make any updates if needed—would love your review when you have time. Thanks!

@mikachan mikachan added [Type] Bug An existing feature does not function as intended [Block] Image Affects the Image Block labels Apr 5, 2026
@adithya-naik
Copy link
Copy Markdown
Contributor Author

Hi @mikachan,

This is just a follow up remainder on this PR

Copy link
Copy Markdown
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

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

Thanks @adithya-naik! This fixes the issue for me, and the testing instructions work as described 🙌

@mikachan mikachan merged commit f10f300 into WordPress:trunk Apr 17, 2026
48 checks passed
@github-actions github-actions Bot added this to the Gutenberg 23.1 milestone Apr 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Image Affects the Image Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Aspect Ratio not preserved when changing width of Image Block

2 participants