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): draw a menu's separators with FormDropdownDivider
Elian Doranc554c6c66020
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex 3570c8b01e..5c5e8146e8 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -4,7 +4,6 @@ import { h, JSX, render } from "preact";import note_tooltip from "../services/note_tooltip.js";import utils from "../services/utils.js";-import Menu from "../widgets/react/Menu";import { suspendModalFocusTraps } from "../widgets/react/modal_focustrap";export interface ContextMenuOptions<T> {@@ -130,6 +129,10 @@ class ContextMenu {await this.hide();}+ // Loaded here rather than imported: the core services that import this module would+ // otherwise reach the widget tree through the rows `Menu` draws, in a cycle.+ const { default: Menu } = await import("../widgets/react/Menu");+this.options = options;this.focusBeforeShow = document.activeElement;// The menu takes focus, which a modal's focus trap would otherwise pull back into the modal.diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsxindex 38f5ec5492..3f712381f2 100644--- a/apps/client/src/widgets/react/FormList.spec.tsx+++ b/apps/client/src/widgets/react/FormList.spec.tsx@@ -8,7 +8,23 @@ vi.mock("bootstrap", () => ({Tooltip: class { static getInstance() { return null; } }}));-import FormList, { FormListItem } from "./FormList";+import FormList, { FormDropdownDivider, FormListItem } from "./FormList";++describe("FormDropdownDivider", () => {+ it("draws a separator among the rows, whose click closes no menu", () => {+ const host = document.createElement("div");+ render(<menu><FormDropdownDivider /></menu>, host);+ const divider = host.querySelector(".dropdown-divider");+ const pageHeard = vi.fn();+ host.addEventListener("click", pageHeard);++ // A list item, as the menu's rows are, and a separator to assistive technology.+ expect(divider?.tagName).toBe("LI");+ expect(divider?.getAttribute("role")).toBe("separator");+ divider?.dispatchEvent(new MouseEvent("click", { bubbles: true }));+ expect(pageHeard).not.toHaveBeenCalled();+ });+});describe("FormList keyboard activation", () => {it.each([ "Enter", " " ])("activates the focused item on %j like a click", (key) => {diff --git a/apps/client/src/widgets/react/FormList.tsx b/apps/client/src/widgets/react/FormList.tsxindex 4608e045c3..9afb6cf95d 100644--- a/apps/client/src/widgets/react/FormList.tsx+++ b/apps/client/src/widgets/react/FormList.tsx@@ -247,11 +247,9 @@ export function FormListHeader({ text, action }: FormListHeaderOpts) {);}+/** A line between groups of rows. A click on it closes no menu. */export function FormDropdownDivider() {- return <div- className="dropdown-divider"- onClick={e => e.stopPropagation()}- />;+ return <li className="dropdown-divider" role="separator" onClick={(e) => e.stopPropagation()} />;}export function FormDropdownSubmenu({ icon, title, children, dropStart, onDropdownToggleClicked }: {diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsxindex aadb8b507f..bae649ab6b 100644--- a/apps/client/src/widgets/react/Menu.spec.tsx+++ b/apps/client/src/widgets/react/Menu.spec.tsx@@ -1,7 +1,8 @@import { render } from "preact";import { afterEach, describe, expect, it, vi } from "vitest";-import Menu, { MenuCommand, MenuHeader, MenuSeparator, rowInNextColumn } from "./Menu";+import { FormDropdownDivider } from "./FormList";+import Menu, { MenuCommand, MenuHeader, rowInNextColumn } from "./Menu";/** A row 20px tall, `left` into the menu and `top` down it. */function row(left: number, top: number) {@@ -63,7 +64,7 @@ describe("Menu with declared rows", () => {<MenuHeader title="Note" /><MenuCommand title="Copy" keyboardShortcut={undefined} onSelect={() => calls.push("copy")} /><MenuCommand title="Delete" enabled={false} onSelect={() => calls.push("delete")} />- <MenuSeparator />+ <FormDropdownDivider /><Advanced /></Menu>), host);diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 297beaf5ef..adc0b8475f 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -9,6 +9,7 @@ import { useCallback, useContext, useId, useLayoutEffect, useRef, useState } froimport type { MenuCommandItem, MenuItem } from "../../menus/context_menu";import { getActionSync } from "../../services/keyboard_actions";import { handleRightToLeftPlacement, isMobile } from "../../services/utils";+import { FormDropdownDivider } from "./FormList";import { joinElements } from "./react_utils";import { renderShortcutKbds } from "./shortcut_kbd";@@ -18,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`,- * `MenuSeparator`, `MenuHeader` and `MenuCustom`. A `MenuCommand` with children opens them as its+ * `FormDropdownDivider`, `MenuHeader` and `MenuCustom`. A `MenuCommand` with children opens them as its* submenu.*/export interface MenuProps<T> {@@ -471,10 +472,6 @@ export function MenuCommand(props: MenuCommandProps) {);}-export function MenuSeparator() {- return <li className="dropdown-divider" role="separator" />;-}-/** 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.@@ -593,7 +590,7 @@ 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 <MenuSeparator />;+ if (row.kind === "separator") return <FormDropdownDivider />;if (row.kind === "header") return <MenuHeader title={row.title} />;return <MenuCustom><row.componentFn /></MenuCustom>;}