Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): make FormListItem an item of the menu it stands in

Elian Doran4068d5fa75be
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 7863f35d77..87860c8a96 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -181,7 +181,7 @@ describe("contextMenu", () => {
`<span class="keyboard-shortcut"><kbd>Ctrl</kbd>+<kbd>C</kbd>,<kbd>Ctrl</kbd>+<kbd>Insert</kbd></span>`,
"<kbd>Ctrl+V</kbd>",
// An action with no shortcut assigned shows none: the row ends with its title.
- "title Cut"
+ "<span>Cut</span>"
]);
});
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsx
index 16e46db190..a4ce398401 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -13,7 +13,7 @@ import FormToggle from "./FormToggle";
import HelpTooltipButton from "./HelpTooltipButton";
import { useStaticTooltip, useSyncedRef } from "./hooks";
import Icon from "./Icon";
-import { MenuContext, MenuLevelContext } from "./menu_context";
+import { MenuContext, MenuLevelContext, pointerMoved } from "./menu_context";
import { joinElements } from "./react_utils";
import { renderShortcutKbds } from "./shortcut_kbd";
@@ -135,6 +135,16 @@ export interface FormListItemOpts {
rtl?: boolean;
postContent?: ComponentChildren;
itemRef?: RefObject<HTMLLIElement>;
+ /**
+ * Makes the row one that is checked or not, which a menu tells assistive technology;
+ * {@link checked} says which.
+ */
+ checkable?: boolean;
+ /**
+ * Inside a menu, whether running the row closes the menu before its {@link onClick} runs. It
+ * does unless this is `false`, for a caller that decides for itself, as `contextMenu` does.
+ */
+ closeOnSelect?: boolean;
/** The action whose shortcuts, as the user configured them, show at the end of the row. */
keyboardShortcut?: KeyboardActionNames;
/** A shortcut shown as it is written, for a row with no action of its own. */
@@ -153,8 +163,29 @@ const TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
animation: false
};
-export function FormListItem({ className, icon, iconClassName, value, title, active, disabled, checked, container, onClick, selected, rtl, triggerCommand, description, itemRef: externalItemRef, keyboardShortcut, shortcut, trailingIcon, ...contentProps }: FormListItemOpts) {
+export function FormListItem({ className, icon, iconClassName, value, title, active, disabled, checked, checkable, container, onClick, selected, rtl, triggerCommand, description, itemRef: externalItemRef, keyboardShortcut, shortcut, trailingIcon, closeOnSelect, ...contentProps }: FormListItemOpts) {
const itemRef = useSyncedRef<HTMLLIElement>(externalItemRef, null);
+ // Inside a `Menu` the row is one of its items; elsewhere, as in a dropdown, it stands alone.
+ const menu = useContext(MenuContext);
+ const level = useContext(MenuLevelContext);
+ const id = useId();
+ const isActive = menu?.active?.id === id;
+
+ /**
+ * Runs the row for the keys, as a click would, with the modifiers of the key that ran it, so
+ * Ctrl+Enter does what a Ctrl+click does.
+ */
+ function select(e: MouseEvent | KeyboardEvent) {
+ if (closeOnSelect !== false) menu?.close();
+ onClick?.(new MouseEvent("click", { ctrlKey: e.ctrlKey, shiftKey: e.shiftKey, altKey: e.altKey, metaKey: e.metaKey }));
+ }
+
+ function onPointed(e: { currentTarget: HTMLLIElement }) {
+ if (!menu || isMobile()) return;
+ // The keyboard goes on from the row the pointer last pointed at.
+ if (!disabled) menu.setActive(level, id);
+ menu.openSubmenu(level, undefined, e.currentTarget);
+ }
if (checked) {
icon = "bx bx-check";
@@ -164,11 +195,31 @@ export function FormListItem({ className, icon, iconClassName, value, title, act
return (
<li
- ref={itemRef}
+ ref={(element) => {
+ itemRef.current = element;
+ menu?.registerRow(id, element
+ ? { level, element, custom: false, disabled: !!disabled, hasSubmenu: false, select }
+ : undefined);
+ }}
+ id={menu ? id : undefined}
class={clsx("dropdown-item", active && "active", disabled && "disabled", selected && "selected",
- container && "dropdown-container-item", className)}
+ container && "dropdown-container-item", isActive && "tn-menu-active", className)}
data-value={value} title={title}
- tabIndex={container ? -1 : 0}
+ role={menu ? (checkable ? "menuitemcheckbox" : "menuitem") : undefined}
+ aria-checked={menu && checkable ? !!checked : undefined}
+ aria-disabled={(menu && disabled) || undefined}
+ // A menu keeps focus itself and marks the row its keys act on instead.
+ tabIndex={menu ? undefined : container ? -1 : 0}
+ // While the keys drive the menu, a row entered by a pointer at rest, as when the menu
+ // appears under it, keeps the keys' row. See `pointerMoved`.
+ onPointerEnter={menu ? (e) => {
+ if (!menu.keyboardDriven) onPointed(e);
+ } : undefined}
+ // 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={menu ? (e) => {
+ if (!isActive && pointerMoved(e)) onPointed(e);
+ } : undefined}
onClick={onClick}
data-trigger-command={triggerCommand}
dir={rtl ? "rtl" : undefined}
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index e5595d44a3..2ac8871840 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -8,7 +8,7 @@ import { useCallback, useLayoutEffect, useRef, useState } from "preact/hooks";
import type { MenuCommandItem, MenuItem } from "../../menus/context_menu";
import { handleRightToLeftPlacement, isMobile } from "../../services/utils";
import { FormDropdownDivider, FormDropdownSubmenu, FormListCustomItem, FormListHeader, FormListItem } from "./FormList";
-import { type ActiveRow, MenuContext, type MenuContextValue, type OpenSubmenu, type RowEntry } from "./menu_context";
+import { type ActiveRow, MenuContext, type MenuContextValue, type OpenSubmenu, pointerMoved, type RowEntry } from "./menu_context";
/**
* A menu of commands, drawn with the markup Bootstrap's dropdowns use (`.dropdown-menu`,
@@ -355,9 +355,9 @@ function MenuItemRow<T>({ row, onSelect }: { row: MenuItem<T>, onSelect: MenuPro
<FormListItem
// An item with neither keeps no slot for an icon, as an all-text menu such as spelling fixes needs.
icon={"uiIcon" in row || "checked" in row ? uiIcon : null}
- iconClassName={iconColorClass} checked={checked} disabled={enabled === false}
+ iconClassName={iconColorClass} checked={checked} checkable={"checked" in row} disabled={enabled === false}
className={className} badges={badges?.map((badge) => ({ className: badge.className, text: badge.title }))}
- keyboardShortcut={keyboardShortcut} shortcut={shortcut} trailingIcon={trailingIcon}
+ keyboardShortcut={keyboardShortcut} shortcut={shortcut} trailingIcon={trailingIcon} closeOnSelect={false}
onClick={select}
>
{label}
@@ -403,15 +403,6 @@ export function rowInNextColumn(
return nearest?.index;
}
-/**
- * Whether the pointer really moved. A menu appearing under a pointer at rest has the browser enter
- * the row there, and Chromium follow with a `pointermove` that goes nowhere; neither is the user
- * turning to the pointer.
- */
-function pointerMoved(e: PointerEvent) {
- return e.movementX !== 0 || e.movementY !== 0;
-}
-
/** Where a custom row takes focus: the element its content marks as its way in with `tabindex="0"`. */
function focusTarget(row: HTMLElement | undefined) {
return row?.querySelector<HTMLElement>("[tabindex='0']") ?? null;
diff --git a/apps/client/src/widgets/react/Menu_legacy.tsx b/apps/client/src/widgets/react/Menu_legacy.tsx
index 8b803aaba8..51f3ada07e 100644
--- a/apps/client/src/widgets/react/Menu_legacy.tsx
+++ b/apps/client/src/widgets/react/Menu_legacy.tsx
@@ -38,7 +38,6 @@ export function MenuCommand(props: MenuCommandProps) {
const openSubmenu = menu.open[level];
const open = openSubmenu?.id === id;
const active = menu.active?.id === id;
- const checkable = "checked" in props;
function select(e: MouseEvent | KeyboardEvent) {
if (closeOnSelect ?? !hasSubmenu) menu.close();
@@ -60,8 +59,7 @@ export function MenuCommand(props: MenuCommandProps) {
}}
className={clsx("dropdown-item", hasSubmenu && "dropdown-submenu", open && "submenu-open",
active && "tn-menu-active", disabled && "disabled", className)}
- role={checkable ? "menuitemcheckbox" : "menuitem"}
- aria-checked={checkable ? !!props.checked : undefined}
+ role="menuitem"
aria-disabled={disabled || undefined}
aria-haspopup={hasSubmenu ? "menu" : undefined}
aria-expanded={hasSubmenu ? open : undefined}
diff --git a/apps/client/src/widgets/react/menu_context.ts b/apps/client/src/widgets/react/menu_context.ts
index 50b7691e56..9a21d24e61 100644
--- a/apps/client/src/widgets/react/menu_context.ts
+++ b/apps/client/src/widgets/react/menu_context.ts
@@ -72,3 +72,12 @@ export function useMenu() {
if (!menu) throw new Error("A menu row must be rendered inside a Menu.");
return menu;
}
+
+/**
+ * Whether the pointer really moved. A menu appearing under a pointer at rest has the browser enter
+ * the row there, and Chromium follow with a `pointermove` that goes nowhere; neither is the user
+ * turning to the pointer.
+ */
+export function pointerMoved(e: PointerEvent) {
+ return e.movementX !== 0 || e.movementY !== 0;
+}