Skip to content

Commit 414e24c

Browse files
TonybynMp4claude
andcommitted
fix(web): closing a submenu by hover keeps keyboard focus in the menu
Hovering the parent menu's padding removed a submenu that held focus, leaving focus on the page and the arrow keys dead until another item was hovered. Closing submenus now returns focus to their trigger whichever way they close. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 7bde608 commit 414e24c

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

‎apps/web/src/contextMenuFallback.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,33 @@ describe("showContextMenuFallback keyboard", () => {
468468
await expect(selectionPromise).resolves.toBeNull();
469469
});
470470

471+
it("keeps keyboard focus in the menu when hovering the parent closes a submenu", async () => {
472+
const selectionPromise = showContextMenuFallback([
473+
{ id: "snooze", label: "Snooze", children: [{ id: "snooze:hour", label: "In 1 hour" }] },
474+
{ id: "archive", label: "Archive" },
475+
]);
476+
477+
pressKey("ArrowDown");
478+
pressKey("ArrowRight");
479+
expect(focusedLabel()).toBe("In 1 hour");
480+
481+
(document as unknown as FakeDocument).dispatchEvent(
482+
new MouseEvent("mousemove", { clientX: 10, clientY: 10 }) as unknown as FakeDomEvent,
483+
);
484+
const rootMenu = (document as unknown as FakeDocument)
485+
.querySelectorAll("div")
486+
.find((element) => element.attributes.get("role") === "menu");
487+
rootMenu?.dispatchEvent(new MouseEvent("mouseenter"));
488+
expect(findButton("In 1 hour")).toBeUndefined();
489+
expect(focusedLabel()).toBe("Snooze");
490+
491+
pressKey("ArrowDown");
492+
expect(focusedLabel()).toBe("Archive");
493+
494+
pressKey("Escape");
495+
await expect(selectionPromise).resolves.toBeNull();
496+
});
497+
471498
it("leaves keys alone once focus is outside the menu", async () => {
472499
const selectionPromise = showContextMenuFallback([{ id: "rename", label: "Rename" }]);
473500
(document as unknown as FakeDocument).activeElement?.blur();

‎apps/web/src/contextMenuFallback.ts‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,6 @@ export function showContextMenuFallback<T extends string>(
313313
item?.focus({ preventScroll: true });
314314
item?.scrollIntoView({ block: "nearest" });
315315
};
316-
const closeLevel = () => {
317-
const trigger = submenuTriggerStack[level];
318-
closeMenusFromLevel(level);
319-
trigger?.focus({ preventScroll: true });
320-
};
321-
322316
switch (event.key) {
323317
case "ArrowDown":
324318
focusItem(focusedIndex + 1);
@@ -338,13 +332,13 @@ export function showContextMenuFallback<T extends string>(
338332
break;
339333
case "ArrowLeft":
340334
if (level === 0) return;
341-
closeLevel();
335+
closeMenusFromLevel(level);
342336
break;
343337
case "Escape":
344338
if (level === 0) {
345339
cleanup(null);
346340
} else {
347-
closeLevel();
341+
closeMenusFromLevel(level);
348342
}
349343
break;
350344
case "Tab":
@@ -374,11 +368,16 @@ export function showContextMenuFallback<T extends string>(
374368
cleanup(null);
375369
};
376370

371+
// Focus inside a closing submenu moves to the item that opened it, so the
372+
// arrow keys keep working whether the submenu closed by key or by hover.
377373
const closeMenusFromLevel = (level: number) => {
374+
const trigger = submenuTriggerStack[level];
375+
const hadFocus = isNodeWithinMenuStack(document.activeElement, menuStack.slice(level));
378376
while (menuStack.length > level) {
379377
submenuTriggerStack.pop()?.setAttribute("aria-expanded", "false");
380378
menuStack.pop()?.remove();
381379
}
380+
if (hadFocus) trigger?.focus({ preventScroll: true });
382381
refreshHighlights();
383382
};
384383

0 commit comments

Comments
 (0)