Skip to content

Build your personal knowledge base with Trilium Notes

refactor(menu): draw a menu's rows with FormListItem and FormDropdownSubmenu

Elian Doran284084af7ff4
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsx
index 390c55469d..ec5952f923 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -258,7 +258,7 @@ export function FormDropdownSubmenu({ icon, title, children, dropStart, onDropdo
icon: string,
title: ComponentChildren,
children: ComponentChildren,
- onDropdownToggleClicked?: () => void,
+ onDropdownToggleClicked?: (e: MouseEvent) => void,
dropStart?: boolean
}) {
const [ openOnMobile, setOpenOnMobile ] = useState(false);
@@ -273,7 +273,7 @@ export function FormDropdownSubmenu({ icon, title, children, dropStart, onDropdo
if (isMobile()) {
setOpenOnMobile(!openOnMobile);
} else if (onDropdownToggleClicked) {
- onDropdownToggleClicked();
+ onDropdownToggleClicked(e);
}
}}
>
diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsx
index dbab579da1..470d64f4ca 100644
--- a/apps/client/src/widgets/react/Menu.spec.tsx
+++ b/apps/client/src/widgets/react/Menu.spec.tsx
@@ -1,8 +1,8 @@
import { render } from "preact";
import { afterEach, describe, expect, it, vi } from "vitest";
-import { FormDropdownDivider, FormListHeader } from "./FormList";
-import Menu, { MenuCommand, rowInNextColumn } from "./Menu";
+import { FormDropdownDivider, FormDropdownSubmenu, FormListHeader, FormListItem } from "./FormList";
+import Menu, { rowInNextColumn } from "./Menu";
/** A row 20px tall, `left` into the menu and `top` down it. */
function row(left: number, top: number) {
@@ -53,17 +53,17 @@ describe("Menu with declared rows", () => {
const onClose = vi.fn(() => calls.push("close"));
function Advanced() {
return (
- <MenuCommand title="Advanced" uiIcon="bx bx-chip">
- <MenuCommand title="Show log" onSelect={() => calls.push("log")} />
- <MenuCommand title="Reload" onSelect={() => calls.push("reload")} />
- </MenuCommand>
+ <FormDropdownSubmenu title="Advanced" icon="bx bx-chip">
+ <FormListItem onClick={() => calls.push("log")}>Show log</FormListItem>
+ <FormListItem onClick={() => calls.push("reload")}>Reload</FormListItem>
+ </FormDropdownSubmenu>
);
}
render((
<Menu x={10} y={10} onClose={onClose}>
<FormListHeader text="Note" />
- <MenuCommand title="Copy" keyboardShortcut={undefined} onSelect={() => calls.push("copy")} />
- <MenuCommand title="Delete" enabled={false} onSelect={() => calls.push("delete")} />
+ <FormListItem onClick={() => calls.push("copy")}>Copy</FormListItem>
+ <FormListItem disabled onClick={() => calls.push("delete")}>Delete</FormListItem>
<FormDropdownDivider />
<Advanced />
</Menu>
@@ -147,9 +147,4 @@ describe("Menu with declared rows", () => {
key(menu, "Enter");
expect(calls).toEqual([ "close", "log" ]);
});
-
- it("refuses a row outside a menu", () => {
- vi.spyOn(console, "error").mockImplementation(() => {});
- expect(() => render(<MenuCommand title="Stray" />, host)).toThrow("inside a Menu");
- });
});
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 17d7861142..ec5cfae9a2 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -3,25 +3,20 @@ import "./Menu.css";
import { autoUpdate, computePosition, flip, hide, offset, type Placement, type ReferenceElement, shift, size, type VirtualElement } from "@floating-ui/dom";
import clsx from "clsx";
import type { ComponentChildren } from "preact";
-import { createPortal } from "preact/compat";
-import { useCallback, useContext, useId, useLayoutEffect, useRef, useState } from "preact/hooks";
+import { useCallback, useLayoutEffect, useRef, useState } from "preact/hooks";
import type { MenuCommandItem, MenuItem } from "../../menus/context_menu";
-import { getActionSync } from "../../services/keyboard_actions";
import { handleRightToLeftPlacement, isMobile } from "../../services/utils";
-import { FormDropdownDivider, FormListCustomItem, FormListHeader } from "./FormList";
-import { type ActiveRow, MenuContext, type MenuContextValue, MenuLevelContext, type OpenSubmenu, type RowEntry, useMenu } from "./menu_context";
-import { joinElements } from "./react_utils";
-import { renderShortcutKbds } from "./shortcut_kbd";
+import { FormDropdownDivider, FormDropdownSubmenu, FormListCustomItem, FormListHeader, FormListItem } from "./FormList";
+import { type ActiveRow, MenuContext, type MenuContextValue, type OpenSubmenu, type RowEntry } from "./menu_context";
/**
* A menu of commands, drawn with the markup Bootstrap's dropdowns use (`.dropdown-menu`,
* `.dropdown-item`, `.dropdown-divider`), so the stylesheets and themes that style those style
* this too. Only the placement is its own.
*
- * The rows come as data in {@link items}, or as components in {@link children}: `MenuCommand`,
- * `FormDropdownDivider`, `FormListHeader` and `FormListCustomItem`. A `MenuCommand` with children opens them as its
- * submenu.
+ * The rows come as data in {@link items}, or as components in {@link children}: `FormListItem`,
+ * `FormDropdownSubmenu`, `FormDropdownDivider`, `FormListHeader` and `FormListCustomItem`.
*/
export interface MenuProps<T> {
id?: string;
@@ -307,159 +302,6 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
);
}
-export interface MenuCommandProps extends Pick<MenuCommandItem<unknown>, "title" | "uiIcon" | "iconColorClass" | "checked"
- | "enabled" | "shortcut" | "keyboardShortcut" | "badges" | "trailingIcon" | "className" | "columns"> {
- /** Called when the row is pressed with the primary button, or run from the keyboard. */
- onSelect?(e: MouseEvent | KeyboardEvent): void;
- /** Whether running the row closes the menu. A row does, and one that opens a submenu does not. */
- closeOnSelect?: boolean;
- /** The rows of the submenu this row opens. */
- children?: ComponentChildren;
-}
-
-/**
- * A row that runs a command, or with children, opens them as its submenu. As with
- * {@link MenuCommandItem}, a row given `uiIcon` or `checked`, even as nothing, keeps a slot for
- * the icon, and one given `checked` can be checked.
- */
-export function MenuCommand(props: MenuCommandProps) {
- const { title, enabled, badges, trailingIcon, className, columns, onSelect, closeOnSelect, children } = props;
- const menu = useMenu();
- const level = useContext(MenuLevelContext);
- const id = useId();
- const disabled = enabled === false;
- const hasSubmenu = children !== undefined && children !== null && children !== false;
- 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();
- onSelect?.(e);
- }
-
- function onPointed(e: { currentTarget: HTMLLIElement }) {
- if (isMobile()) return;
- // The keyboard goes on from the row the pointer last pointed at.
- if (!disabled) menu.setActive(level, id);
- menu.openSubmenu(level, hasSubmenu && !disabled ? id : undefined, e.currentTarget);
- }
-
- return (
- <li
- id={id}
- ref={(element) => {
- menu.registerRow(id, element ? { level, element, custom: false, disabled, hasSubmenu, select } : undefined);
- }}
- 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}
- aria-disabled={disabled || undefined}
- aria-haspopup={hasSubmenu ? "menu" : undefined}
- aria-expanded={hasSubmenu ? open : undefined}
- // 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={(e) => {
- if (!menu.keyboardDriven) onPointed(e);
- }}
- // 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 && pointerMoved(e)) 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
- // runs, with the selection that commands such as a spelling fix act on.
- onMouseDown={(e) => {
- if (e.button !== 0) return;
- e.preventDefault();
- // An unfolded row holds its submenu's rows, whose presses would reach it too.
- e.stopPropagation();
- if (disabled) return;
-
- // Pressed again, an unfolded row folds its submenu back.
- if (hasSubmenu && isMobile()) {
- menu.openSubmenu(level, open ? undefined : id, e.currentTarget, true);
- return;
- }
- if (hasSubmenu) menu.openSubmenu(level, id, e.currentTarget, true);
- select(e);
- }}
- >
- <span className={hasSubmenu ? "dropdown-toggle" : undefined}>
- <MenuIconSlot {...props} />
- <span className="tn-menu-gap" />
- {/* Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`. */}
- <span id={titleId(id)} dangerouslySetInnerHTML={{ __html: title }} />
- {badges?.map((badge, index) => (
- <span key={index} className={clsx("badge", badge.className)}>{badge.title}</span>
- ))}
- <MenuShortcut {...props} />
- {trailingIcon && <span className={clsx(trailingIcon, "tn-icon", "menu-trailing-icon")} />}
- </span>
- {hasSubmenu && (isMobile()
- // A phone has no room beside the menu, so an open submenu unfolds under its row.
- ? (
- <ul className={clsx("dropdown-menu", open && "show")} role="menu" aria-labelledby={titleId(id)}>
- <MenuLevelContext.Provider value={level + 1}>{open && children}</MenuLevelContext.Provider>
- </ul>
- )
- : open && openSubmenu && menu.layerHost && createPortal((
- <SubmenuLayer level={level + 1} submenu={openSubmenu} columns={columns}>{children}</SubmenuLayer>
- ), menu.layerHost))}
- </li>
- );
-}
-
-/**
- * A submenu opened on the desktop, placed beside the row it opened from rather than nested in it,
- * so a scrolling menu neither clips it nor scrolls it away.
- */
-function SubmenuLayer({ level, submenu, columns, children }: {
- level: number,
- submenu: OpenSubmenu,
- columns?: number,
- children: ComponentChildren
-}) {
- const layerRef = useRef<HTMLDivElement>(null);
- const columnCount = (columns ?? 1) > 1 ? columns : undefined;
-
- useLayoutEffect(() => {
- const layer = layerRef.current;
- if (!layer) return;
-
- const placement = handleRightToLeftPlacement("right") === "right" ? "right-start" : "left-start";
- return autoUpdate(submenu.anchor, layer, () => void placeMenu(layer, submenu.anchor, placement, true));
- }, [ submenu.anchor ]);
-
- // In a `.dropdown-submenu`, so the theme's submenu rules apply.
- return (
- <MenuLevelContext.Provider value={level}>
- <div className="dropdown-submenu">
- <div
- ref={layerRef} role="menu" aria-labelledby={titleId(submenu.anchor.id)}
- className={clsx("dropdown-menu show tn-menu", submenu.immediate && "tn-menu-immediate")}
- >
- {/* Like the top level, so the blur on the layer's `::before` stays behind its rows. */}
- {columnCount
- // The columns go on an inner list of their full height, so a capped menu
- // scrolls them rather than growing more columns to the side.
- ? (
- <div className="tn-menu-scroll">
- <menu className="tn-menu-columns" role="none" style={{ columnCount }}>
- {children}
- </menu>
- </div>
- )
- : <menu className="tn-menu-scroll" role="none">{children}</menu>}
- </div>
- </div>
- </MenuLevelContext.Provider>
- );
-}
-
/**
* `items` drawn as the rows that stand for them, with a run of separators reduced to one. In
* `columns`, the rows a column must not break between are wrapped in `.dropdown-no-break`.
@@ -494,52 +336,30 @@ function MenuItemRow<T>({ row, onSelect }: { row: MenuItem<T>, onSelect: MenuPro
return <FormListCustomItem><row.componentFn /></FormListCustomItem>;
}
- // The rest keeps `uiIcon` and `checked` only where the item has them, which `MenuCommand` reads.
- const { items, command: _command, handler: _handler, type: _type, mime: _mime, templateNoteId: _templateNoteId,
- spellingSuggestion: _spellingSuggestion, ...props } = row;
- return (
- // `onSelect` decides whether the menu closes, as `contextMenu` does.
- <MenuCommand {...props} closeOnSelect={false} onSelect={(e) => onSelect?.(row, e)}>
- {/* A phone unfolds a submenu in a single column. */}
- {items ? <MenuItems items={items} onSelect={onSelect} columns={(row.columns ?? 1) > 1 && !isMobile()} /> : undefined}
- </MenuCommand>
- );
-}
-
-/** The id of a row's title, which names the submenu it opens. */
-function titleId(rowId: string) {
- return `${rowId}-title`;
-}
+ const { title, uiIcon, iconColorClass, checked, enabled, badges, className, items } = row;
+ // Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`.
+ const label = <span dangerouslySetInnerHTML={{ __html: title }} />;
+ // `onSelect` decides whether the menu closes, as `contextMenu` does.
+ const select = (e: MouseEvent) => onSelect?.(row, e);
-/**
- * The shortcuts of the row's `keyboardShortcut` action as the user configured them, or else its
- * literal `shortcut`. Read synchronously, so the menu is placed at its final width.
- */
-function MenuShortcut({ keyboardShortcut, shortcut }: Pick<MenuCommandProps, "keyboardShortcut" | "shortcut">) {
- if (keyboardShortcut) {
- const shortcuts = getActionSync(keyboardShortcut)?.effectiveShortcuts;
- if (!shortcuts?.length) return null;
+ if (items) {
return (
- <span className="keyboard-shortcut">
- {joinElements(shortcuts.map(shortcut => renderShortcutKbds(shortcut)), ",")}
- </span>
+ <FormDropdownSubmenu icon={uiIcon ?? "bx bx-empty"} title={label} onDropdownToggleClicked={select}>
+ {/* A phone unfolds a submenu in a single column. */}
+ <MenuItems items={items} onSelect={onSelect} columns={(row.columns ?? 1) > 1 && !isMobile()} />
+ </FormDropdownSubmenu>
);
}
- return shortcut ? <kbd>{shortcut}</kbd> : null;
-}
-
-/**
- * The icon, or a check mark in its place. A row that sets `uiIcon` to nothing gets a blank slot,
- * and one without `uiIcon` or `checked` an empty one.
- */
-function MenuIconSlot(props: Pick<MenuCommandProps, "uiIcon" | "checked" | "iconColorClass">) {
- if (!("uiIcon" in props || "checked" in props)) return <span />;
-
- const icon = props.checked ? "bx bx-check" : props.uiIcon;
- return icon
- ? <span className={clsx(icon, "tn-icon", props.iconColorClass)} />
- : <span>{" "}</span>;
+ return (
+ <FormListItem
+ icon={uiIcon} iconClassName={iconColorClass} checked={checked} disabled={enabled === false}
+ className={className} badges={badges?.map((badge) => ({ className: badge.className, text: badge.title }))}
+ onClick={select}
+ >
+ {label}
+ </FormListItem>
+ );
}
/** How far apart, in pixels, two rows' left edges can be and still stand in one column. */
@@ -640,7 +460,7 @@ function isHeader<T>(item: MenuItem<T>) {
* A submenu overlaps its row by 2px, so the pointer crosses no gap on its way over, and lines its
* first row up with that row. It is hidden while its row is scrolled out of its menu's view.
*/
-async function placeMenu(menu: HTMLElement, anchor: ReferenceElement, placement: Placement, isSubmenu = false) {
+export async function placeMenu(menu: HTMLElement, anchor: ReferenceElement, placement: Placement, isSubmenu = false) {
const [ side ] = placement.split("-");
const otherSide = side === "left" ? "right" : "left";
const { x, y, middlewareData } = await computePosition(anchor, menu, {
diff --git a/apps/client/src/widgets/react/Menu_legacy.tsx b/apps/client/src/widgets/react/Menu_legacy.tsx
new file mode 100644
index 0000000000..5c89e9efcc
--- /dev/null
+++ b/apps/client/src/widgets/react/Menu_legacy.tsx
@@ -0,0 +1,215 @@
+// Reference copy of `MenuCommand`, the row `Menu` drew before its data adapter switched to
+// `FormListItem` and `FormDropdownSubmenu`. Nothing imports it. Delete a piece once those rows
+// cover it, and the file once they reach parity.
+
+import { autoUpdate } from "@floating-ui/dom";
+import clsx from "clsx";
+import type { ComponentChildren } from "preact";
+import { createPortal } from "preact/compat";
+import { useContext, useId, useLayoutEffect, useRef } from "preact/hooks";
+
+import type { MenuCommandItem } from "../../menus/context_menu";
+import { getActionSync } from "../../services/keyboard_actions";
+import { handleRightToLeftPlacement, isMobile } from "../../services/utils";
+import { placeMenu } from "./Menu";
+import { MenuLevelContext, type OpenSubmenu, useMenu } from "./menu_context";
+import { joinElements } from "./react_utils";
+import { renderShortcutKbds } from "./shortcut_kbd";
+
+export interface MenuCommandProps extends Pick<MenuCommandItem<unknown>, "title" | "uiIcon" | "iconColorClass" | "checked"
+ | "enabled" | "shortcut" | "keyboardShortcut" | "badges" | "trailingIcon" | "className" | "columns"> {
+ /** Called when the row is pressed with the primary button, or run from the keyboard. */
+ onSelect?(e: MouseEvent | KeyboardEvent): void;
+ /** Whether running the row closes the menu. A row does, and one that opens a submenu does not. */
+ closeOnSelect?: boolean;
+ /** The rows of the submenu this row opens. */
+ children?: ComponentChildren;
+}
+
+/**
+ * A row that runs a command, or with children, opens them as its submenu. As with
+ * {@link MenuCommandItem}, a row given `uiIcon` or `checked`, even as nothing, keeps a slot for
+ * the icon, and one given `checked` can be checked.
+ */
+export function MenuCommand(props: MenuCommandProps) {
+ const { title, enabled, badges, trailingIcon, className, columns, onSelect, closeOnSelect, children } = props;
+ const menu = useMenu();
+ const level = useContext(MenuLevelContext);
+ const id = useId();
+ const disabled = enabled === false;
+ const hasSubmenu = children !== undefined && children !== null && children !== false;
+ 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();
+ onSelect?.(e);
+ }
+
+ function onPointed(e: { currentTarget: HTMLLIElement }) {
+ if (isMobile()) return;
+ // The keyboard goes on from the row the pointer last pointed at.
+ if (!disabled) menu.setActive(level, id);
+ menu.openSubmenu(level, hasSubmenu && !disabled ? id : undefined, e.currentTarget);
+ }
+
+ return (
+ <li
+ id={id}
+ ref={(element) => {
+ menu.registerRow(id, element ? { level, element, custom: false, disabled, hasSubmenu, select } : undefined);
+ }}
+ 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}
+ aria-disabled={disabled || undefined}
+ aria-haspopup={hasSubmenu ? "menu" : undefined}
+ aria-expanded={hasSubmenu ? open : undefined}
+ // 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={(e) => {
+ if (!menu.keyboardDriven) onPointed(e);
+ }}
+ // 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 && pointerMoved(e)) 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
+ // runs, with the selection that commands such as a spelling fix act on.
+ onMouseDown={(e) => {
+ if (e.button !== 0) return;
+ e.preventDefault();
+ // An unfolded row holds its submenu's rows, whose presses would reach it too.
+ e.stopPropagation();
+ if (disabled) return;
+
+ // Pressed again, an unfolded row folds its submenu back.
+ if (hasSubmenu && isMobile()) {
+ menu.openSubmenu(level, open ? undefined : id, e.currentTarget, true);
+ return;
+ }
+ if (hasSubmenu) menu.openSubmenu(level, id, e.currentTarget, true);
+ select(e);
+ }}
+ >
+ <span className={hasSubmenu ? "dropdown-toggle" : undefined}>
+ <MenuIconSlot {...props} />
+ <span className="tn-menu-gap" />
+ {/* Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`. */}
+ <span id={titleId(id)} dangerouslySetInnerHTML={{ __html: title }} />
+ {badges?.map((badge, index) => (
+ <span key={index} className={clsx("badge", badge.className)}>{badge.title}</span>
+ ))}
+ <MenuShortcut {...props} />
+ {trailingIcon && <span className={clsx(trailingIcon, "tn-icon", "menu-trailing-icon")} />}
+ </span>
+ {hasSubmenu && (isMobile()
+ // A phone has no room beside the menu, so an open submenu unfolds under its row.
+ ? (
+ <ul className={clsx("dropdown-menu", open && "show")} role="menu" aria-labelledby={titleId(id)}>
+ <MenuLevelContext.Provider value={level + 1}>{open && children}</MenuLevelContext.Provider>
+ </ul>
+ )
+ : open && openSubmenu && menu.layerHost && createPortal((
+ <SubmenuLayer level={level + 1} submenu={openSubmenu} columns={columns}>{children}</SubmenuLayer>
+ ), menu.layerHost))}
+ </li>
+ );
+}
+
+/**
+ * A submenu opened on the desktop, placed beside the row it opened from rather than nested in it,
+ * so a scrolling menu neither clips it nor scrolls it away.
+ */
+function SubmenuLayer({ level, submenu, columns, children }: {
+ level: number,
+ submenu: OpenSubmenu,
+ columns?: number,
+ children: ComponentChildren
+}) {
+ const layerRef = useRef<HTMLDivElement>(null);
+ const columnCount = (columns ?? 1) > 1 ? columns : undefined;
+
+ useLayoutEffect(() => {
+ const layer = layerRef.current;
+ if (!layer) return;
+
+ const placement = handleRightToLeftPlacement("right") === "right" ? "right-start" : "left-start";
+ return autoUpdate(submenu.anchor, layer, () => void placeMenu(layer, submenu.anchor, placement, true));
+ }, [ submenu.anchor ]);
+
+ // In a `.dropdown-submenu`, so the theme's submenu rules apply.
+ return (
+ <MenuLevelContext.Provider value={level}>
+ <div className="dropdown-submenu">
+ <div
+ ref={layerRef} role="menu" aria-labelledby={titleId(submenu.anchor.id)}
+ className={clsx("dropdown-menu show tn-menu", submenu.immediate && "tn-menu-immediate")}
+ >
+ {/* Like the top level, so the blur on the layer's `::before` stays behind its rows. */}
+ {columnCount
+ // The columns go on an inner list of their full height, so a capped menu
+ // scrolls them rather than growing more columns to the side.
+ ? (
+ <div className="tn-menu-scroll">
+ <menu className="tn-menu-columns" role="none" style={{ columnCount }}>
+ {children}
+ </menu>
+ </div>
+ )
+ : <menu className="tn-menu-scroll" role="none">{children}</menu>}
+ </div>
+ </div>
+ </MenuLevelContext.Provider>
+ );
+}
+
+/** The id of a row's title, which names the submenu it opens. */
+function titleId(rowId: string) {
+ return `${rowId}-title`;
+}
+
+/**
+ * The shortcuts of the row's `keyboardShortcut` action as the user configured them, or else its
+ * literal `shortcut`. Read synchronously, so the menu is placed at its final width.
+ */
+function MenuShortcut({ keyboardShortcut, shortcut }: Pick<MenuCommandProps, "keyboardShortcut" | "shortcut">) {
+ if (keyboardShortcut) {
+ const shortcuts = getActionSync(keyboardShortcut)?.effectiveShortcuts;
+ if (!shortcuts?.length) return null;
+ return (
+ <span className="keyboard-shortcut">
+ {joinElements(shortcuts.map(shortcut => renderShortcutKbds(shortcut)), ",")}
+ </span>
+ );
+ }
+
+ return shortcut ? <kbd>{shortcut}</kbd> : null;
+}
+
+/**
+ * The icon, or a check mark in its place. A row that sets `uiIcon` to nothing gets a blank slot,
+ * and one without `uiIcon` or `checked` an empty one.
+ */
+function MenuIconSlot(props: Pick<MenuCommandProps, "uiIcon" | "checked" | "iconColorClass">) {
+ if (!("uiIcon" in props || "checked" in props)) return <span />;
+
+ const icon = props.checked ? "bx bx-check" : props.uiIcon;
+ return icon
+ ? <span className={clsx(icon, "tn-icon", props.iconColorClass)} />
+ : <span>{" "}</span>;
+}
+
+/**
+ * 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;
+}