Skip to content

Commit b170c2c

Browse files
authored
feat(ui): grid picker for session icons (#124335)
* feat(ui): add session icon grid picker * fix(ui): use radio semantics for icon grid * fix(ui): use pressed-state action-grid semantics for the icon picker ClawSweeper: menuitemradio/radio semantics promise arrow-key selection, but arrows here only move focus (selection persists server-side, so select-on-focus would patch per keypress). Plain buttons with aria-pressed match the click/Enter-only contract.
1 parent 1b95467 commit b170c2c

3 files changed

Lines changed: 234 additions & 49 deletions

File tree

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

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ function menuItem(menu: ParentNode, label: string): SessionMenuItem {
116116
return item;
117117
}
118118

119+
function iconChoices(menu: ParentNode): HTMLButtonElement[] {
120+
return Array.from(menu.querySelectorAll<HTMLButtonElement>(".session-menu__icon-choice"));
121+
}
122+
119123
describe("session menu", () => {
120124
it("disables only denied mutation actions and ignores forced selection", async () => {
121125
const onAction = vi.fn<(action: SessionMenuAction) => void>();
@@ -292,36 +296,72 @@ describe("session menu", () => {
292296
expect(onAction).toHaveBeenCalledWith({ kind: "new-group" });
293297
});
294298

295-
it("offers emoji-only icon choices, marks the current icon, and dispatches set and remove", async () => {
299+
it("renders the icon grid, marks the current icon, and dispatches set and remove", async () => {
296300
const onAction = vi.fn<(action: SessionMenuAction) => void>();
297301
const menu = await mountMenu({ session: { icon: "🦞" }, onAction });
298302
const submenu = menuItem(menu, "Set icon");
299303
(submenu as SessionMenuItem & { submenuOpen: boolean }).submenuOpen = true;
300304

301-
expect(menuItemLabels(submenu)).toEqual([
305+
const choices = iconChoices(submenu);
306+
expect(submenu.querySelector(".session-menu__icon-grid")?.getAttribute("role")).toBe("group");
307+
expect(choices.map((choice) => choice.textContent?.trim())).toEqual([
302308
"🦞",
303309
"🚀",
304310
"🐛",
305311
"✅",
306312
"🔥",
313+
"📦",
314+
"🧪",
307315
"📝",
316+
"🔍",
317+
"⚡",
318+
"🎯",
308319
"⭐",
309-
"📦",
310-
"Remove icon",
311320
]);
312-
const current = menuItem(submenu, "🦞");
313-
await current.updateComplete;
314-
await Promise.resolve();
315-
expect(current.getAttribute("role")).toBe("menuitemradio");
316-
expect(current.getAttribute("aria-checked")).toBe("true");
317-
expect(current.querySelector(".session-menu__check")).not.toBeNull();
318-
319-
menuItem(submenu, "🚀").click();
321+
const current = choices[0];
322+
if (!current) {
323+
throw new Error("Expected the first icon choice");
324+
}
325+
// Click/Enter-only activation: pressed-state action grid, not radio semantics,
326+
// so arrow keys may move focus without changing the persisted selection.
327+
expect(current.getAttribute("role")).toBeNull();
328+
expect(current.getAttribute("aria-pressed")).toBe("true");
329+
expect(choices.filter((choice) => choice.tabIndex === 0)).toEqual([current]);
330+
331+
choices[1]?.click();
320332
expect(onAction).toHaveBeenCalledWith({ kind: "set-icon", icon: "🚀" });
321-
menuItem(submenu, "Remove icon").click();
333+
const remove = submenu.querySelector<HTMLButtonElement>(".session-menu__icon-remove");
334+
expect(remove?.textContent?.trim()).toBe("Remove icon");
335+
remove?.click();
322336
expect(onAction).toHaveBeenCalledWith({ kind: "set-icon", icon: null });
323337
});
324338

339+
it("only renders Remove icon for a session with an icon", async () => {
340+
const menu = await mountMenu();
341+
const submenu = menuItem(menu, "Set icon");
342+
343+
expect(iconChoices(submenu)).toHaveLength(12);
344+
expect(submenu.querySelector(".session-menu__icon-separator")).toBeNull();
345+
expect(submenu.querySelector(".session-menu__icon-remove")).toBeNull();
346+
});
347+
348+
it("moves icon-grid focus by cell and row with arrow keys", async () => {
349+
const menu = await mountMenu({ session: { icon: "🚀" } });
350+
const submenu = menuItem(menu, "Set icon");
351+
const choices = iconChoices(submenu);
352+
choices[1]?.focus();
353+
354+
choices[1]?.dispatchEvent(
355+
new KeyboardEvent("keydown", { key: "ArrowRight", bubbles: true, cancelable: true }),
356+
);
357+
expect(document.activeElement).toBe(choices[2]);
358+
choices[2]?.dispatchEvent(
359+
new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true, cancelable: true }),
360+
);
361+
expect(document.activeElement).toBe(choices[8]);
362+
expect(choices.filter((choice) => choice.tabIndex === 0)).toEqual([choices[8]]);
363+
});
364+
325365
it("omits Remove from group when the session has no category", async () => {
326366
const menu = await mountMenu({ groups: ["Research"] });
327367

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

Lines changed: 112 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,21 @@ const EMPTY_SESSION: SessionMenuData = {
5959
categoryClearReturnsToGroups: false,
6060
};
6161

62-
const SESSION_ICON_CHOICES = ["🦞", "🚀", "🐛", "✅", "🔥", "📝", "⭐", "📦"] as const;
62+
const SESSION_ICON_CHOICES = [
63+
"🦞",
64+
"🚀",
65+
"🐛",
66+
"✅",
67+
"🔥",
68+
"📦",
69+
"🧪",
70+
"📝",
71+
"🔍",
72+
"⚡",
73+
"🎯",
74+
"⭐",
75+
] as const;
76+
const SESSION_ICON_GRID_COLUMNS = 6;
6377

6478
class SessionMenu extends OpenClawLightDomElement {
6579
@property({ attribute: false }) session: SessionMenuData = EMPTY_SESSION;
@@ -274,45 +288,106 @@ class SessionMenu extends OpenClawLightDomElement {
274288

275289
private renderIconSubmenu() {
276290
const currentIcon = this.session.icon;
291+
const tabStop =
292+
SESSION_ICON_CHOICES.find((icon) => icon === currentIcon) ?? SESSION_ICON_CHOICES[0];
277293
return html`
278-
${SESSION_ICON_CHOICES.map((icon) => {
279-
const checked = currentIcon === icon;
280-
return html`
281-
<wa-dropdown-item
282-
slot="submenu"
283-
class="session-menu__item"
284-
value=${`set-icon:${encodeURIComponent(icon)}`}
285-
role="menuitemradio"
286-
aria-checked=${String(checked)}
287-
${ref((element) => syncDropdownItemRadio(element, checked))}
288-
?disabled=${this.actionDisabled("set-icon")}
289-
title=${this.actionTitle("set-icon")}
290-
>
291-
<span class="session-menu__text">${icon}</span>
292-
${checked
293-
? html`<span slot="details" class="session-menu__check" aria-hidden="true"
294-
>${icons.check}</span
295-
>`
296-
: nothing}
297-
</wa-dropdown-item>
298-
`;
299-
})}
300-
${currentIcon
301-
? html`
302-
<wa-dropdown-item
303-
slot="submenu"
304-
class="session-menu__item"
305-
value="set-icon:"
306-
?disabled=${this.actionDisabled("set-icon")}
307-
title=${this.actionTitle("set-icon")}
308-
>
309-
<span class="session-menu__text">${t("sessionsView.removeIcon")}</span>
310-
</wa-dropdown-item>
311-
`
312-
: nothing}
294+
<div slot="submenu" class="session-menu__icon-picker">
295+
<div
296+
class="session-menu__icon-grid"
297+
role="group"
298+
aria-label=${t("sessionsView.setIconMenu")}
299+
@keydown=${this.handleIconGridKeydown}
300+
>
301+
${SESSION_ICON_CHOICES.map((icon) => {
302+
const checked = currentIcon === icon;
303+
return html`
304+
<button
305+
type="button"
306+
class="session-menu__icon-choice"
307+
aria-pressed=${String(checked)}
308+
tabindex=${icon === tabStop ? "0" : "-1"}
309+
?disabled=${this.actionDisabled("set-icon")}
310+
title=${this.actionTitle("set-icon")}
311+
@click=${(event: MouseEvent) => this.selectIcon(event, icon)}
312+
>
313+
${icon}
314+
</button>
315+
`;
316+
})}
317+
</div>
318+
${currentIcon
319+
? html`
320+
<div class="session-menu__icon-separator" role="separator"></div>
321+
<button
322+
type="button"
323+
class="session-menu__icon-remove"
324+
?disabled=${this.actionDisabled("set-icon")}
325+
title=${this.actionTitle("set-icon")}
326+
@click=${this.removeIcon}
327+
>
328+
${t("sessionsView.removeIcon")}
329+
</button>
330+
`
331+
: nothing}
332+
</div>
313333
`;
314334
}
315335

336+
private readonly selectIcon = (event: MouseEvent, icon: string) => {
337+
event.stopPropagation();
338+
this.runAction({ kind: "set-icon", icon });
339+
};
340+
341+
private readonly removeIcon = (event: MouseEvent) => {
342+
event.stopPropagation();
343+
this.runAction({ kind: "set-icon", icon: null });
344+
};
345+
346+
private readonly handleIconGridKeydown = (event: KeyboardEvent) => {
347+
const choice = event.target;
348+
if (!(choice instanceof HTMLButtonElement)) {
349+
return;
350+
}
351+
const offsets: Partial<Record<string, number>> = {
352+
ArrowLeft: -1,
353+
ArrowRight: 1,
354+
ArrowUp: -SESSION_ICON_GRID_COLUMNS,
355+
ArrowDown: SESSION_ICON_GRID_COLUMNS,
356+
};
357+
const offset = offsets[event.key];
358+
if (offset === undefined) {
359+
return;
360+
}
361+
const grid = event.currentTarget;
362+
if (!(grid instanceof HTMLElement)) {
363+
return;
364+
}
365+
const choices = Array.from(
366+
grid.querySelectorAll<HTMLButtonElement>(".session-menu__icon-choice:not(:disabled)"),
367+
);
368+
const index = choices.indexOf(choice);
369+
if (index < 0) {
370+
return;
371+
}
372+
event.preventDefault();
373+
event.stopPropagation();
374+
const nextIndex = (index + offset + choices.length) % choices.length;
375+
choices.forEach((button, buttonIndex) => {
376+
button.tabIndex = buttonIndex === nextIndex ? 0 : -1;
377+
});
378+
choices[nextIndex]?.focus();
379+
};
380+
381+
private readonly focusIconGridOnOpen = (event: CustomEvent<{ item: HTMLElement }>) => {
382+
const item = event.currentTarget;
383+
if (!(item instanceof HTMLElement) || event.detail.item !== item) {
384+
return;
385+
}
386+
requestAnimationFrame(() => {
387+
item.querySelector<HTMLButtonElement>('.session-menu__icon-choice[tabindex="0"]')?.focus();
388+
});
389+
};
390+
316391
override render() {
317392
const menuWidth = 240;
318393
const menuMaxHeight = 460;
@@ -414,6 +489,7 @@ class SessionMenu extends OpenClawLightDomElement {
414489
aria-keyshortcuts="I"
415490
?disabled=${this.actionDisabled("set-icon")}
416491
title=${this.actionTitle("set-icon")}
492+
@submenu-opening=${this.focusIconGridOnOpen}
417493
>
418494
<span slot="icon" class="session-menu__icon" aria-hidden="true">${icons.star}</span>
419495
<span class="session-menu__text">${t("sessionsView.setIconMenu")}</span>

‎ui/src/styles/layout.css‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2814,6 +2814,75 @@ wa-dropdown-item.session-menu__item::part(submenu-icon) {
28142814
border-top: 1px solid color-mix(in srgb, var(--border) 80%, transparent);
28152815
}
28162816

2817+
.session-menu__icon-picker {
2818+
padding: 2px;
2819+
}
2820+
2821+
.session-menu__icon-grid {
2822+
display: grid;
2823+
grid-template-columns: repeat(6, 28px);
2824+
gap: 3px;
2825+
}
2826+
2827+
.session-menu__icon-choice {
2828+
display: inline-flex;
2829+
align-items: center;
2830+
justify-content: center;
2831+
width: 28px;
2832+
height: 28px;
2833+
padding: 0;
2834+
border: 0;
2835+
border-radius: var(--menu-item-radius);
2836+
background: transparent;
2837+
cursor: var(--cursor-action);
2838+
font-size: 17px;
2839+
line-height: 1;
2840+
}
2841+
2842+
.session-menu__icon-choice:hover,
2843+
.session-menu__icon-choice:focus-visible {
2844+
background: var(--bg-hover);
2845+
}
2846+
2847+
.session-menu__icon-choice:focus-visible {
2848+
outline: 2px solid color-mix(in srgb, var(--accent) 48%, transparent);
2849+
outline-offset: -2px;
2850+
}
2851+
2852+
.session-menu__icon-choice[aria-pressed="true"] {
2853+
background: color-mix(in srgb, var(--accent) 14%, transparent);
2854+
box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 55%, transparent);
2855+
}
2856+
2857+
.session-menu__icon-separator {
2858+
margin: 5px 4px 3px;
2859+
border-top: 1px solid color-mix(in srgb, var(--border) 80%, transparent);
2860+
}
2861+
2862+
.session-menu__icon-remove {
2863+
width: 100%;
2864+
min-height: 28px;
2865+
padding: 0 7px;
2866+
border: 0;
2867+
border-radius: var(--menu-item-radius);
2868+
background: transparent;
2869+
color: var(--text);
2870+
cursor: var(--cursor-action);
2871+
font: inherit;
2872+
font-size: calc(13px * var(--control-ui-text-scale));
2873+
text-align: left;
2874+
}
2875+
2876+
.session-menu__icon-remove:hover,
2877+
.session-menu__icon-remove:focus-visible {
2878+
background: var(--bg-hover);
2879+
}
2880+
2881+
.session-menu__icon-remove:focus-visible {
2882+
outline: 2px solid color-mix(in srgb, var(--accent) 48%, transparent);
2883+
outline-offset: -2px;
2884+
}
2885+
28172886
/* Nav rail entries are anchors (for middle-click/new-tab) but read as app
28182887
chrome: override the UA link pointer back to the default arrow. */
28192888
.nav-item {

0 commit comments

Comments
 (0)