Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): show shortcuts and a trailing icon on FormListItem

Elian Doranb378c34c3b76
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsx
index 53204a5db6..0b2548f8ec 100644
--- a/apps/client/src/widgets/react/FormList.spec.tsx
+++ b/apps/client/src/widgets/react/FormList.spec.tsx
@@ -54,10 +54,25 @@ describe("FormListItem", () => {
expect(span?.tagName).toBe("SPAN");
expect(span?.className).toBe("");
expect([ ...span?.children ?? [] ].map((child) => child.className)).toEqual([
- "bx bx-copy use-note-color tn-icon", "tn-menu-gap", "badge "
+ "bx bx-copy use-note-color tn-icon", "tn-menu-gap", "badge"
]);
expect(span?.textContent).toBe("Copynew");
});
+
+ it("ends its row with a shortcut, then a trailing icon, leaving its own icon standing", () => {
+ const host = document.createElement("div");
+ render((
+ <menu>
+ <FormListItem icon="bx bx-sort" shortcut="Ctrl+S" trailingIcon="bx bx-check">By title</FormListItem>
+ </menu>
+ ), host);
+ const span = host.querySelector("li.dropdown-item > span");
+
+ expect([ ...span?.children ?? [] ].map((child) => `${child.tagName.toLowerCase()}.${child.className}`)).toEqual([
+ "span.bx bx-sort tn-icon", "span.tn-menu-gap", "kbd.", "span.bx bx-check tn-icon menu-trailing-icon"
+ ]);
+ expect(span?.textContent).toBe("By titleCtrl+S");
+ });
});
describe("FormListCustomItem", () => {
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsx
index e28ce5cb2b..16e46db190 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -1,17 +1,21 @@
import "./FormList.css";
+import type { KeyboardActionNames } from "@triliumnext/commons";
import { Dropdown as BootstrapDropdown, Tooltip } from "bootstrap";
import clsx from "clsx";
import { ComponentChildren, RefObject } from "preact";
import { type CSSProperties, useContext, useEffect, useId, useMemo, useRef, useState } from "preact/compat";
import { CommandNames } from "../../components/app_context";
+import { getActionSync } from "../../services/keyboard_actions";
import { handleRightToLeftPlacement, isMobile, openInAppHelpFromUrl } from "../../services/utils";
import FormToggle from "./FormToggle";
import HelpTooltipButton from "./HelpTooltipButton";
import { useStaticTooltip, useSyncedRef } from "./hooks";
import Icon from "./Icon";
import { MenuContext, MenuLevelContext } from "./menu_context";
+import { joinElements } from "./react_utils";
+import { renderShortcutKbds } from "./shortcut_kbd";
interface FormListOpts {
children: ComponentChildren;
@@ -131,6 +135,16 @@ export interface FormListItemOpts {
rtl?: boolean;
postContent?: ComponentChildren;
itemRef?: RefObject<HTMLLIElement>;
+ /** 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. */
+ shortcut?: string;
+ /**
+ * An icon at the end of the row, where a shortcut would go. Unlike {@link checked}, which takes
+ * the place of {@link icon}, this leaves the row's own icon standing, for a list where that icon
+ * is what tells one row from another.
+ */
+ trailingIcon?: string;
}
const TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
@@ -139,7 +153,7 @@ 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, ...contentProps }: FormListItemOpts) {
+export function FormListItem({ className, icon, iconClassName, value, title, active, disabled, checked, container, onClick, selected, rtl, triggerCommand, description, itemRef: externalItemRef, keyboardShortcut, shortcut, trailingIcon, ...contentProps }: FormListItemOpts) {
const itemRef = useSyncedRef<HTMLLIElement>(externalItemRef, null);
if (checked) {
@@ -172,6 +186,8 @@ export function FormListItem({ className, icon, iconClassName, value, title, act
) : (
<FormListContent description={description} disabled={disabled} {...contentProps} />
)}
+ <FormListShortcut keyboardShortcut={keyboardShortcut} shortcut={shortcut} />
+ {trailingIcon && <span className={clsx(trailingIcon, "tn-icon", "menu-trailing-icon")} />}
</span>
</li>
);
@@ -229,7 +245,7 @@ function FormListContent({ children, badges, description, disabled, disabledTool
return <>
{children}
{badges && badges.map(({ className, text }) => (
- <span className={`badge ${className ?? ""}`}>{text}</span>
+ <span className={clsx("badge", className)}>{text}</span>
))}
{disabled && disabledTooltip && (
<span class="bx bx-info-circle contextual-help" title={disabledTooltip} />
@@ -339,3 +355,21 @@ function actsOnClick(target: EventTarget | null, row: HTMLElement) {
return !!acting && row.contains(acting) && acting !== row
&& acting.getAttribute("aria-disabled") !== "true" && !acting.matches(":disabled");
}
+
+/**
+ * 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 FormListShortcut({ keyboardShortcut, shortcut }: Pick<FormListItemOpts, "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;
+}
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 6c8990a406..e5595d44a3 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -336,7 +336,7 @@ function MenuItemRow<T>({ row, onSelect }: { row: MenuItem<T>, onSelect: MenuPro
return <FormListCustomItem><row.componentFn /></FormListCustomItem>;
}
- const { title, uiIcon, iconColorClass, checked, enabled, badges, className, items } = row;
+ const { title, uiIcon, iconColorClass, checked, enabled, badges, className, keyboardShortcut, shortcut, trailingIcon, 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.
@@ -357,6 +357,7 @@ function MenuItemRow<T>({ row, onSelect }: { row: MenuItem<T>, onSelect: MenuPro
icon={"uiIcon" in row || "checked" in row ? uiIcon : null}
iconClassName={iconColorClass} checked={checked} disabled={enabled === false}
className={className} badges={badges?.map((badge) => ({ className: badge.className, text: badge.title }))}
+ keyboardShortcut={keyboardShortcut} shortcut={shortcut} trailingIcon={trailingIcon}
onClick={select}
>
{label}
diff --git a/apps/client/src/widgets/react/Menu_legacy.tsx b/apps/client/src/widgets/react/Menu_legacy.tsx
index f41f3e8957..8b803aaba8 100644
--- a/apps/client/src/widgets/react/Menu_legacy.tsx
+++ b/apps/client/src/widgets/react/Menu_legacy.tsx
@@ -9,12 +9,9 @@ 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"> {
@@ -32,7 +29,7 @@ export interface MenuCommandProps extends Pick<MenuCommandItem<unknown>, "title"
* the icon, and one given `checked` can be checked.
*/
export function MenuCommand(props: MenuCommandProps) {
- const { title, enabled, trailingIcon, className, columns, onSelect, closeOnSelect, children } = props;
+ const { title, enabled, className, columns, onSelect, closeOnSelect, children } = props;
const menu = useMenu();
const level = useContext(MenuLevelContext);
const id = useId();
@@ -100,8 +97,6 @@ export function MenuCommand(props: MenuCommandProps) {
<span className={hasSubmenu ? "dropdown-toggle" : undefined}>
{/* Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`. */}
<span id={titleId(id)} dangerouslySetInnerHTML={{ __html: title }} />
- <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.
@@ -169,24 +164,6 @@ 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;
-}
-
/**
* 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