Skip to content

Build your personal knowledge base with Trilium Notes

refactor(menu): draw a menu's headers with FormListHeader

Elian Doran863be3a3c3a3
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsx
index 3f712381f2..46e7b0d223 100644
--- a/apps/client/src/widgets/react/FormList.spec.tsx
+++ b/apps/client/src/widgets/react/FormList.spec.tsx
@@ -8,7 +8,7 @@ vi.mock("bootstrap", () => ({
Tooltip: class { static getInstance() { return null; } }
}));
-import FormList, { FormDropdownDivider, FormListItem } from "./FormList";
+import FormList, { FormDropdownDivider, FormListHeader, FormListItem } from "./FormList";
describe("FormDropdownDivider", () => {
it("draws a separator among the rows, whose click closes no menu", () => {
@@ -26,6 +26,19 @@ describe("FormDropdownDivider", () => {
});
});
+describe("FormListHeader", () => {
+ it("heads the rows after it as text, out of the list's own roles", () => {
+ const host = document.createElement("div");
+ // A name the user wrote, such as a board column's status.
+ render(<menu><FormListHeader text="<b>Doing</b>" /></menu>, host);
+ const row = host.querySelector("menu > li");
+
+ // Its heading speaks for it, so the row reads as no list item of the menu's.
+ expect(row?.getAttribute("role")).toBe("none");
+ expect(row?.querySelector("h6.dropdown-header")?.textContent).toBe("<b>Doing</b>");
+ });
+});
+
describe("FormList keyboard activation", () => {
it.each([ "Enter", " " ])("activates the focused item on %j like a click", (key) => {
const { container, onSelect } = renderList();
diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsx
index 9afb6cf95d..a48aa8c0d9 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -237,9 +237,10 @@ interface FormListHeaderOpts {
}
export function FormListHeader({ text, action }: FormListHeaderOpts) {
+ // Its heading speaks for the row, which a menu does not count among its items.
return (
- <li>
- <h6 className={`dropdown-header ${action ? "dropdown-header-with-action" : ""}`}>
+ <li role="none">
+ <h6 className={clsx("dropdown-header", action && "dropdown-header-with-action")}>
<span>{text}</span>
{action}
</h6>
diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsx
index bae649ab6b..dbab579da1 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 } from "./FormList";
-import Menu, { MenuCommand, MenuHeader, rowInNextColumn } from "./Menu";
+import { FormDropdownDivider, FormListHeader } from "./FormList";
+import Menu, { MenuCommand, rowInNextColumn } from "./Menu";
/** A row 20px tall, `left` into the menu and `top` down it. */
function row(left: number, top: number) {
@@ -61,7 +61,7 @@ describe("Menu with declared rows", () => {
}
render((
<Menu x={10} y={10} onClose={onClose}>
- <MenuHeader title="Note" />
+ <FormListHeader text="Note" />
<MenuCommand title="Copy" keyboardShortcut={undefined} onSelect={() => calls.push("copy")} />
<MenuCommand title="Delete" enabled={false} onSelect={() => calls.push("delete")} />
<FormDropdownDivider />
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index adc0b8475f..691a13ef6f 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -9,7 +9,7 @@ import { useCallback, useContext, useId, useLayoutEffect, useRef, useState } fro
import type { MenuCommandItem, MenuItem } from "../../menus/context_menu";
import { getActionSync } from "../../services/keyboard_actions";
import { handleRightToLeftPlacement, isMobile } from "../../services/utils";
-import { FormDropdownDivider } from "./FormList";
+import { FormDropdownDivider, FormListHeader } from "./FormList";
import { joinElements } from "./react_utils";
import { renderShortcutKbds } from "./shortcut_kbd";
@@ -19,7 +19,7 @@ import { renderShortcutKbds } from "./shortcut_kbd";
* this too. Only the placement is its own.
*
* The rows come as data in {@link items}, or as components in {@link children}: `MenuCommand`,
- * `FormDropdownDivider`, `MenuHeader` and `MenuCustom`. A `MenuCommand` with children opens them as its
+ * `FormDropdownDivider`, `FormListHeader` and `MenuCustom`. A `MenuCommand` with children opens them as its
* submenu.
*/
export interface MenuProps<T> {
@@ -472,12 +472,6 @@ export function MenuCommand(props: MenuCommandProps) {
);
}
-/** A heading over the rows after it. Its title is text, as it can be a name the user wrote. */
-export function MenuHeader({ title }: { title: string }) {
- // As `FormListHeader` draws one.
- return <li role="none"><h6 className="dropdown-header">{title}</h6></li>;
-}
-
/**
* A row holding a control of its own, such as the color picker. The keys stand on it when its
* content marks a way in with `tabindex="0"`, and a click on something in it that acts closes the
@@ -591,7 +585,8 @@ function MenuItems<T>({ items, onSelect, columns }: {
function MenuItemRow<T>({ row, onSelect }: { row: MenuItem<T>, onSelect: MenuProps<T>["onSelect"] }) {
if ("kind" in row) {
if (row.kind === "separator") return <FormDropdownDivider />;
- if (row.kind === "header") return <MenuHeader title={row.title} />;
+ // Its title is text, as it can be a name the user wrote.
+ if (row.kind === "header") return <FormListHeader text={row.title} />;
return <MenuCustom><row.componentFn /></MenuCustom>;
}