-
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
Buttons block: fix focus on insert #39684
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
In
trunk
, this code fails to get the focus to the inner block whentemplateInsertUpdatesSelection
istrue
because:initialPosition
is 0, hence this hooks gives it the focus.initialPosition
is nullish, so it doesn't gain focus. The reason it's no longer selected (and initialPosition nullish) is because theuseFocusHandler
has already detected that selection & focus are not in sync and has dispatched theselect_block
action to update selection to the wrapper.In
trunk
, this code works as expected whentemplateInsertUpdatesSelection
isfalse
because:initialPosition
is 0, hence it gets the focus.initialPosition
is nullish, so the hook does nothing.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.
In
trunk
, or you mean in this branch?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 meant in
trunk
, just trying to document the before/after behavior :)A more precise description of why this fails in
trunk
whentemplateInsertUpdatesSelection
istrue
is this:useFocusFirstElement
runs for the wrapper. At this point, the wrapper is selected and soinitialPosition
is 0. The effect is hooked for React to execute it.initialPosition
computation for the wrapper wouldn't be 0. However, the effect for it is already queued and we don't have a way to update that stale info.initialPosition
to 0) and focuses the wrapper.useFocusHandler
hook detects the wrapper is focused and updates the selection to be in the wrapper again (select_block action).useFocusFirstElement
hook runs for the inner block, but theinitialPosition
is undefined. When the effect is executed, it does nothing.So this fix prevents 4 from focusing the wrapper and 5 from happening. Hence, at 6, the initialPosition for the inner block is 0 and will focus it.
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've looked at
useInitialPosition
for other potential issues. It seems the rest are more difficult to get into a race condition like block selection is because they're user actions (happen at lower cycles than redux/react cycles). Though, in theory, it could happen again: what would you think of removinguseInitialPosition
hook entirely and inline its contents within the effect itself for clarity?