Skip to content

Commit

Permalink
Set to undefined instead
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 committed Aug 21, 2024
1 parent da60c17 commit b7bc071
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -984,19 +984,27 @@ export default function Image( {
onResizeStop={ ( event, direction, elt ) => {
onResizeStop();

let resizedWidth = `${ elt.offsetWidth }px`;
// Set width to 'auto' if the resized width is close to the max-content width.
if ( Math.abs( elt.offsetWidth - maxContentWidth ) < 10 ) {
resizedWidth = 'auto';
elt.style.width = 'auto';
// Clear hardcoded width if the resized width is close to the max-content width.
if (
// Only do this if the image is bigger than the container to prevent it from being squished.
// TODO: Remove this check if the image support setting 100% width.
naturalWidth >= maxContentWidth &&
Math.abs( elt.offsetWidth - maxContentWidth ) < 10
) {
setAttributes( {
width: undefined,
height: undefined,
aspectRatio: undefined,
} );
return;
}

// Since the aspect ratio is locked when resizing, we can
// use the width of the resized element to calculate the
// height in CSS to prevent stretching when the max-width
// is reached.
setAttributes( {
width: resizedWidth,
width: `${ elt.offsetWidth }px`,
height: 'auto',
aspectRatio:
ratio === naturalRatio
Expand Down

0 comments on commit b7bc071

Please sign in to comment.