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
refactor(menu): lay FormListItem's row out as a menu's row
Elian Doran918e584700c9
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex a72edf903a..7863f35d77 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -151,9 +151,10 @@ describe("contextMenu", () => {return icon?.className || icon?.textContent;});expect(slots).toEqual([- "bx bx-list-ul tn-icon use-note-color color-e64d4d",+ "bx bx-list-ul use-note-color color-e64d4d tn-icon","bx bx-check tn-icon",- "\u00a0",+ // A blank icon of an icon's width, which lines the title up with the others.+ "bx bx-empty tn-icon",""]);// Only the icon is tinted, so the title keeps the menu's own colour.diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 4178e2d6db..9c3f478678 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -542,7 +542,7 @@ body.desktop .tabulator-popup-container,.dropdown-menu:not(#context-menu-container) .dropdown-item,body.desktop .dropdown-menu .dropdown-toggle,-body #context-menu-container .dropdown-item > span,+.dropdown-item > span:not([class]),body.mobile .dropdown .dropdown-submenu > span {display: flex;align-items: center;diff --git a/apps/client/src/stylesheets/theme-next/base.css b/apps/client/src/stylesheets/theme-next/base.cssindex 3bf657245c..a376a7a60b 100644--- a/apps/client/src/stylesheets/theme-next/base.css+++ b/apps/client/src/stylesheets/theme-next/base.css@@ -161,7 +161,8 @@ body.backdrop-effects-disabled {white-space-collapse: discard;}-.dropdown-menu.tn-dropdown-menu .dropdown-item .tn-icon {+/* The row's own icon is spaced by the `.tn-menu-gap` beside it. */+.dropdown-menu.tn-dropdown-menu .dropdown-item .tn-icon:not(.dropdown-item > span > :first-child) {margin-inline-end: 6px;}diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsxindex 585ee540fe..53204a5db6 100644--- a/apps/client/src/widgets/react/FormList.spec.tsx+++ b/apps/client/src/widgets/react/FormList.spec.tsx@@ -39,6 +39,27 @@ describe("FormListHeader", () => {});});+describe("FormListItem", () => {+ it("lays its row out as one span of icon, gap and content, as a menu's rows are", () => {+ const host = document.createElement("div");+ render((+ <menu>+ <FormListItem icon="bx bx-copy" iconClassName="use-note-color" badges={[ { text: "new" } ]}>Copy</FormListItem>+ </menu>+ ), host);+ const row = host.querySelector("li.dropdown-item");++ const [ span, ...others ] = [ ...row?.children ?? [] ];+ expect(others).toEqual([]);+ 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 "+ ]);+ expect(span?.textContent).toBe("Copynew");+ });+});+describe("FormListCustomItem", () => {it("holds a control of its own in a row outside any menu too, where there is none to join", () => {const host = document.createElement("div");diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex ec5952f923..e28ce5cb2b 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -110,7 +110,8 @@ export interface FormListBadge {export interface FormListItemOpts {children: ComponentChildren;- icon?: string;+ /** Without one the row keeps a blank slot, lining it up with the rows that have one; `null` leaves no slot. */+ icon?: string | null;/** Extra class for the icon itself, e.g. to hang a marker off its corner. */iconClassName?: string;value?: string;@@ -150,21 +151,28 @@ export function FormListItem({ className, icon, iconClassName, value, title, actreturn (<liref={itemRef}- class={`dropdown-item ${active ? "active" : ""} ${disabled ? "disabled" : ""} ${selected ? "selected" : ""} ${container ? "dropdown-container-item": ""} ${className ?? ""}`}+ class={clsx("dropdown-item", active && "active", disabled && "disabled", selected && "selected",+ container && "dropdown-container-item", className)}data-value={value} title={title}tabIndex={container ? -1 : 0}onClick={onClick}data-trigger-command={triggerCommand}dir={rtl ? "rtl" : undefined}>- <Icon icon={icon} className={iconClassName} /> - {description ? (- <div>+ {/* One classless span holds the row, as the rows of a menu are laid out. */}+ <span>+ {icon === null ? <span /> : <Icon icon={icon} className={iconClassName} />}+ {/* An element, not spaces: in a flex row, text merges with a plain title but is+ trimmed before one boxed by `menuName()`, indenting the two kinds of row apart. */}+ <span className="tn-menu-gap" />+ {description ? (+ <div>+ <FormListContent description={description} disabled={disabled} {...contentProps} />+ </div>+ ) : (<FormListContent description={description} disabled={disabled} {...contentProps} />- </div>- ) : (- <FormListContent description={description} disabled={disabled} {...contentProps} />- )}+ )}+ </span></li>);}diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex ec5cfae9a2..6c8990a406 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -353,7 +353,9 @@ function MenuItemRow<T>({ row, onSelect }: { row: MenuItem<T>, onSelect: MenuProreturn (<FormListItem- icon={uiIcon} iconClassName={iconColorClass} checked={checked} disabled={enabled === false}+ // 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}className={className} badges={badges?.map((badge) => ({ className: badge.className, text: badge.title }))}onClick={select}>diff --git a/apps/client/src/widgets/react/Menu_legacy.tsx b/apps/client/src/widgets/react/Menu_legacy.tsxindex 5c89e9efcc..f41f3e8957 100644--- a/apps/client/src/widgets/react/Menu_legacy.tsx+++ b/apps/client/src/widgets/react/Menu_legacy.tsx@@ -32,7 +32,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, badges, trailingIcon, className, columns, onSelect, closeOnSelect, children } = props;+ const { title, enabled, trailingIcon, className, columns, onSelect, closeOnSelect, children } = props;const menu = useMenu();const level = useContext(MenuLevelContext);const id = useId();@@ -98,13 +98,8 @@ export function MenuCommand(props: MenuCommandProps) {}}><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>@@ -192,19 +187,6 @@ function MenuShortcut({ keyboardShortcut, shortcut }: Pick<MenuCommandProps, "kereturn 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