Repository navigation
fix(web): restore composer focus when returning to a thread #29179
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
Merged
juliusmarminge
merged 4 commits into
pingdotgg:main
from
voltcrash:voltcrash/restore-thread-focus
Nov 12, 2026
+262
−41
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1cf2701
fix(web): restore composer focus when returning to a thread
voltcrash a506294
fix(web): discard stale terminal focus requests after chat remounts
voltcrash cb8fab6
Merge remote-tracking branch 'origin/main' into voltcrash/restore-thr…
juliusmarminge cb63d68
refactor(web): require the chat root when restoring thread focus
juliusmarminge 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 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
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
17 changes: 13 additions & 4 deletions
17
apps/web/src/components/chat/useFocusComposerOnThreadOpen.browser.test.tsx
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
39 changes: 29 additions & 10 deletions
39
apps/web/src/components/chat/useFocusComposerOnThreadOpen.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,40 @@ | ||
| import type { ScopedThreadRef } from "@t3tools/contracts"; | ||
| import { useEffect } from "react"; | ||
|
|
||
| /** Lands the caret in the composer a frame after a thread opens. */ | ||
| import { restoreFocusOnThreadEnter } from "../../lib/terminalFocus"; | ||
|
|
||
| /** Restores thread focus, landing the caret a frame after composer entry. */ | ||
| export function useFocusComposerOnThreadOpen( | ||
| threadId: string | undefined, | ||
| threadRef: ScopedThreadRef | null, | ||
| root: HTMLElement | null, | ||
| enabled: boolean, | ||
| terminalOpen: boolean, | ||
| focusComposer: () => void, | ||
| ) { | ||
| // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- schedules a frame to focus the composer DOM element when a thread opens with the terminal closed | ||
| // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- restores DOM focus on thread entry and after the drawer opens or closes | ||
| useEffect(() => { | ||
| if (!threadId || !enabled) return; | ||
| const frame = window.requestAnimationFrame(() => { | ||
| // Double-clicking a sidebar row opens its thread and its rename field in one gesture. | ||
| if (document.activeElement?.matches("[data-sidebar-row] input")) return; | ||
| focusComposer(); | ||
| if (!threadRef || !enabled || !root) return; | ||
| let frame: number | undefined; | ||
| const leave = restoreFocusOnThreadEnter(threadRef, { | ||
| root, | ||
| focusComposer: () => { | ||
| frame = window.requestAnimationFrame(() => { | ||
| // Double-clicking a sidebar row opens its thread and its rename field in one gesture. | ||
| if (document.activeElement?.matches("[data-sidebar-row] input")) return; | ||
| focusComposer(); | ||
| }); | ||
| }, | ||
| }); | ||
| return () => { | ||
| window.cancelAnimationFrame(frame); | ||
| leave(); | ||
| if (frame !== undefined) window.cancelAnimationFrame(frame); | ||
| }; | ||
| }, [threadId, enabled, focusComposer]); | ||
| }, [ | ||
| threadRef, | ||
| root, | ||
| enabled, | ||
| // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Drawer visibility changes also restore focus after explicit open/close actions. | ||
| terminalOpen, | ||
| focusComposer, | ||
| ]); | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Preserve terminal focus if it changes before the composer frame runs.
If entry schedules composer restoration and an explicit action focuses a terminal before the next frame, this callback still calls
focusComposer(). The sidebar-input guard does not protect the terminal action. Skip the deferred callback when terminal focus has taken precedence. The PR objective requires explicit terminal actions to retain priority. (raw.githubusercontent.com)