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

Try using maxHeight instead of height in the dimensions tool #53365

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
"height": {
"type": "string"
},
"maxWidth": {
"type": "string"
},
"maxHeight": {
"type": "string"
},
"aspectRatio": {
"type": "string"
},
Expand Down
31 changes: 21 additions & 10 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default function Image( {
title,
width,
height,
maxHeight,
aspectRatio,
scale,
linkTarget,
Expand All @@ -117,7 +118,7 @@ export default function Image( {

// The only supported unit is px, so we can parseInt to strip the px here.
const numericWidth = width ? parseInt( width, 10 ) : undefined;
const numericHeight = height ? parseInt( height, 10 ) : undefined;
const numericHeight = height ? parseInt( maxHeight, 10 ) : undefined;

const imageRef = useRef();
const prevCaption = usePrevious( caption );
Expand Down Expand Up @@ -326,7 +327,7 @@ export default function Image( {

function updateAlignment( nextAlign ) {
const extraUpdatedAttributes = [ 'wide', 'full' ].includes( nextAlign )
? { width: undefined, height: undefined }
? { width: undefined, maxHeight: undefined }
: {};
setAttributes( {
...extraUpdatedAttributes,
Expand Down Expand Up @@ -443,7 +444,7 @@ export default function Image( {
resetAll={ () =>
setAttributes( {
width: undefined,
height: undefined,
maxHeight: undefined,
scale: undefined,
aspectRatio: undefined,
} )
Expand Down Expand Up @@ -479,14 +480,19 @@ export default function Image( {
) }
{ isResizable && (
<DimensionsTool
value={ { width, height, scale, aspectRatio } }
value={ {
width,
height: maxHeight,
scale,
aspectRatio,
} }
onChange={ ( newValue ) => {
// Rebuilding the object forces setting `undefined`
// for values that are removed since setAttributes
// doesn't do anything with keys that aren't set.
setAttributes( {
width: newValue.width,
height: newValue.height,
maxHeight: newValue.height,
scale: newValue.scale,
aspectRatio: newValue.aspectRatio,
} );
Expand Down Expand Up @@ -564,9 +570,13 @@ export default function Image( {
className={ borderProps.className }
style={ {
width:
( width && height ) || aspectRatio ? '100%' : undefined,
height:
( width && height ) || aspectRatio ? '100%' : undefined,
( width && maxHeight ) || aspectRatio
? '100%'
: undefined,
maxHeight:
( width && maxHeight ) || aspectRatio
? '100%'
: undefined,
objectFit: scale,
...borderProps.style,
} }
Expand Down Expand Up @@ -600,7 +610,7 @@ export default function Image( {
/>
);
} else if ( ! isResizable ) {
img = <div style={ { width, height, aspectRatio } }>{ img }</div>;
img = <div style={ { width, maxHeight, aspectRatio } }>{ img }</div>;
} else {
const numericRatio = aspectRatio && evalAspectRatio( aspectRatio );
const customRatio = numericWidth / numericHeight;
Expand Down Expand Up @@ -666,7 +676,7 @@ export default function Image( {
display: 'block',
objectFit: scale,
aspectRatio:
! width && ! height && aspectRatio
! width && ! maxHeight && aspectRatio
? aspectRatio
: undefined,
} }
Expand Down Expand Up @@ -696,6 +706,7 @@ export default function Image( {
setAttributes( {
width: `${ elt.offsetWidth }px`,
height: 'auto',
maxHeight: undefined,
aspectRatio: `${ ratio }`,
} );
} }
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/image/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export default function save( { attributes } ) {
linkClass,
width,
height,
maxWidth,
maxHeight,
aspectRatio,
scale,
id,
Expand Down Expand Up @@ -60,6 +62,8 @@ export default function save( { attributes } ) {
objectFit: scale,
width,
height,
maxWidth,
maxHeight,
} }
title={ title }
/>
Expand Down
Loading