Skip to content

Commit ad409a8

Browse files
steipetePeter Steinberger
andauthored
fix(ui): preserve session, tool, archive, and focus ownership (#117191)
* fix(ui): unify tool, session, and keyboard interaction owners * refactor(ui): separate archived session regression coverage * test(ui): isolate memory plugin mutation mock registries --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 6922ddd commit ad409a8

24 files changed

Lines changed: 1348 additions & 551 deletions

‎test/vitest/vitest.ui-isolated-paths.mjs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export const uiIsolatedTestFiles = [
1717
"ui/src/pages/chat/chat-pane.session-discussion.test.ts",
1818
"ui/src/pages/chat/chat-pane.test.ts",
1919
"ui/src/pages/chat/components/chat-thread.measure.test.ts",
20+
"ui/src/pages/config/memory-mutation-owner.test.ts",
21+
"ui/src/pages/config/memory-page.test.ts",
2022
"ui/src/pages/workboard/view.test.ts",
2123
];
2224

‎ui/src/components/app-sidebar-session-row-render.ts‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ export function renderRecentSession(params: {
177177
const metaId = hasTrail ? sidebarSessionMetaId(session.key) : undefined;
178178
const menuSession = display ? { ...session, meta } : session;
179179
const title = display?.title ?? [label, narration, rowMeta].filter(Boolean).join(" · ");
180+
const pinLabel = `${t(session.pinned ? "sessionsView.unpinSession" : "sessionsView.pinSession")}: ${label}`;
181+
const menuLabel = `${t("chat.sidebar.openSessionMenu")}: ${label}`;
180182
const rowClass = [
181183
"sidebar-recent-session",
182184
"session-row-host",
@@ -227,7 +229,13 @@ export function renderRecentSession(params: {
227229
? nothing
228230
: (event: MouseEvent) => {
229231
event.preventDefault();
230-
host.sidebarMenus.openSessionMenu(menuSession, event.clientX, event.clientY);
232+
const rowElement = event.currentTarget as HTMLElement;
233+
const trigger =
234+
rowElement.querySelector<HTMLElement>("[data-session-menu]") ??
235+
(event.target instanceof Element
236+
? event.target.closest<HTMLElement>("a, button, [tabindex]")
237+
: null);
238+
host.sidebarMenus.openSessionMenu(menuSession, event.clientX, event.clientY, trigger);
231239
}}
232240
@mouseenter=${(event: MouseEvent) => startHoverMarquee(event.currentTarget as HTMLElement)}
233241
@mouseleave=${(event: MouseEvent) => stopHoverMarquee(event.currentTarget as HTMLElement)}
@@ -336,12 +344,8 @@ export function renderRecentSession(params: {
336344
class="session-action session-action--pin"
337345
data-sidebar-session-pin="true"
338346
type="button"
339-
title=${session.pinned
340-
? t("sessionsView.unpinSession")
341-
: t("sessionsView.pinSession")}
342-
aria-label=${session.pinned
343-
? t("sessionsView.unpinSession")
344-
: t("sessionsView.pinSession")}
347+
title=${pinLabel}
348+
aria-label=${pinLabel}
345349
?disabled=${!host.connected}
346350
@click=${() => host.toggleSessionPin(session)}
347351
>
@@ -351,8 +355,8 @@ export function renderRecentSession(params: {
351355
class="session-action"
352356
data-session-menu="true"
353357
type="button"
354-
title=${t("chat.sidebar.openSessionMenu")}
355-
aria-label=${t("chat.sidebar.openSessionMenu")}
358+
title=${menuLabel}
359+
aria-label=${menuLabel}
356360
aria-haspopup="menu"
357361
aria-expanded=${String(host.sidebarMenus.sessionMenu?.session.key === session.key)}
358362
@click=${(event: MouseEvent) => {

‎ui/src/components/dropdown-menu-controller.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { ReactiveController, ReactiveControllerHost } from "lit";
2+
import { trackDropdownKeyboardDismissal } from "./web-awesome.ts";
23

34
type DropdownMenuHost = ReactiveControllerHost & HTMLElement;
45

@@ -29,6 +30,21 @@ export class DropdownMenuController implements ReactiveController {
2930

3031
private readonly handleDocumentKeydown = (event: KeyboardEvent) => {
3132
if (event.key !== "Escape") {
33+
// Nested dialogs and outside targets own their own Tab order; only menu
34+
// items need the durable trigger before Web Awesome dismisses them.
35+
if (
36+
event.key === "Tab" &&
37+
event
38+
.composedPath()
39+
.some(
40+
(target) =>
41+
target instanceof Element &&
42+
target.localName === "wa-dropdown-item" &&
43+
this.host.contains(target),
44+
)
45+
) {
46+
trackDropdownKeyboardDismissal(event, () => this.options.getTrigger()?.focus());
47+
}
3248
this.options.onKeydown?.(event);
3349
return;
3450
}

‎ui/src/components/hub-tabs.ts‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,32 @@ const PENDING_FOCUS_WINDOW_MS = 2000;
3030
// Web Awesome selects its first tab when `active` is empty. A truthy value that
3131
// matches no panel preserves an intentional no-selection state.
3232
const NO_ACTIVE_TAB = "__openclaw-hub-tabs-no-active__";
33-
let pendingFocus: { hubId: string; tab: string; at: number } | null = null;
33+
let pendingFocus: { hubId: string; tab: string; at: number; source: Element } | null = null;
3434

3535
function reclaimFocus(hubId: string, tab: string, element: Element | undefined) {
3636
if (!element || pendingFocus?.hubId !== hubId || pendingFocus.tab !== tab) {
3737
return;
3838
}
3939
const pending = pendingFocus;
40-
pendingFocus = null;
4140
if (Date.now() - pending.at > PENDING_FOCUS_WINDOW_MS) {
41+
pendingFocus = null;
4242
return;
4343
}
4444
// The ref fires while the strip is still inside Lit's template fragment.
4545
// A task lets both Lit and Web Awesome finish connecting before focus moves.
4646
window.setTimeout(() => {
47-
if (element.isConnected) {
47+
if (pendingFocus !== pending) {
48+
return;
49+
}
50+
pendingFocus = null;
51+
const currentFocus = document.activeElement;
52+
if (
53+
element.isConnected &&
54+
Date.now() - pending.at <= PENDING_FOCUS_WINDOW_MS &&
55+
(currentFocus === pending.source ||
56+
currentFocus === document.body ||
57+
currentFocus === document.documentElement)
58+
) {
4859
(element as HTMLElement).focus();
4960
}
5061
}, 0);
@@ -93,7 +104,12 @@ export function renderHubTabs<T extends string>(props: HubTabsProps<T>): Templat
93104
tab.value !== props.active
94105
) {
95106
event.preventDefault();
96-
pendingFocus = { hubId: props.id, tab: tab.value, at: Date.now() };
107+
pendingFocus = {
108+
hubId: props.id,
109+
tab: tab.value,
110+
at: Date.now(),
111+
source: event.currentTarget as Element,
112+
};
97113
props.onSelect(tab.value);
98114
}
99115
}}

‎ui/src/components/native-link-menu.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,26 @@ describe("native link menu", () => {
137137
expect(document.activeElement).toBe(trigger);
138138
});
139139

140+
it("returns focus to its durable trigger before a Tab leaves the menu", async () => {
141+
const trigger = document.createElement("a");
142+
trigger.href = "https://example.com";
143+
document.body.append(trigger);
144+
containers.push(trigger);
145+
const menu = await mountMenu({ trigger });
146+
const item = menuItems(menu)[0];
147+
item?.focus();
148+
149+
const keydown = new KeyboardEvent("keydown", {
150+
key: "Tab",
151+
bubbles: true,
152+
cancelable: true,
153+
});
154+
item?.dispatchEvent(keydown);
155+
156+
expect(document.activeElement).toBe(trigger);
157+
expect(keydown.defaultPrevented).toBe(false);
158+
});
159+
140160
it("closes after Web Awesome hides without stealing focus", async () => {
141161
const trigger = document.createElement("a");
142162
trigger.href = "https://example.com";

‎ui/src/components/session-menu.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ describe("session menu", () => {
237237

238238
it("opens an accessible icon picker with keyboard grid navigation", async () => {
239239
const menu = await mountMenu();
240+
const dropdown = menu.querySelector("wa-dropdown");
240241

241242
await openIconPicker(menu);
242243

244+
expect(menu.querySelector("wa-dropdown")).toBe(dropdown);
243245
expect(menu.querySelector(".session-menu__icon-picker")?.getAttribute("role")).toBe("dialog");
244246
const choices = Array.from(
245247
menu.querySelectorAll<HTMLButtonElement>(".session-menu__icon-choice"),
@@ -249,6 +251,47 @@ describe("session menu", () => {
249251
expect(document.activeElement).toBe(choices[1]);
250252
});
251253

254+
it("keeps Tab ownership inside the nested icon-picker dialog", async () => {
255+
const trigger = document.createElement("button");
256+
document.body.append(trigger);
257+
containers.push(trigger);
258+
const menu = await mountMenu({ trigger });
259+
260+
await openIconPicker(menu);
261+
262+
const choice = menu.querySelector<HTMLButtonElement>(".session-menu__icon-choice");
263+
expect(document.activeElement).toBe(choice);
264+
choice?.dispatchEvent(
265+
new KeyboardEvent("keydown", { key: "Tab", bubbles: true, cancelable: true }),
266+
);
267+
268+
expect(document.activeElement).toBe(choice);
269+
});
270+
271+
it("preserves an intentionally focused icon-picker field after the dropdown opens", async () => {
272+
const menu = await mountMenu();
273+
await openIconPicker(menu);
274+
const input = menu.querySelector<HTMLInputElement>(".session-menu__emoji-field input");
275+
input?.focus();
276+
277+
menu.querySelector("wa-dropdown")?.dispatchEvent(new CustomEvent("wa-after-show"));
278+
279+
expect(document.activeElement).toBe(input);
280+
});
281+
282+
it("keeps one dropdown and restores its originating item after leaving the icon picker", async () => {
283+
const menu = await mountMenu();
284+
const dropdown = menu.querySelector("wa-dropdown");
285+
await openIconPicker(menu);
286+
287+
menu.querySelector<HTMLButtonElement>(".session-menu__icon-picker-back")?.click();
288+
await menu.updateComplete;
289+
await Promise.resolve();
290+
291+
expect(menu.querySelector("wa-dropdown")).toBe(dropdown);
292+
expect(document.activeElement).toBe(menuItem(menu, "Change icon"));
293+
});
294+
252295
it("opens group actions and dispatches group, removal, and creation choices", async () => {
253296
const onAction = vi.fn<(action: SessionMenuAction) => void>();
254297
const menu = await mountMenu({
@@ -439,6 +482,40 @@ describe("session menu", () => {
439482
expect(onAction).not.toHaveBeenCalled();
440483
});
441484

485+
it("returns focus to its durable trigger before a Tab leaves the menu", async () => {
486+
const trigger = document.createElement("button");
487+
document.body.append(trigger);
488+
containers.push(trigger);
489+
const menu = await mountMenu({ trigger });
490+
const item = menuItem(menu, "Open chat");
491+
item.focus();
492+
493+
const keydown = new KeyboardEvent("keydown", {
494+
key: "Tab",
495+
bubbles: true,
496+
cancelable: true,
497+
});
498+
item.dispatchEvent(keydown);
499+
500+
expect(document.activeElement).toBe(trigger);
501+
expect(keydown.defaultPrevented).toBe(false);
502+
});
503+
504+
it("does not reclaim focus when Tab originates outside the open menu", async () => {
505+
const trigger = document.createElement("button");
506+
const outside = document.createElement("button");
507+
document.body.append(trigger, outside);
508+
containers.push(trigger, outside);
509+
await mountMenu({ trigger });
510+
outside.focus();
511+
512+
outside.dispatchEvent(
513+
new KeyboardEvent("keydown", { key: "Tab", bubbles: true, cancelable: true }),
514+
);
515+
516+
expect(document.activeElement).toBe(outside);
517+
});
518+
442519
it("closes on Escape without leaking the key past the menu", async () => {
443520
const trigger = document.createElement("button");
444521
document.body.append(trigger);

0 commit comments

Comments
 (0)