diff --git a/src/workbench/MainMenu.interaction.test.tsx b/src/workbench/MainMenu.interaction.test.tsx
index 09c1c9e82..3dac6ef06 100644
--- a/src/workbench/MainMenu.interaction.test.tsx
+++ b/src/workbench/MainMenu.interaction.test.tsx
@@ -2,7 +2,7 @@ import { act, fireEvent, render, screen, within } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { commands, type Settings } from "@/platform/ipc";
import { useProject } from "@/platform/project";
-import { registerActions } from "@/platform/actions";
+import { getAction, registerActions } from "@/platform/actions";
import { usePlatform } from "@/platform/platformInfo";
import { MainMenu } from "./MainMenu";
@@ -60,6 +60,45 @@ afterEach(() => {
const titles = () => within(screen.getByRole("menubar"));
+describe("main menu action focus", () => {
+ it.each([
+ { style: "hamburger", narrow: false, row: false },
+ { style: "merged", narrow: false, row: false },
+ { style: "merged", narrow: true, row: false },
+ { style: "toolbar", narrow: false, row: true },
+ ])("restores the invoking field before Undo runs ($style, narrow=$narrow)", async ({ style, narrow, row }) => {
+ if (narrow) available = 100;
+ setStyle(style);
+ const focusAtUndo = vi.fn(() => document.activeElement);
+ vi.mocked(getAction("test.undo")!.run).mockImplementation(() => {
+ focusAtUndo();
+ });
+ render(
+ <>
+
+
+ >,
+ );
+ const field = screen.getByRole("textbox", { name: "Editor field" });
+ field.focus();
+ const open = (button: HTMLElement) => {
+ fireEvent.pointerDown(button);
+ button.focus();
+ fireEvent.click(button);
+ };
+ if (style === "hamburger" || narrow) open(screen.getByRole("button", { name: "Main Menu" }));
+ if (narrow) {
+ const root = await screen.findByRole("menu", { name: "Main Menu" });
+ fireEvent.click(within(root).getByRole("menuitem", { name: /Edit/ }));
+ } else open(within(await screen.findByRole("menubar")).getByRole("menuitem", { name: "Edit" }));
+ const undo = await screen.findByRole("menuitem", { name: "Undo" });
+ expect(undo).toHaveFocus();
+ fireEvent.click(undo);
+ expect(focusAtUndo).toHaveReturnedWith(field);
+ expect(field).toHaveFocus();
+ });
+});
+
describe("main menu presentation", () => {
it("keeps the hamburger default, expanding on click and collapsing on Escape", async () => {
const expanded = vi.fn();
diff --git a/src/workbench/MainMenu.tsx b/src/workbench/MainMenu.tsx
index 6b752dfae..72ac965f4 100644
--- a/src/workbench/MainMenu.tsx
+++ b/src/workbench/MainMenu.tsx
@@ -108,6 +108,17 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
const persistent = (merged && fits) || row;
const showTitles = persistent || (!merged && expanded);
+ const rememberFocus = (target: EventTarget | null) => {
+ if (
+ target instanceof HTMLElement &&
+ target !== document.body &&
+ target !== hamburger.current &&
+ !bar?.contains(target) &&
+ !target.closest('[role="menu"]')
+ )
+ previousFocus.current = target;
+ };
+
useEffect(() => {
let disposed = false;
if (merged || row)
@@ -155,6 +166,13 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
setOpen(null);
setExpanded(false);
};
+ // MenuList runs the action synchronously after onDone, before Popup unmounts.
+ const closeForAction = () => {
+ const target = previousFocus.current;
+ previousFocus.current = null;
+ close();
+ if (target?.isConnected) target.focus({ preventScroll: true });
+ };
const expand = async () => {
const top = await loadMenus();
if (!top.length) return;
@@ -188,10 +206,13 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
} else if (persistent) {
if (bar?.contains(document.activeElement)) previousFocus.current?.focus();
else {
- previousFocus.current = document.activeElement as HTMLElement;
+ rememberFocus(document.activeElement);
focusTitle(0);
}
- } else void expand();
+ } else {
+ rememberFocus(document.activeElement);
+ void expand();
+ }
});
});
const onKeyDown = (e: KeyboardEvent) => {
@@ -219,9 +240,13 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
title={t("menu.mainMenu")}
aria-haspopup="menu"
aria-expanded={expanded}
+ onFocus={(event) => rememberFocus(event.relatedTarget)}
onClick={() => {
if (expanded) close();
- else void expand();
+ else {
+ rememberFocus(document.activeElement);
+ void expand();
+ }
}}
>
@@ -238,7 +263,10 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
aria-haspopup="menu"
aria-expanded={open === i}
className={clsx(styles.menuTitle, open === i && styles.menuTitleOpen)}
- onFocus={() => setFocused(i)}
+ onFocus={(event) => {
+ rememberFocus(event.relatedTarget);
+ setFocused(i);
+ }}
onPointerDown={() => {
pressedTitle.current = true;
setTimeout(() => {
@@ -252,6 +280,7 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
if (open !== null) setOpen(i);
}}
onClick={() => {
+ rememberFocus(document.activeElement);
if (open === i) close();
else setOpen(i);
}}
@@ -274,7 +303,7 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
>
@@ -302,7 +331,7 @@ function MainMenuBar({ merged, row, onExpandedChange }: MainMenuProps & { merged
autoFocus={false}
className={menuStyles.popup}
>
-
+
)}