fix(ui): comment editor textarea not focused on open in WebKit hosts#1031
Open
BrandonNoad wants to merge 1 commit into
Open
fix(ui): comment editor textarea not focused on open in WebKit hosts#1031BrandonNoad wants to merge 1 commit into
BrandonNoad wants to merge 1 commit into
Conversation
The CommentPopover focus effect scheduled a setTimeout(0) keyed on [mode], but in popover mode the textarea only renders after `position` is measured by a later effect. Whether the 0ms timer fires before or after that commit is engine-dependent task ordering: Chromium commits the re-render first, so focus lands; WebKit fires the timer first, so textareaRef.current is still null and the comment editor opens unfocused (reproduced in a WKWebView host — the glimpseui integration — where users had to tab or click into the field for every annotation). Focus from a ref callback on the textarea instead: it runs exactly when the element mounts, so there is nothing to race. Mode switches (popover/dialog) still refocus because the textarea remounts. The deferred focus and caret-to-end behavior are preserved; isConnected guards the popover closing before the timer fires.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
In Comment mode, selecting text (or double-clicking a word) opens the comment editor, but the textarea isn't focused — you have to tab several times or reach for the mouse before you can type. This happens consistently in WKWebView hosts (observed with the
glimpseuiintegration on macOS); Chromium-based browsers are unaffected.Root cause
CommentPopoverfocused the textarea from a mount effect keyed on[mode]that scheduled asetTimeout(0):nulluntilpositionis computed by a later effect, so the textarea only mounts in the next commit.textareaRef.currentis set when the timer fires. WebKit fires the timer first — the ref is stillnull, the effect never re-runs (modedoesn't change), and the editor opens unfocused.Traced by instrumenting the page inside a real glimpse (WKWebView) window:
Manually calling
focus()on the same textarea afterwards worked fine, confirming the engine wasn't refusing focus — the app just never asked.Fix
Focus from a ref callback on the textarea instead of a mount effect. The callback runs exactly when the element mounts, so there's no commit/timer race in either engine:
setTimeout(0)) focus and caret-to-end behavior are preserved.[mode]dep provided).isConnectedguards the case where the popover unmounts before the deferred focus runs (also makes the StrictMode double-attach harmless).Verification
focus(): TEXTAREA active=trueanddocument.activeElementis the textarea as soon as the popover opens.tsc --noEmitonpackages/ui/tsconfig.jsonandtsconfig.strict-consumer.json— clean.bun test packages/ui— 352 pass, 0 fail.