Autocomplete: Prevent text cursor position loss when clicking to insert an item#70660
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. |
|
@ellatrix, I would appreciate your feedback regarding this "naive" solution. |
|
Another approach would be to explicitly return the focus, but what do you think? diff --git a/packages/components/src/autocomplete/index.tsx b/packages/components/src/autocomplete/index.tsx
index d13d1d9b6a..ebd3d92755 100644
--- a/packages/components/src/autocomplete/index.tsx
+++ b/packages/components/src/autocomplete/index.tsx
@@ -126,6 +126,10 @@ export function useAutocomplete( {
// Reset autocomplete state after insertion rather than before
// so insertion events don't cause the completion menu to redisplay.
reset();
+
+ if ( contentRef.current ) {
+ contentRef.current.focus();
+ }
}
function reset() { |
b10357a to
0ec5682
Compare
|
Good call, @t-hamano! I like this explicit solution more. |
|
Flaky tests detected in 0ec5682. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/16257939190
|
t-hamano
left a comment
There was a problem hiding this comment.
LGTM! I know that the failing unit tests are not related to this PR.
|
Restoring focus is the correct solution for restoring the selection 👍 |
…rt an item (#70660) Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: ellatrix <ellatrix@git.wordpress.org> Co-authored-by: sandymcfadden <ourvalley@git.wordpress.org>
What?
Fixes #50168.
PR fixes text cursor position loss when clicking the autocomplete suggestion item and allows users to continue typing after selection.
Why?
The behavior works correctly when using the keyboard; it should function similarly after a click interaction.
How?
Prevent the button from "stealing" focus during the click event and let RichText restore the text cursor position.Ensure that the content receives focus back after selection.
Testing Instructions
@symbol.Testing Instructions for Keyboard
Same.
Screenshots or screencast
CleanShot.2025-07-09.at.14.34.35.mp4