Skip to content

Commit 8e3d768

Browse files
fix(web): returning to the window keeps a text selection
Refocusing the composer on window focus moved the document selection into the editor, wiping text the user had selected in a reply. Skip the refocus while the document holds a non-collapsed selection; otherwise the existing refocus rules are unchanged. Fixes #31698 Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ea674ca commit 8e3d768

3 files changed

Lines changed: 14 additions & 8 deletions

File tree

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ it("keeps keyboard focus on a sidebar row on window return, but not a clicked ro
2020

2121
await userEvent.click(clicked);
2222
expect(document.activeElement).toBe(clicked);
23-
expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(true);
23+
expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
24+
true,
25+
);
2426

2527
await userEvent.tab();
2628
expect(document.activeElement).toBe(tabbed);
27-
expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
29+
expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
30+
false,
31+
);
2832
});
2933

3034
it("leaves a text selection in the timeline alone on window return", async () => {
@@ -40,7 +44,7 @@ it("leaves a text selection in the timeline alone on window return", async () =>
4044
expect(document.activeElement).toBe(timeline);
4145
expect(document.getSelection()?.toString()).toContain("Steel bridges");
4246
expect(
43-
shouldRefocusComposerOnWindowFocus(document.activeElement),
47+
shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()),
4448
"refocusing the composer would clear the selected reply text",
4549
).toBe(false);
4650
});

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,15 +1140,17 @@ export function hasServerAcknowledgedLocalDispatch(input: {
11401140
);
11411141
}
11421142

1143-
// Returning to the window should land the caret in the composer, so the reader can type right
1144-
// away. The exceptions are places where focus is deliberate: another text field, a terminal in
1145-
// the drawer or the right panel, an open dialog, menu, or popup, or a region or sidebar row
1146-
// reached from the keyboard. A plain focused button or a clicked row yields to the composer.
1143+
// Returning to the window should land the caret in the composer, except where focus or a
1144+
// selection is deliberate: another text field, a terminal, an open dialog, menu, or popup, a
1145+
// region or sidebar row reached from the keyboard, or selected text the composer would clear.
1146+
// A plain focused button or a clicked row yields to the composer.
11471147
export function shouldRefocusComposerOnWindowFocus(
11481148
activeElement:
11491149
| (Pick<Element, "tagName" | "closest" | "getAttribute"> & { isContentEditable?: boolean })
11501150
| null,
1151+
selection: Pick<Selection, "isCollapsed"> | null,
11511152
): boolean {
1153+
if (selection !== null && !selection.isCollapsed) return false;
11521154
if (activeElement === null || activeElement.tagName === "BODY") return true;
11531155
if (
11541156
activeElement.tagName === "INPUT" ||

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2143,7 +2143,7 @@ export default function ChatView(props: ChatViewProps) {
21432143
if (document.querySelector("[data-mcp-app-fullscreen]") !== null) return;
21442144
if (
21452145
!isInSideChatPane(document.activeElement) &&
2146-
shouldRefocusComposerOnWindowFocus(document.activeElement)
2146+
shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())
21472147
) {
21482148
focusComposerStable();
21492149
}

0 commit comments

Comments
 (0)