-
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
Tidy and minor refactor of Link UI code #37833
Conversation
Size Change: -6 B (0%) Total Size: 1.29 MB
ℹ️ View Unchanged
|
*/ | ||
import { useState, useEffect } from '@wordpress/element'; | ||
|
||
export default function useInternalInputValue( value ) { |
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.
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.
Would using key
to force remount the component be simpler in this context?
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.
for a use case like a different link in a RichText component, it potentially makes sense to force remount but it's a fairly common practice in reusable components to have "internal/external state and changes in external forcing updates to internal state"
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.
t's a fairly common practice in reusable components to have "internal/external state and changes in external forcing updates to internal state"
I'm not sure, it's essentially the same as a derived state, which isn't really common according to React core team. And seems like we can just call setState
during the rendering phase if we really need to do this (which I'm not sure if is still the recommended approach in concurrent rendering 🤔 ).
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.
Yeh I always felt a little uncomfortable regarding this pattern here.
The key UX here is that the value
shouldn't be committed until the user takes an explicit action (e.g. hitting Submit
or ENTER
). Until that point the data the user has typed should be considered local to the component because if the user clicks away we should be able to discard the change.
That is why the internal state exists.
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.
Would using key to force remount the component be simpler in this context?
When we use LinkControl
we now often pass a key
prop to force re-render the component when a "new" link is being handled (like when you move between links in a paragraph block).
gutenberg/packages/format-library/src/link/inline.js
Lines 236 to 237 in fab909c
<LinkControl | |
key={ forceRemountKey } |
packages/block-editor/src/components/link-control/use-internal-input-value.js
Outdated
Show resolved
Hide resolved
a6a1c8a
to
f02c2ba
Compare
Co-authored-by: Haz <hazdiego@gmail.com>
f0402e1
to
0f7b120
Compare
It would be great if we could get this refactor merged. I don't see any actionable feedback remaining 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.
✅ Added a new link
✅ Read the code
Description
The Link UI code had become a bit jumbled and inordly. This PR
How has this been tested?
Check Link UI works as before.
Check all tests pass.
Screenshots
Types of changes
Checklist:
*.native.js
files for terms that need renaming or removal).