Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/ui/menus: close the open submenus while hovering the action buttons
Adorian Doran8a9e88fb2164
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex d5e2b1f50c..c4268fd195 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -523,8 +523,12 @@ function MenuSubmenu({ menu, icon, iconSvg, title, description, children, onDropclassName={clsx("tn-menu-action", open && "submenu-open", isActive && "tn-menu-active", className)}role="menuitem" aria-haspopup="menu" aria-expanded={open}disabled={disabled} tabIndex={-1}+ // As over a row, the pointer closes another submenu at this level, but the tile+ // opens its own only on a press.onPointerMove={(e) => {- if (!disabled && !isActive && pointerMoved(e)) menu.setActive(level, id);+ if (!pointerMoved(e)) return;+ if (openSubmenu && !open) menu.openSubmenu(level);+ if (!disabled && !isActive) menu.setActive(level, id);}}// As any row: the press keeps focus where it was, and the release acts.onMouseDown={(e) => e.preventDefault()}diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsxindex 7823b3a7ea..a66c275569 100644--- a/apps/client/src/widgets/react/Menu.spec.tsx+++ b/apps/client/src/widgets/react/Menu.spec.tsx@@ -390,6 +390,25 @@ describe("Menu with an action row", () => {expect(onSelect).toHaveBeenCalledWith(expect.objectContaining({ command: "cut" }), expect.any(KeyboardEvent));});+ it("closes a row's submenu once the pointer turns to the actions, a disabled one included", async () => {+ const { menu } = renderMenu();+ const layers = () => menu.querySelectorAll(":scope > div.dropdown-submenu > .dropdown-menu").length;+ const [ cut, copy ] = menu.querySelectorAll<HTMLElement>(".tn-menu-action");+ const more = [ ...menu.querySelectorAll<HTMLElement>("li.dropdown-item") ]+ .find((row) => row.querySelector(":scope > span")?.textContent === "More");+ if (!cut || !copy || !more) throw new Error("expected the actions and the More row to render");+ const point = (element: HTMLElement) =>+ element.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, movementX: 3 }));++ for (const action of [ cut, copy ]) {+ more.dispatchEvent(new PointerEvent("pointerenter"));+ await vi.waitFor(() => expect(layers()).toBe(1));+ point(action);+ await vi.waitFor(() => expect(layers()).toBe(0));+ }+ expect(more.classList).not.toContain("submenu-open");+ });+it("sets an action's badge over the corner of its icon, which gives way around it", () => {const { menu } = renderMenu();const icons = [ ...menu.querySelectorAll(".tn-menu-action-icon") ].map((icon) =>diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 7e07020c81..9f55e3d156 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -660,9 +660,12 @@ export function MenuTile({ icon, iconSvg, iconBadge, title, tooltip, keyboardShoaria-checked={checked}className={clsx("tn-menu-action", menu?.active?.id === id && "tn-menu-active")}disabled={disabled} tabIndex={menu ? -1 : undefined}- // The keyboard goes on from the action the pointer last pointed at, as from a row.+ // As over a row, the pointer closes the submenu open at this level, and the keyboard+ // goes on from the action it last pointed at.onPointerMove={(e) => {- if (menu && !disabled && menu.active?.id !== id && pointerMoved(e)) menu.setActive(level, id);+ if (!menu || !pointerMoved(e)) return;+ if (menu.open[level]) menu.openSubmenu(level);+ if (!disabled && menu.active?.id !== id) menu.setActive(level, id);}}// A press keeps focus where it was, as on a row.onMouseDown={(e) => e.preventDefault()}diff --git a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsxindex 8ee0372b2b..c3e52524a0 100644--- a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.spec.tsx@@ -284,6 +284,23 @@ describe("createToolbarGroupMenuHost", () => {expect(runs).toEqual([ "iso" ]);});+ it("keeps its submenu open under the pointer, until the pointer turns to another tile", async () => {+ const { menu, tile } = showTiles();+ const layer = () => menu.querySelector(":scope > div.dropdown-submenu > .dropdown-menu");+ const point = (element: Element | undefined) =>+ element?.dispatchEvent(new PointerEvent("pointermove", { bubbles: true, movementX: 5 }));++ tile.click();+ await vi.waitFor(() => expect(layer()).not.toBeNull());+ point(tile);+ await new Promise((resolve) => setTimeout(resolve));+ expect(layer()).not.toBeNull();++ point([ ...menu.querySelectorAll(".tn-menu-action") ].find((action) => action.textContent === "Math"));+ await vi.waitFor(() => expect(layer()).toBeNull());+ expect(tile.getAttribute("aria-expanded")).toBe("false");+ });+it("opens its submenu from the keys with Enter, Right moving on to the next tile", async () => {const { menu, runs } = showTiles();const key = (name: string) =>