Skip to content

Commit 92c639c

Browse files
committed
fix(web): leave focus alone when a human click interrupts the agent's click
1 parent 08186e3 commit 92c639c

2 files changed

Lines changed: 19 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,19 @@ describe("keepHostFocusDuringGuestInput", () => {
112112
expect(otherWebview.blur).not.toHaveBeenCalled();
113113
});
114114

115-
it("restores focus when the click fails and rethrows the failure", async () => {
115+
it("keeps focus in the page when a human click interrupts the agent's click", async () => {
116116
const composer = element("TEXTAREA");
117117
const webview = element("WEBVIEW", TAB);
118118
const focus = focusTracker(composer.node);
119119

120120
await expect(
121121
keepHostFocusDuringGuestInput(TAB, focus.read, async () => {
122122
focus.move(webview.node);
123-
throw new Error("click failed");
123+
throw new Error("PreviewAutomationControlInterruptedError");
124124
}),
125-
).rejects.toThrow("click failed");
125+
).rejects.toThrow("PreviewAutomationControlInterruptedError");
126126

127-
expect(composer.focus).toHaveBeenCalledWith({ preventScroll: true });
127+
expect(composer.focus).not.toHaveBeenCalled();
128+
expect(webview.blur).not.toHaveBeenCalled();
128129
});
129130
});

‎apps/web/src/components/preview/previewHostFocus.ts‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ export async function keepHostFocusDuringGuestInput<A>(
1313
action: () => Promise<A>,
1414
): Promise<A> {
1515
const previous = readActiveElement();
16-
try {
17-
return await action();
18-
} finally {
19-
const current = readActiveElement();
20-
if (
21-
current !== null &&
22-
current !== previous &&
23-
current.tagName === "WEBVIEW" &&
24-
current.getAttribute("data-preview-tab") === runtimeTabId
25-
) {
26-
if (previous !== null && previous.isConnected && previous.tagName !== "BODY") {
27-
previous.focus({ preventScroll: true });
28-
} else {
29-
current.blur();
30-
}
16+
// A human click in the page interrupts the agent's action, so only a completed action restores focus.
17+
const result = await action();
18+
const current = readActiveElement();
19+
if (
20+
current !== null &&
21+
current !== previous &&
22+
current.tagName === "WEBVIEW" &&
23+
current.getAttribute("data-preview-tab") === runtimeTabId
24+
) {
25+
if (previous !== null && previous.isConnected && previous.tagName !== "BODY") {
26+
previous.focus({ preventScroll: true });
27+
} else {
28+
current.blur();
3129
}
3230
}
31+
return result;
3332
}

0 commit comments

Comments
 (0)