Skip to content

Commit 6e4711d

Browse files
t3dotggjuliusmarmingeclaude
authored
fix(web): clicked workspace areas no longer show the focus ring after a stray key (#33525)
Co-authored-by: Julius Marminge <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent eb6e7fa commit 6e4711d

5 files changed

Lines changed: 117 additions & 4 deletions

File tree

‎apps/web/src/components/ChatView.logic.browser.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect, it } from "vite-plus/test";
22
import { userEvent } from "vite-plus/test/browser";
3+
import { createWorkspaceFocusController } from "../lib/workspaceFocus";
34
import { shouldRefocusComposerOnWindowFocus } from "./ChatView.logic";
45

56
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
2627
expect(document.activeElement).toBe(tabbed);
2728
expect(shouldRefocusComposerOnWindowFocus(document.activeElement)).toBe(false);
2829
});
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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,7 @@ export function shouldRefocusComposerOnWindowFocus(
11011101
}
11021102
return (
11031103
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]',
11051105
) === null
11061106
);
11071107
}

‎apps/web/src/index.css‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2577,8 +2577,12 @@ code {
25772577
}
25782578
}
25792579

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. */
25812582
[data-workspace-area]:focus-visible {
2583+
outline: none;
2584+
}
2585+
[data-workspace-keyboard-focus]:focus {
25822586
outline: 2px solid var(--ring);
25832587
outline-offset: -2px;
25842588
}

‎apps/web/src/lib/workspaceFocus.browser.test.ts‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
2+
import { userEvent } from "vite-plus/test/browser";
3+
import appCss from "~/index.css?inline";
24
import { createWorkspaceFocusController } from "./workspaceFocus";
35

46
function setup() {
@@ -288,4 +290,53 @@ describe("workspace area focus", () => {
288290
expect(fixture.controller.move(1)).toBe(true);
289291
expect(document.activeElement).toBe(terminal.button);
290292
});
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+
});
291342
});

‎apps/web/src/lib/workspaceFocus.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,28 @@ function revealInScrollers(target: HTMLElement) {
9090
export function createWorkspaceFocusController(doc: Document) {
9191
const lastFocused = new Map<string, HTMLElement>();
9292
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+
};
93106
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;
94111
if (!(event.target instanceof HTMLElement)) return;
95112
const area = workspaceArea(event.target);
96113
if (!area?.dataset.workspaceArea) return;
114+
if (arrived && event.target === area) markKeyboardArea(area);
97115
lastArea = area.dataset.workspaceArea;
98116
const key = area.dataset.workspaceFocusKey ?? lastArea;
99117
lastFocused.delete(key);
@@ -162,6 +180,8 @@ export function createWorkspaceFocusController(doc: Document) {
162180
const active = root instanceof ShadowRoot ? root.activeElement : doc.activeElement;
163181
if (active === target || (active && target.contains(active))) {
164182
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);
165185
return true;
166186
}
167187
}
@@ -308,7 +328,11 @@ export function createWorkspaceFocusController(doc: Document) {
308328
const onUserInput = () => {
309329
if (cancelOnInput) cancelRequest();
310330
};
311-
doc.addEventListener("pointerdown", onUserInput, true);
331+
const onPointerDown = () => {
332+
unmarkKeyboardArea();
333+
onUserInput();
334+
};
335+
doc.addEventListener("pointerdown", onPointerDown, true);
312336
doc.addEventListener("keydown", onUserInput, true);
313337

314338
return {
@@ -320,9 +344,10 @@ export function createWorkspaceFocusController(doc: Document) {
320344
request,
321345
dispose: () => {
322346
cancelRequest();
347+
unmarkKeyboardArea();
323348
doc.removeEventListener("focusin", remember);
324349
doc.removeEventListener("focusout", recover);
325-
doc.removeEventListener("pointerdown", onUserInput, true);
350+
doc.removeEventListener("pointerdown", onPointerDown, true);
326351
doc.removeEventListener("keydown", onUserInput, true);
327352
},
328353
};

0 commit comments

Comments
 (0)