-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 cropper: remove clientWidth prop with useResizeObserver #60674
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
imageWidthWithinContainer = exceedMaxWidth ? clientWidth : naturalWidth; | ||
} | ||
|
||
if ( ! isResizable || ! imageWidthWithinContainer ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I understood why this condition was done this way. imageWidthWithinContainer
returns either clientWidth
or naturalWidth
. Which will always be a number unless something went wrong with the image loading. So a check for ! naturalWidth || ! naturalHeight
should be enough here, to make sure the image loaded.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This goes all the way back to #18811, cc @scruffian
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry I don't remember!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine, I think we're good here
Size Change: -192 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice simplification, I think the useResizeObserver hook didn't exist yet when we had this clientWidth thing in place.
What?
Removes
useClientWidth
in favour of usinguseResizeObserver
.Why?
This has the advantage of (1) being easier to use by consumers and (2) not calculating the clientWidth on block mount, which is expensive and causes a layout calculation.
How?
We can use
useResizeObserver
inside the cropper component.Testing Instructions
npm install
after the change in #60581, otherwise this fix won't work as expected.Create an image block. Click on the cropper button. The cropper area should be correct (images should be same width as before you pressed the cropper button).
Now switch between align none and align full. The cropper area should adjust correctly.
You can also test if the cropper works for the Site Logo block.
Testing Instructions for Keyboard
Screenshots or screencast