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: hide selection when the mouse pointer leaves the menu
Adorian Doran6b79caed062c
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex bc4df4920d..cd970f6608 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -1070,6 +1070,24 @@ describe("contextMenu", () => {expect(contextMenu.isShown).toBe(false);});+ it("keeps the match typed into the input as the pointer leaves the menu", async () => {+ const picked: string[] = [];+ await openFilterable(picked);+ const parent = [ ...menuElement()?.querySelectorAll<HTMLElement>("li.dropdown-submenu") ?? [] ]+ .find((row) => row.textContent?.includes("Insert child note"));+ parent?.dispatchEvent(new PointerEvent("pointerenter"));+ await vi.waitFor(() => expect(filterInput()).not.toBeNull());++ filterInput()?.focus();+ edit("boa");+ await vi.waitFor(() => expect(activeRow()).toMatch(/^Board/));+ menuElement()?.dispatchEvent(new PointerEvent("pointerleave"));+ await new Promise((resolve) => setTimeout(resolve));+ expect(activeRow()).toMatch(/^Board/);+ key("Enter");+ expect(picked).toEqual([ "Board" ]);+ });+it("gives Right and Left to the menu where the caret has nowhere to go", async () => {await openFilterable();await openSubmenuByKeys();diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex a4d1c094c0..56de26884c 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -516,7 +516,7 @@ function MenuSubmenu({ menu, icon, iconSvg, title, description, children, onDrop? { level, element, custom: false, disabled: !!disabled, hasSubmenu: true, inline: true, filterable, select: () => {} }: undefined);}}- className={clsx("tn-menu-action", isActive && "tn-menu-active", className)}+ className={clsx("tn-menu-action", open && "submenu-open", isActive && "tn-menu-active", className)}role="menuitem" aria-haspopup="menu" aria-expanded={open}disabled={disabled} tabIndex={-1}onPointerMove={(e) => {diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.cssindex e8ba8e36a7..2903c09263 100644--- a/apps/client/src/widgets/react/Menu.css+++ b/apps/client/src/widgets/react/Menu.css@@ -260,12 +260,13 @@ body.desktop .dropdown-item.dropdown-submenu.submenu-open,line-height: 1.2;&:hover:not(:disabled),- &.tn-menu-active {+ &.tn-menu-active,+ body.desktop &.submenu-open {color: var(--hover-item-text-color);background-color: var(--hover-item-background-color);}- .tn-menu-keyboard &:hover:not(.tn-menu-active) {+ .tn-menu-keyboard &:hover:not(.tn-menu-active, .submenu-open) {color: var(--menu-text-color);background-color: transparent;}diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsxindex 41f3e72014..54a0a9cc3e 100644--- a/apps/client/src/widgets/react/Menu.spec.tsx+++ b/apps/client/src/widgets/react/Menu.spec.tsx@@ -202,6 +202,32 @@ describe("Menu with declared rows", () => {expect(onClose).not.toHaveBeenCalled();});+ it("unmarks the pointer's row as the pointer leaves, but not an open submenu's row or the keys' row", async () => {+ const { menu } = renderMenu();+ const leave = () => menu.dispatchEvent(new PointerEvent("pointerleave"));++ rowTitled("Copy").dispatchEvent(new PointerEvent("pointerenter"));+ await vi.waitFor(() => expect(activeTitle(menu)).toBe("Copy"));+ leave();+ await vi.waitFor(() => expect(activeTitle(menu)).toBeUndefined());++ rowTitled("Advanced").dispatchEvent(new PointerEvent("pointerenter"));+ await vi.waitFor(() => expect(layers(menu)).toHaveLength(1));+ rowTitled("Reload").dispatchEvent(new PointerEvent("pointerenter"));+ await vi.waitFor(() => expect(activeTitle(menu)).toBe("Reload"));+ leave();+ // The keys go on from the row the open submenu stands beside.+ await vi.waitFor(() => expect(activeTitle(menu)).toBe("Advanced"));+ expect(layers(menu)).toEqual([ [ "Show log", "Reload" ] ]);+ expect(rowTitled("Advanced").classList).toContain("submenu-open");++ key(menu, "ArrowUp");+ await vi.waitFor(() => expect(activeTitle(menu)).toBe("Copy"));+ leave();+ await new Promise((resolve) => setTimeout(resolve));+ expect(activeTitle(menu)).toBe("Copy");+ });+it("starts at the last row when asked to, and takes no focus once it is no longer wanted", async () => {const { menu } = renderMenu({ startAt: "last" });await vi.waitFor(() => expect(activeTitle(menu)).toBe("Advanced"));diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 4dc573de42..13158d2497 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -202,6 +202,8 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,lastFilter.current = filter;if (level === undefined) return;if (!activateFirstRow(level + 1)) pendingFirstRow.current = level + 1;+ // Typing into the input moves the active row, as the keys do.+ if (filter) setKeyboardDriven(true);const input = filterInputIn(menuRef.current);if (filter && input && document.activeElement !== input) {@@ -408,6 +410,14 @@ export default function Menu<T>({ id, className, anchor, placement, bottomSheet,onPointerMove={(e) => {if (pointerMoved(e)) setKeyboardDriven(false);}}+ // Fires only when the pointer is off every level, as the submenu layers are children+ // of this element. Unless the keys drive the menu, only the deepest open submenu's+ // row stays active.+ onPointerLeave={() => {+ if (keyboardDriven) return;+ const deepest = open.at(-1);+ setActiveRow(deepest && { level: open.length - 1, id: deepest.id });+ }}// Neither the browser's menu nor another of the app's opens over this one. Every level// is inside this element, so one handler covers them all.onContextMenu={(e) => {diff --git a/apps/client/src/widgets/react/Popup.tsx b/apps/client/src/widgets/react/Popup.tsxindex 2556c4062f..3b11049093 100644--- a/apps/client/src/widgets/react/Popup.tsx+++ b/apps/client/src/widgets/react/Popup.tsx@@ -13,7 +13,7 @@ import { useCallback, useContext, useLayoutEffect, useMemo, useRef } from "preac* places itself, keeps inside the viewport, and says when it is dismissed.*/export interface PopupProps extends Pick<HTMLAttributes<HTMLDivElement>, "id" | "className" | "role" | "style" | "tabIndex"- | "onClick" | "onPointerMove" | "onContextMenu" | "aria-labelledby"> {+ | "onClick" | "onPointerMove" | "onPointerLeave" | "onContextMenu" | "aria-labelledby"> {/*** What it stands beside: an element, a point in the viewport such as where a right-click landed,* or a rect that is no element, such as a text editor's caret.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 54a0dd78d1..0051f1db98 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@@ -277,6 +277,7 @@ describe("createToolbarGroupMenuHost", () => {await vi.waitFor(() => expect(menu.querySelector(":scope > div.dropdown-submenu > .dropdown-menu")?.textContent).toBe("2026-10-05"));expect(tile.getAttribute("aria-expanded")).toBe("true");+ expect(tile.classList).toContain("submenu-open");expect(runs).toEqual([]);press(rowTitled("2026-10-05"));