Skip to content

Commit 728456d

Browse files
t3dotggclaude
andcommitted
fix(preview): overlapping agent clicks still give focus back
A click that started while another was in flight saw the page that click focused as "what had focus", so the last click to finish could hand focus to the first page. Overlapping clicks now share one window: the first remembers the host focus and the last to finish restores it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 2c6553c commit 728456d

2 files changed

Lines changed: 63 additions & 14 deletions

File tree

‎apps/web/src/components/preview/previewClickFocus.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ describe("runPreviewClickKeepingHostFocus", () => {
4949
expect(document.activeElement).toBe(composer);
5050
});
5151

52+
it("gives focus back once overlapping clicks in two tabs finish", async () => {
53+
const composer = mount("textarea");
54+
const first = mount("webview", TAB);
55+
const second = mount("webview", "other-tab");
56+
composer.focus();
57+
let finishSecond = () => {};
58+
59+
const firstClick = runPreviewClickKeepingHostFocus(TAB, async () => {
60+
first.focus();
61+
});
62+
// Starts while the first click holds focus in its page, and finishes last.
63+
const secondClick = runPreviewClickKeepingHostFocus("other-tab", async () => {
64+
second.focus();
65+
await new Promise<void>((resolve) => {
66+
finishSecond = resolve;
67+
});
68+
});
69+
await firstClick;
70+
finishSecond();
71+
await secondClick;
72+
73+
expect(document.activeElement).toBe(composer);
74+
});
75+
5276
it("leaves no focus in the page when nothing in the app had focus", async () => {
5377
const webview = mount("webview", TAB);
5478

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
1-
const isPreviewWebview = (element: Element, runtimeTabId: string): boolean =>
2-
element.localName === "webview" && element.getAttribute("data-preview-tab") === runtimeTabId;
1+
interface PendingClicks {
2+
readonly previous: Element | null;
3+
readonly runtimeTabIds: Set<string>;
4+
count: number;
5+
}
6+
7+
// Agent clicks can overlap across tabs and threads. A click that starts while
8+
// another is in flight could see the page that click focused as "what had
9+
// focus", so overlapping clicks share one window: the first remembers the host
10+
// focus and the last to finish restores it.
11+
let pendingClicks: PendingClicks | null = null;
12+
13+
const restoreHostFocus = ({ previous, runtimeTabIds }: PendingClicks): void => {
14+
const current = document.activeElement;
15+
if (
16+
!(current instanceof HTMLElement) ||
17+
current === previous ||
18+
current.localName !== "webview" ||
19+
!runtimeTabIds.has(current.getAttribute("data-preview-tab") ?? "")
20+
) {
21+
return;
22+
}
23+
if (previous instanceof HTMLElement && previous.isConnected && previous !== document.body) {
24+
previous.focus({ preventScroll: true });
25+
} else {
26+
current.blur();
27+
}
28+
};
329

430
/**
531
* Runs an agent click in a preview tab and hands keyboard focus back to the
@@ -15,23 +41,22 @@ export async function runPreviewClickKeepingHostFocus<A>(
1541
runtimeTabId: string,
1642
click: () => Promise<A>,
1743
): Promise<A> {
18-
const previous = document.activeElement;
44+
const pending = (pendingClicks ??= {
45+
previous: document.activeElement,
46+
runtimeTabIds: new Set(),
47+
count: 0,
48+
});
49+
pending.count += 1;
50+
pending.runtimeTabIds.add(runtimeTabId);
1951
try {
2052
return await click();
2153
} finally {
2254
// Also runs when the click fails: a keystroke that reaches the page after
2355
// the press counts as human input and fails the click as interrupted.
24-
const current = document.activeElement;
25-
if (
26-
current instanceof HTMLElement &&
27-
current !== previous &&
28-
isPreviewWebview(current, runtimeTabId)
29-
) {
30-
if (previous instanceof HTMLElement && previous.isConnected && previous !== document.body) {
31-
previous.focus({ preventScroll: true });
32-
} else {
33-
current.blur();
34-
}
56+
pending.count -= 1;
57+
if (pending.count === 0) {
58+
pendingClicks = null;
59+
restoreHostFocus(pending);
3560
}
3661
}
3762
}

0 commit comments

Comments
 (0)