Repository navigation
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.
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.
This goes all the way back to #18811, cc @scruffian
There was a problem hiding this comment.
I'm sorry I don't remember!
There was a problem hiding this comment.
It's fine, I think we're good here
|
Size Change: -192 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
youknowriad
left a comment
There was a problem hiding this comment.
Nice simplification, I think the useResizeObserver hook didn't exist yet when we had this clientWidth thing in place.
What?
Removes
useClientWidthin 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
useResizeObserverinside the cropper component.Testing Instructions
npm installafter 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