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

Image: Use layout.wideSize as ResizableBox max-width #39678

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 8 additions & 9 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
BlockControls,
InspectorControls,
RichText,
useSetting,
__experimentalImageSizeControl as ImageSizeControl,
__experimentalImageURLInputUI as ImageURLInputUI,
MediaReplaceFlow,
Expand Down Expand Up @@ -141,6 +142,7 @@ export default function Image( {
},
[ clientId ]
);
const wideSize = useSetting( 'layout.wideSize' );
const { replaceBlocks, toggleSelection } = useDispatch( blockEditorStore );
const { createErrorNotice, createSuccessNotice } = useDispatch(
noticesStore
Expand Down Expand Up @@ -498,15 +500,12 @@ export default function Image( {
naturalHeight < naturalWidth ? MIN_SIZE : MIN_SIZE / ratio;

// With the current implementation of ResizableBox, an image needs an
// explicit pixel value for the max-width. In absence of being able to
// set the content-width, this max-width is currently dictated by the
// vanilla editor style. The following variable adds a buffer to this
// vanilla style, so 3rd party themes have some wiggleroom. This does,
// in most cases, allow you to scale the image beyond the width of the
// main column, though not infinitely.
// @todo It would be good to revisit this once a content-width variable
// becomes available.
const maxWidthBuffer = maxWidth * 2.5;
// explicit pixel value for the max-width.
// The block will try to use a wide layout size defined in the `theme.json`
// or fall back to the editor max-width + some wiggle-room.
const maxWidthBuffer = wideSize
? parseInt( wideSize, 10 )
Copy link
Contributor

Choose a reason for hiding this comment

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

wideSize might not be in px and that could result to quite unexpected results. Also shouldn't we take into account if an image is inside a container with wideSize set?

Copy link
Member Author

Choose a reason for hiding this comment

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

wideSize might not be in px and that could result to quite unexpected results.

I didn't know all the theme.json files I checked had px specified. But it shouldn't be problem all the following will return 1000:

parseInt('1000rem', 10);
parseInt('1000', 10);
parseInt(1000, 10);

Also shouldn't we take into account if an image is inside a container with wideSize set?

The current implementation has the same issue. Ideally, we want ResizableBox to have max-width: 100%, but currently, it only accepts px values. Here're some details: #11846 (comment)

Copy link
Contributor

@ntsekouras ntsekouras Mar 23, 2022

Choose a reason for hiding this comment

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

But it shouldn't be problem all the following will return 1000:

Yes, but if we have something like 50%, it will make the max width 50px. Maybe we should try to improve when we only have a px value? In addition if the value here is smaller than than the viewport it keeps the image contained and I noticed some distortion of the height in that case when I stop dragging.

Screen.Recording.2022-03-23.at.4.04.16.PM.mov

The video is with wideSize: 500px.

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 point. I'll look into this tomorrow. Thanks, Nik!

: maxWidth * 2;

let showRightHandle = false;
let showLeftHandle = false;
Expand Down