The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 09:45 UTC
fix(web): returning to the window keeps a text selection (#31710)
Julius Marminge committed
Nov 16, 2026 at 12:57 UTC
6e650b384906ffa1dd28645a68d5295c06a18af9
3 files changed
+38
-10
apps/web/src/components/ChatView.logic.browser.test.ts
+31
-5
@@ -21,11 +21,33 @@ it("keeps keyboard focus on a sidebar row on window return, but not a clicked ro
21
22
await userEvent.click(clicked);
23
expect(document.activeElement).toBe(clicked);
24
- expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(true);
24
+ expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
25
+ true,
26
+ );
27
28
await userEvent.tab();
29
expect(document.activeElement).toBe(tabbed);
28
- expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
30
+ expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
31
+ false,
32
+ );
33
+});
34
+
35
+it("leaves a text selection in the timeline alone on window return", async () => {
36
+ const timeline = document.createElement("div");
37
+ timeline.tabIndex = -1;
38
+ const reply = document.createElement("p");
39
+ reply.textContent = "Steel bridges are also easy to recycle at the end of their life.";
40
+ timeline.append(reply);
41
+ document.body.append(timeline);
42
+ using _timeline = { [Symbol.dispose]: () => timeline.remove() };
43
+
44
+ await userEvent.tripleClick(reply);
45
+ expect(document.activeElement).toBe(timeline);
46
+ expect(document.getSelection()?.toString()).toContain("Steel bridges");
47
+ expect(
48
+ shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()),
49
+ "refocusing the composer would clear the selected reply text",
50
+ ).toBe(false);
51
});
52
53
it("keeps focus in a region with its own keys on window return", async () => {
@@ -38,7 +60,9 @@ it("keeps focus in a region with its own keys on window return", async () => {
60
61
await userEvent.click(region);
62
expect(document.activeElement).toBe(region);
41
- expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
63
+ expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
64
+ false,
65
+ );
66
});
67
68
it("refocuses the composer on window return from a clicked area after a bare key press", async () => {
@@ -61,7 +85,7 @@ it("refocuses the composer on window return from a clicked area after a bare key
85
// A window switch can deliver a lone modifier key without moving focus.
86
await userEvent.keyboard("{Shift}");
87
expect(
64
- shouldRefocusComposerOnWindowFocus(document.activeElement),
88
+ shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection()),
89
"a clicked area counts as reached from the keyboard after a bare key press",
90
).toBe(true);
91
@@ -70,5 +94,7 @@ it("refocuses the composer on window return from a clicked area after a bare key
94
await userEvent.keyboard("{F6}");
95
controller.move(1);
96
expect(document.activeElement).toBe(conversation);
73
- expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
97
+ expect(shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())).toBe(
98
+ false,
99
+ );
100
});
apps/web/src/components/ChatView.logic.ts
+6
-4
@@ -1096,15 +1096,17 @@ export function hasServerAcknowledgedLocalDispatch(input: {
1096
);
1097
}
1098
1099
-// Window return puts the caret in the composer unless focus is deliberate: another text field,
1100
-// a terminal, an open dialog, menu, or popup, a region, timeline, or sidebar row reached from the
1101
-// keyboard, or a region with its own keys (aria-keyshortcuts). A plain focused button or a clicked
1102
-// row yields to the composer.
1099
+// Window return puts the caret in the composer unless focus or a selection is deliberate: another
1100
+// text field, a terminal, an open dialog, menu, or popup, a region, timeline, or sidebar row reached
1101
+// from the keyboard, a region with its own keys (aria-keyshortcuts), or selected text the composer
1102
+// would clear. A plain focused button or a clicked row yields to the composer.
1103
export function shouldRefocusComposerOnWindowFocus(
1104
activeElement:
1105
| (Pick<Element, "tagName" | "closest" | "getAttribute"> & { isContentEditable?: boolean })
1106
| null,
1107
+ selection: Pick<Selection, "isCollapsed"> | null,
1108
): boolean {
1109
+ if (selection !== null && !selection.isCollapsed) return false;
1110
if (activeElement === null || activeElement.tagName === "BODY") return true;
1111
if (
1112
activeElement.tagName === "INPUT" ||
apps/web/src/components/ChatView.tsx
+1
-1
@@ -2192,7 +2192,7 @@ export default function ChatView(props: ChatViewProps) {
2192
if (document.querySelector("[data-mcp-app-fullscreen]") !== null) return;
2193
if (
2194
!isInSideChatPane(document.activeElement) &&
2195
- shouldRefocusComposerOnWindowFocus(document.activeElement)
2195
+ shouldRefocusComposerOnWindowFocus(document.activeElement, document.getSelection())
2196
) {
2197
focusComposerStable();
2198
}