-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Replace deprecated useResizeObserver for useScaleCanvas #67508
base: trunk
Are you sure you want to change the base?
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. |
Size Change: +84 B (0%) Total Size: 1.83 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.
Thanks for the code quality cleanup! Working well.
b48f02a
to
714ad14
Compare
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 very happy to see us migrating to the new useResizeObserver
🚀
contentResizeListener, | ||
containerResizeListener, | ||
contentRefCallback, | ||
containerRefCallback, |
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.
Let's call it just contentRef
/containerRef
. The fact that it's a callback is an implementation detail that nobody really needs to know.
Also I think React Compiler treats variables differently when their name has the *Ref
suffix.
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.
Let's call it just
contentRef
/containerRef
. The fact that it's a callback is an implementation detail that nobody really needs to know.
When I'm writing my own React code, I typically differentiate between RefObject<T>
and RefCallback<T>
with different suffixes so I know if I can access a .current
property or not.
Also I think React Compiler treats variables differently when their name has the
*Ref
suffix.
Thanks for bringing that to my attention. It looks like React Compiler bases it on the useRef()
call, not the name of the variable.
I know ESLint's react/compiler
rules are still a little wonky. Maybe ESLint was using the *Ref
suffix for checking before, but I don't see any errors now. 🤷
That all being said, I don't see anyone else differentiating them in Gutenberg though, so I suppose I can change it here.
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 looks like React Compiler bases it on the
useRef()
call
@tyxla had to change a lot of ref variable names in #64718 to prevent a "mutating a value" error from the compiler. So the names matter.
In this case contentRefCallback
would probably still be fine, because there is never any .current = ...
assignment related to contentRefCallback
.
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.
The ESLint plugin has been enabled since #61788, so it should automatically detect if we have a problem with the ref and if it needs to be named with *Ref
. If it didn't detect it, then you're most likely not mutating the ref's .current
, which is totally fine.
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 did some more testing in the React Compiler Playground. I didn't have the enableTreatRefLikeIdentifiersAsRefs
option set which corresponds to the ESLint rule that you're talking about. The new example has it enabled.
I'm wondering if we should actually have a different name for RefCallback<T>
variables because they don't have a .current
property like the RefObject<T>
variables? A Ref
suffix makes the ESLint (and React Compiler) think that they do with the enableTreatRefLikeIdentifiersAsRefs
option enabled.
This PR probably isn't the best place to discuss, so I'll open a discussion and link it here.
EDIT: Discussion in #67756
packages/block-editor/src/components/iframe/use-scale-canvas.js
Outdated
Show resolved
Hide resolved
714ad14
to
8e25519
Compare
b28ef42
to
4f8c5be
Compare
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.
Looks good, thanks for applying the feedback 🚀
4f8c5be
to
ef60a79
Compare
What?
Replaces
useResizeObserver
with the new callback-based implementation inuseScaleCanvas
.Why?
The old usage was deprecated.
How?
contentBlockSize
is available in all our supported browsers, so I only implemented that branch of the legacyuseResizeObserver
and I didn't bother with rounding as the algorithm does rounding elsewhere.gutenberg/packages/compose/src/hooks/use-resize-observer/legacy/index.tsx
Lines 45 to 67 in 1693537
Testing Instructions
Testing Instructions for Keyboard
Screenshots or screencast