Skip to content

Commit de51d71

Browse files
fix(ui): preserve agent menu keyboard navigation
1 parent 590d3c4 commit de51d71

2 files changed

Lines changed: 47 additions & 9 deletions

File tree

‎ui/src/components/app-sidebar-agent-menu.ts‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ const AGENT_VALUE_PREFIX = "agent:";
5252
const COMMAND_VALUE_PREFIX = "command:";
5353
const LINK_VALUE_PREFIX = "link:";
5454

55+
function sidebarMenuItems(dropdown: Element | null) {
56+
return [
57+
...(dropdown?.querySelectorAll<HTMLElement & { active: boolean }>(
58+
":scope > wa-dropdown-item:not([disabled]), :scope > .sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])",
59+
) ?? []),
60+
];
61+
}
62+
5563
// Nested overlays bubble lifecycle events through the dropdown. Only the
5664
// owner's completed hide may remove its menu or consume its Escape state.
5765
function closeMenuAfterOwnDropdownHide(event: Event, onClose: (restoreFocus?: boolean) => void) {
@@ -66,11 +74,7 @@ function moveSidebarMenuFocus(event: KeyboardEvent): boolean {
6674
return false;
6775
}
6876
const dropdown = (event.currentTarget as HTMLElement).closest("wa-dropdown");
69-
const items = [
70-
...(dropdown?.querySelectorAll<HTMLElement & { active: boolean }>(
71-
":scope > wa-dropdown-item:not([disabled]), :scope > .sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])",
72-
) ?? []),
73-
];
77+
const items = sidebarMenuItems(dropdown);
7478
const footer = dropdown?.querySelector<HTMLElement>(".sidebar-identity-menu__footer");
7579
const controls = [
7680
...items,
@@ -98,6 +102,18 @@ function moveSidebarMenuFocus(event: KeyboardEvent): boolean {
98102
return true;
99103
}
100104

105+
function focusActiveAgentMenuItem(dropdown: HTMLElement) {
106+
const items = sidebarMenuItems(dropdown);
107+
const target =
108+
items.find((item) => item.classList.contains("sidebar-agent-menu__agent-switch--active")) ??
109+
items.find((item) => item.classList.contains("sidebar-agent-menu__agent-switch"));
110+
if (!target) {
111+
return;
112+
}
113+
items.forEach((item) => (item.active = item === target));
114+
target.focus({ preventScroll: true });
115+
}
116+
101117
type AgentMenuAgent = {
102118
id: string;
103119
name?: string;
@@ -333,10 +349,28 @@ export function renderSidebarAgentMenu(params: SidebarAgentMenuParams) {
333349
(event.currentTarget as HTMLElement)
334350
.querySelector<HTMLInputElement>(".sidebar-agent-menu__filter input")
335351
?.focus();
352+
} else {
353+
focusActiveAgentMenuItem(event.currentTarget as HTMLElement);
354+
}
355+
}}
356+
@keydown=${(event: KeyboardEvent) => {
357+
if (moveSidebarMenuFocus(event)) {
358+
return;
359+
}
360+
const item =
361+
event.target instanceof HTMLElement
362+
? event.target.closest<HTMLElement>(
363+
".sidebar-agent-menu__agent-grid > wa-dropdown-item:not([disabled])",
364+
)
365+
: null;
366+
if ((event.key === "Enter" || event.key === " ") && item) {
367+
event.preventDefault();
368+
event.stopPropagation();
369+
item.click();
370+
return;
336371
}
372+
trackDropdownKeyboardDismissal(event, params.onTabAway);
337373
}}
338-
@keydown=${(event: KeyboardEvent) =>
339-
trackDropdownKeyboardDismissal(event, params.onTabAway)}
340374
@wa-after-hide=${(event: Event) => closeMenuAfterOwnDropdownHide(event, params.onClose)}
341375
>
342376
<button

‎ui/src/e2e/sidebar-interactions.e2e.test.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ suite.define(() => {
194194
});
195195
const page = await context.newPage();
196196
const agentsList = {
197-
agents: [{ id: "main" }, { id: "research" }],
197+
agents: [{ id: "main" }, { id: "research" }, { id: "forge" }],
198198
defaultId: "main",
199199
mainKey: "main",
200200
scope: "agent",
@@ -216,6 +216,10 @@ suite.define(() => {
216216
name: "Research",
217217
},
218218
},
219+
{
220+
match: { agentId: "forge" },
221+
response: { agentId: "forge", avatar: "", emoji: "🔧", name: "Forge" },
222+
},
219223
],
220224
},
221225
"agents.list": agentsList,
@@ -239,7 +243,7 @@ suite.define(() => {
239243
await expect
240244
.poll(() =>
241245
researchSwitch.evaluate(
242-
(element) => element.parentElement?.matches("wa-dropdown.sidebar-agent-menu") ?? false,
246+
(element) => element.parentElement?.matches(".sidebar-agent-menu__agent-grid") ?? false,
243247
),
244248
)
245249
.toBe(true);

0 commit comments

Comments
 (0)