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
fix(menu): mark only the context menu's active row once the keys move it
Elian Doranf19d2c5ed353
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex f9f68f6ab9..628fba596c 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -751,6 +751,14 @@ describe("contextMenu", () => {key("ArrowDown");await vi.waitFor(() => expect(activeRow()).toBe("Templates"));+ // The pointer still rests on "Paste", whose `:hover` would mark a second row: while the+ // keys drive the menu, `Menu.css` takes the hover look off every row but the active one.+ expect(menuElement()?.classList.contains("tn-menu-keyboard")).toBe(true);++ // The slightest move makes the row under the pointer the active one again.+ paste?.dispatchEvent(new PointerEvent("pointermove", { bubbles: true }));+ await vi.waitFor(() => expect(activeRow()).toBe("Paste"));+ expect(menuElement()?.classList.contains("tn-menu-keyboard")).toBe(false);});});diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.cssindex 3d63a1465a..04b87d1d78 100644--- a/apps/client/src/widgets/react/Menu.css+++ b/apps/client/src/widgets/react/Menu.css@@ -75,6 +75,18 @@ body.desktop .dropdown-item.dropdown-submenu.submenu-open,background-color: var(--hover-item-background-color);}+/*+ * Once the keys move the active row, a pointer at rest still hovers the row it left, which would+ * mark a second row until the pointer moves. The row looks unhovered meanwhile, as in the resting+ * `.dropdown-item` rule. `!important` and the extra classes outrank the theme's `:hover` rule in+ * style.css, whose values these undo.+ */+.tn-menu.tn-menu-keyboard .dropdown-item:hover:not(.disabled, .active, .dropdown-container-item, .tn-menu-active, .submenu-open) {+ color: var(--menu-text-color) !important;+ background-color: transparent !important;+ border-color: transparent !important;+}+/** The theme draws the line between columns with the menu's `column-rule`, which is not inherited,* so the scroller hands it on to the columns inside it.diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 593865d7ab..95c0ba289f 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -87,6 +87,8 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,const [ active, setActiveRow ] = useState<ActiveRow<T>>();const rows = useRef(new Map<MenuCommandItem<T>, HTMLElement>()).current;const typeahead = useRef({ text: "", timeout: 0 });+ /** Whether the keys moved the menu since the pointer last did. See `Menu.css`. */+ const [ keyboardDriven, setKeyboardDriven ] = useState(false);const setActive = useCallback((level: number, item: MenuCommandItem<T>) => {setActiveRow({ level, item });@@ -193,6 +195,7 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,}}}+ setKeyboardDriven(true);e.preventDefault();e.stopPropagation();}@@ -225,7 +228,9 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,return (<divref={menuRef} id={id} role="menu" tabIndex={-1}- className={clsx("dropdown-menu show tn-menu", bottomSheet && "mobile-bottom-menu", className)}+ className={clsx("dropdown-menu show tn-menu", bottomSheet && "mobile-bottom-menu",+ keyboardDriven && "tn-menu-keyboard", className)}+ onPointerMove={() => setKeyboardDriven(false)}// 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) => {@@ -327,6 +332,13 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandItconst active = state.active?.level === level && state.active.item === item;const id = useId();+ function onPointed(e: { currentTarget: HTMLLIElement }) {+ if (isMobile()) return;+ // The keyboard goes on from the row the pointer last pointed at.+ if (!disabled) state.setActive(level, item);+ state.openSubmenu(level, hasSubmenu && !disabled ? item : undefined, e.currentTarget);+ }+return (<liid={id}@@ -340,11 +352,11 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandItaria-disabled={disabled || undefined}aria-haspopup={hasSubmenu ? "menu" : undefined}aria-expanded={hasSubmenu ? open : undefined}- onPointerEnter={(e) => {- if (isMobile()) return;- // The keyboard goes on from the row the pointer last pointed at.- if (!disabled) state.setActive(level, item);- state.openSubmenu(level, hasSubmenu && !disabled ? item : undefined, e.currentTarget);+ onPointerEnter={onPointed}+ // The keys can move the active row from under a pointer at rest, whose `:hover` would+ // then mark a second row. The next move of the pointer makes its row the active one.+ onPointerMove={(e) => {+ if (!active) onPointed(e);}}// `mousedown` rather than `click`, and its default prevented, so the press does not move// focus out of the menu: `contextMenu` hands it back to a text editor before the command