Skip to content

Commit 3bd6336

Browse files
t3dotggclaude
andcommitted
fix(web): focus the composer when the window regains focus
Tabbing back into T3 Code left focus wherever it last was, often a button in the pull request panel or the body. The composer only took focus on thread change or terminal close. Now a window focus moves the caret to the composer unless a text field, the terminal, or an open dialog or popup already holds focus. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 7e03dcf commit 3bd6336

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎apps/web/src/components/ChatView.logic.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
getStartedThreadModelChangeBlockReason,
4343
hasEnvironmentReconnectWarningGraceElapsed,
4444
hasServerAcknowledgedLocalDispatch,
45+
shouldRefocusComposerOnWindowFocus,
4546
isBranchMismatchDismissedForSession,
4647
reconcileMountedTerminalThreadIds,
4748
reconcileRetainedMountedThreadIds,
@@ -1861,3 +1862,34 @@ describe("hasServerAcknowledgedLocalDispatch", () => {
18611862
).toBe(true);
18621863
});
18631864
});
1865+
1866+
describe("shouldRefocusComposerOnWindowFocus", () => {
1867+
function element(tagName: string, options?: { editable?: boolean; within?: string }) {
1868+
return {
1869+
tagName,
1870+
isContentEditable: options?.editable ?? false,
1871+
closest: (selector: string) =>
1872+
options?.within !== undefined && selector.includes(options.within) ? ({} as Element) : null,
1873+
};
1874+
}
1875+
1876+
it("refocuses when nothing or the body holds focus", () => {
1877+
expect(shouldRefocusComposerOnWindowFocus(null)).toBe(true);
1878+
expect(shouldRefocusComposerOnWindowFocus(element("BODY"))).toBe(true);
1879+
});
1880+
1881+
it("refocuses away from a plain button, such as a pull request tab", () => {
1882+
expect(shouldRefocusComposerOnWindowFocus(element("BUTTON"))).toBe(true);
1883+
});
1884+
1885+
it("leaves other text fields alone", () => {
1886+
expect(shouldRefocusComposerOnWindowFocus(element("INPUT"))).toBe(false);
1887+
expect(shouldRefocusComposerOnWindowFocus(element("TEXTAREA"))).toBe(false);
1888+
expect(shouldRefocusComposerOnWindowFocus(element("DIV", { editable: true }))).toBe(false);
1889+
});
1890+
1891+
it("leaves focus inside a dialog or popup alone", () => {
1892+
expect(shouldRefocusComposerOnWindowFocus(element("BUTTON", { within: "dialog" }))).toBe(false);
1893+
expect(shouldRefocusComposerOnWindowFocus(element("BUTTON", { within: "-popup" }))).toBe(false);
1894+
});
1895+
});

‎apps/web/src/components/ChatView.logic.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,3 +1027,23 @@ export function hasServerAcknowledgedLocalDispatch(input: {
10271027
input.localDispatch.sessionUpdatedAt !== (session?.updatedAt ?? null)
10281028
);
10291029
}
1030+
1031+
// Returning to the window should land the caret in the composer, so the reader can type right
1032+
// away. The exceptions are places where focus is deliberate: another text field, the terminal,
1033+
// or an open dialog or popup. A focused button is not one of those, so it yields to the composer.
1034+
export function shouldRefocusComposerOnWindowFocus(
1035+
activeElement: (Pick<Element, "tagName" | "closest"> & { isContentEditable?: boolean }) | null,
1036+
): boolean {
1037+
if (activeElement === null || activeElement.tagName === "BODY") return true;
1038+
if (
1039+
activeElement.tagName === "INPUT" ||
1040+
activeElement.tagName === "TEXTAREA" ||
1041+
activeElement.tagName === "SELECT" ||
1042+
activeElement.isContentEditable === true
1043+
) {
1044+
return false;
1045+
}
1046+
return (
1047+
activeElement.closest('[role="dialog"], [role="alertdialog"], [data-slot$="-popup"]') === null
1048+
);
1049+
}

‎apps/web/src/components/ChatView.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ import {
404404
codexArtifactTemplatePromptToAppend,
405405
toolGroupConsumesUpwardNavigation,
406406
waitForStartedServerThread,
407+
shouldRefocusComposerOnWindowFocus,
407408
} from "./ChatView.logic";
408409
import type { ThreadSyncPhase } from "../threadSync";
409410
import { useLocalStorage } from "~/hooks/useLocalStorage";
@@ -1625,6 +1626,7 @@ export default function ChatView(props: ChatViewProps) {
16251626
const [pendingUserInputQuestionIndexByRequestId, setPendingUserInputQuestionIndexByRequestId] =
16261627
useState<Record<string, number>>({});
16271628
const shouldUseRightPanelSheet = useMediaQuery(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY);
1629+
const isMobileViewport = useMediaQuery("max-sm");
16281630
const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
16291631
const [pullRequestDialogState, setPullRequestDialogState] =
16301632
useState<PullRequestDialogState | null>(null);
@@ -4988,6 +4990,31 @@ export default function ChatView(props: ChatViewProps) {
49884990
};
49894991
}, [activeThread?.id, focusComposer, terminalUiState.terminalOpen]);
49904992

4993+
// Tabbing back into the app lands focus wherever it last was, often the right panel or the
4994+
// body. Put it in the composer unless something that takes typing already holds it. Mobile is
4995+
// left alone so returning to the app does not raise the keyboard.
4996+
useEffect(() => {
4997+
if (!activeThread?.id || terminalUiState.terminalOpen || isMobileViewport) return;
4998+
let frame: number | null = null;
4999+
const onWindowFocus = () => {
5000+
if (frame !== null) window.cancelAnimationFrame(frame);
5001+
// The element that held focus receives it again after the window's own event, and the
5002+
// composer ignores that same frame so a restored focus does not lift a scroll-collapsed
5003+
// composer. Wait one more frame so this focus counts as a request to expand it.
5004+
frame = window.requestAnimationFrame(() => {
5005+
frame = window.requestAnimationFrame(() => {
5006+
frame = null;
5007+
if (shouldRefocusComposerOnWindowFocus(document.activeElement)) focusComposer();
5008+
});
5009+
});
5010+
};
5011+
window.addEventListener("focus", onWindowFocus);
5012+
return () => {
5013+
window.removeEventListener("focus", onWindowFocus);
5014+
if (frame !== null) window.cancelAnimationFrame(frame);
5015+
};
5016+
}, [activeThread?.id, focusComposer, isMobileViewport, terminalUiState.terminalOpen]);
5017+
49915018
useEffect(() => {
49925019
if (!activeThread?.id) return;
49935020
if (activeThread.messages.length === 0) {

0 commit comments

Comments
 (0)