@devries-cameron20 / t3code Mirror · read-only / commits / 6e4711d376
Fork 0

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 22, 00:45 UTC

fix(web): clicked workspace areas no longer show the focus ring after a stray key (#33525)

Co-authored-by: Julius Marminge <julius0216@outlook.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Theo Browne committed Nov 9, 2026 at 12:11 UTC 6e4711d37690e8cc7225fc1a66011f4b1840d095
5 files changed +117 -4
apps/web/src/components/ChatView.logic.browser.test.ts
+33
@@ -1,5 +1,6 @@
1 import { expect, it } from "vite-plus/test";
2 import { userEvent } from "vite-plus/test/browser";
3 +import { createWorkspaceFocusController } from "../lib/workspaceFocus";
4 import { shouldRefocusComposerOnWindowFocus } from "./ChatView.logic";
5
6 it("keeps keyboard focus on a sidebar row on window return, but not a clicked row", async () => {
@@ -26,3 +27,35 @@ it("keeps keyboard focus on a sidebar row on window return, but not a clicked ro
27 expect(document.activeElement).toBe(tabbed);
28 expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
29 });
30 +
31 +it("refocuses the composer on window return from a clicked area after a bare key press", async () => {
32 + const controller = createWorkspaceFocusController(document);
33 + const root = document.createElement("div");
34 + root.innerHTML =
35 + '<nav data-workspace-area="sidebar" data-workspace-order="0" tabindex="-1"><button>Thread</button></nav>' +
36 + '<section data-workspace-area="conversation" data-workspace-order="1" tabindex="-1" style="height: 200px"></section>';
37 + document.body.append(root);
38 + using _root = {
39 + [Symbol.dispose]: () => {
40 + controller.dispose();
41 + root.remove();
42 + },
43 + };
44 + const conversation = root.querySelector("section")!;
45 +
46 + await userEvent.click(conversation);
47 + expect(document.activeElement).toBe(conversation);
48 + // A window switch can deliver a lone modifier key without moving focus.
49 + await userEvent.keyboard("{Shift}");
50 + expect(
51 + shouldRefocusComposerOnWindowFocus(document.activeElement),
52 + "a clicked area counts as reached from the keyboard after a bare key press",
53 + ).toBe(true);
54 +
55 + await userEvent.keyboard("{F6}");
56 + controller.move(-1);
57 + await userEvent.keyboard("{F6}");
58 + controller.move(1);
59 + expect(document.activeElement).toBe(conversation);
60 + expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
61 +});
apps/web/src/components/ChatView.logic.ts
+1 -1
@@ -1101,7 +1101,7 @@ export function shouldRefocusComposerOnWindowFocus(
1101 }
1102 return (
1103 activeElement.closest(
1104 - '[data-workspace-area]:focus-visible, [data-sidebar-row]:focus-visible, [role="dialog"], [role="alertdialog"], [role="application"], [role="menu"], [role="listbox"], [data-slot$="-popup"], [data-terminal-owner]',
1104 + '[data-workspace-keyboard-focus], [data-sidebar-row]:focus-visible, [role="dialog"], [role="alertdialog"], [role="application"], [role="menu"], [role="listbox"], [data-slot$="-popup"], [data-terminal-owner]',
1105 ) === null
1106 );
1107 }
apps/web/src/index.css
+5 -1
@@ -2577,8 +2577,12 @@ code {
2577 }
2578 }
2579
2580 -/* A workspace area with no control of its own takes F6 focus itself; keep that visible. */
2580 +/* A workspace area with no control of its own takes F6 focus itself; keep that visible.
2581 + workspaceFocus.ts marks those, since :focus-visible also matches a clicked area after a key press. */
2582 [data-workspace-area]:focus-visible {
2583 + outline: none;
2584 +}
2585 +[data-workspace-keyboard-focus]:focus {
2586 outline: 2px solid var(--ring);
2587 outline-offset: -2px;
2588 }
apps/web/src/lib/workspaceFocus.browser.test.ts
+51
@@ -1,4 +1,6 @@
1 import { describe, expect, it, vi } from "vite-plus/test";
2 +import { userEvent } from "vite-plus/test/browser";
3 +import appCss from "~/index.css?inline";
4 import { createWorkspaceFocusController } from "./workspaceFocus";
5
6 function setup() {
@@ -288,4 +290,53 @@ describe("workspace area focus", () => {
290 expect(fixture.controller.move(1)).toBe(true);
291 expect(document.activeElement).toBe(terminal.button);
292 });
293 +
294 + it("rings an area reached with F6, but not a clicked one after a bare key press", async () => {
295 + using fixture = setup();
296 + const style = document.createElement("style");
297 + style.textContent = appCss;
298 + document.head.append(style);
299 + using _style = { [Symbol.dispose]: () => style.remove() };
300 + const sidebar = fixture.area("sidebar", 0);
301 + const home = fixture.append(
302 + '<main data-workspace-area="home" data-workspace-order="1" tabindex="-1" style="height: 200px"></main>',
303 + );
304 + const outline = () => getComputedStyle(home).outlineStyle;
305 +
306 + await userEvent.click(home);
307 + expect(document.activeElement).toBe(home);
308 + expect(outline()).toBe("none");
309 + // A window switch can deliver a lone modifier key without moving focus.
310 + await userEvent.keyboard("{Shift}");
311 + expect(outline(), "a clicked area shows the focus ring after a bare key press").toBe("none");
312 + // Window activation refocuses the element that had focus, with nothing focused in between.
313 + home.blur();
314 + home.focus();
315 + expect(outline(), "a clicked area rings when the window regains focus").toBe("none");
316 +
317 + await userEvent.keyboard("{F6}");
318 + fixture.controller.move(-1);
319 + expect(document.activeElement).toBe(sidebar.button);
320 + await userEvent.keyboard("{F6}");
321 + fixture.controller.move(1);
322 + expect(document.activeElement).toBe(home);
323 + expect(outline()).toBe("solid");
324 +
325 + // A dialog opened from the keyboard returns focus to the area when it closes.
326 + await userEvent.keyboard("{Enter}");
327 + sidebar.button.focus();
328 + await userEvent.keyboard("{Escape}");
329 + home.focus();
330 + expect(outline(), "an area loses its ring when keyboard focus returns to it").toBe("solid");
331 +
332 + await userEvent.click(home);
333 + expect(outline(), "clicking the ringed area removes its ring").toBe("none");
334 +
335 + // With no other area visible, F6 lands on the area that already has focus.
336 + sidebar.element.hidden = true;
337 + await userEvent.keyboard("{F6}");
338 + fixture.controller.move(1);
339 + expect(document.activeElement).toBe(home);
340 + expect(outline(), "F6 onto the only, already focused area shows no ring").toBe("solid");
341 + });
342 });
apps/web/src/lib/workspaceFocus.ts
+27 -2
@@ -90,10 +90,28 @@ function revealInScrollers(target: HTMLElement) {
90 export function createWorkspaceFocusController(doc: Document) {
91 const lastFocused = new Map<string, HTMLElement>();
92 let lastArea: string | undefined;
93 + // An area that took focus itself from the keyboard. Chromium's :focus-visible also matches
94 + // a clicked area after any later key press, so the ring and window return read this mark.
95 + let keyboardArea: HTMLElement | undefined;
96 + let previousTarget: EventTarget | null = null;
97 + const unmarkKeyboardArea = () => {
98 + keyboardArea?.removeAttribute("data-workspace-keyboard-focus");
99 + keyboardArea = undefined;
100 + };
101 + const markKeyboardArea = (area: HTMLElement) => {
102 + if (!area.matches(":focus-visible")) return;
103 + keyboardArea = area;
104 + area.setAttribute("data-workspace-keyboard-focus", "");
105 + };
106 const remember = (event: FocusEvent) => {
107 + if (event.target !== keyboardArea) unmarkKeyboardArea();
108 + // Window activation refocuses the same element; only focus arriving from elsewhere counts.
109 + const arrived = event.target !== previousTarget;
110 + previousTarget = event.target;
111 if (!(event.target instanceof HTMLElement)) return;
112 const area = workspaceArea(event.target);
113 if (!area?.dataset.workspaceArea) return;
114 + if (arrived && event.target === area) markKeyboardArea(area);
115 lastArea = area.dataset.workspaceArea;
116 const key = area.dataset.workspaceFocusKey ?? lastArea;
117 lastFocused.delete(key);
@@ -162,6 +180,8 @@ export function createWorkspaceFocusController(doc: Document) {
180 const active = root instanceof ShadowRoot ? root.activeElement : doc.activeElement;
181 if (active === target || (active && target.contains(active))) {
182 if (target !== area && !isConversation) revealInScrollers(target);
183 + // Focusing the already focused area fires no focusin, as when F6 has nowhere else to go.
184 + if (target === area) markKeyboardArea(area);
185 return true;
186 }
187 }
@@ -308,7 +328,11 @@ export function createWorkspaceFocusController(doc: Document) {
328 const onUserInput = () => {
329 if (cancelOnInput) cancelRequest();
330 };
311 - doc.addEventListener("pointerdown", onUserInput, true);
331 + const onPointerDown = () => {
332 + unmarkKeyboardArea();
333 + onUserInput();
334 + };
335 + doc.addEventListener("pointerdown", onPointerDown, true);
336 doc.addEventListener("keydown", onUserInput, true);
337
338 return {
@@ -320,9 +344,10 @@ export function createWorkspaceFocusController(doc: Document) {
344 request,
345 dispose: () => {
346 cancelRequest();
347 + unmarkKeyboardArea();
348 doc.removeEventListener("focusin", remember);
349 doc.removeEventListener("focusout", recover);
325 - doc.removeEventListener("pointerdown", onUserInput, true);
350 + doc.removeEventListener("pointerdown", onPointerDown, true);
351 doc.removeEventListener("keydown", onUserInput, true);
352 },
353 };