Skip to content
TypeScript

fix(web): the device screen keeps focus when typing or returning to the window (#14534)

hayden0c609ab87d60
diff --git a/apps/web/src/components/ChatView.logic.test.ts b/apps/web/src/components/ChatView.logic.test.ts
index 6889515e75b..b4cc66e2b2e 100644
--- a/apps/web/src/components/ChatView.logic.test.ts
+++ b/apps/web/src/components/ChatView.logic.test.ts
@@ -1953,6 +1953,12 @@ describe("shouldRefocusComposerOnWindowFocus", () => {
expect(shouldRefocusComposerOnWindowFocus(element(tagName))).toBe(false);
});
+ it("leaves a focused device screen alone", () => {
+ expect(shouldRefocusComposerOnWindowFocus(element("DIV", { within: "application" }))).toBe(
+ false,
+ );
+ });
+
it("leaves a focused terminal alone in the drawer and the right panel", () => {
expect(
shouldRefocusComposerOnWindowFocus(element("BUTTON", { within: "data-terminal-owner" })),
diff --git a/apps/web/src/components/ChatView.logic.ts b/apps/web/src/components/ChatView.logic.ts
index b2c2d8540fd..f14ce8cf71a 100644
--- a/apps/web/src/components/ChatView.logic.ts
+++ b/apps/web/src/components/ChatView.logic.ts
@@ -1259,7 +1259,7 @@ export function shouldRefocusComposerOnWindowFocus(
}
return (
activeElement.closest(
- '[role="dialog"], [role="alertdialog"], [data-slot$="-popup"], [data-terminal-owner]',
+ '[role="dialog"], [role="alertdialog"], [role="application"], [data-slot$="-popup"], [data-terminal-owner]',
) === null
);
}
diff --git a/apps/web/src/components/chat/composerFocusRedirect.browser.test.ts b/apps/web/src/components/chat/composerFocusRedirect.browser.test.ts
new file mode 100644
index 00000000000..d0e0f78dce5
--- /dev/null
+++ b/apps/web/src/components/chat/composerFocusRedirect.browser.test.ts
@@ -0,0 +1,38 @@
+import { afterEach, describe, expect, it } from "vite-plus/test";
+
+import { shouldTypeToFocusComposer } from "./composerFocusRedirect";
+
+// composedPath() is only populated while the event is dispatching, so decide
+// inside a window capture listener the way the chat view does.
+function typeOn(target: Element, key: string): boolean {
+ let redirected = false;
+ const listener = (event: KeyboardEvent) => {
+ redirected = shouldTypeToFocusComposer(event);
+ };
+ window.addEventListener("keydown", listener, { capture: true });
+ target.dispatchEvent(new KeyboardEvent("keydown", { key, bubbles: true, composed: true }));
+ window.removeEventListener("keydown", listener, { capture: true });
+ return redirected;
+}
+
+afterEach(() => {
+ document.body.replaceChildren();
+});
+
+describe("shouldTypeToFocusComposer", () => {
+ it("sends letters typed on the chat to the composer", () => {
+ const chat = document.createElement("div");
+ document.body.append(chat);
+
+ expect(typeOn(chat, "a")).toBe(true);
+ });
+
+ it("keeps letters typed on the device screen on the device", () => {
+ const deviceScreen = document.createElement("div");
+ deviceScreen.setAttribute("role", "application");
+ deviceScreen.tabIndex = 0;
+ document.body.append(deviceScreen);
+
+ expect(typeOn(deviceScreen, "a")).toBe(false);
+ });
+});
diff --git a/apps/web/src/components/chat/composerFocusRedirect.ts b/apps/web/src/components/chat/composerFocusRedirect.ts
index e7937779a76..2bdd6834239 100644
--- a/apps/web/src/components/chat/composerFocusRedirect.ts
+++ b/apps/web/src/components/chat/composerFocusRedirect.ts
@@ -13,6 +13,7 @@ const TYPE_TO_FOCUS_INTERACTIVE_SELECTOR = [
"button",
"a[href]",
"summary",
+ '[role="application"]',
'[role="button"]',
'[role="checkbox"]',
'[role="menuitem"]',