Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): draw the context menu as <menu> lists, with checkable rows and named submenus

Elian Doran7543405c69fd
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 4f260d700a..cd6b2e6c1c 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -111,7 +111,7 @@ describe("contextMenu", () => {
expect(rows).toEqual([
"li.dropdown-item Cut",
"li.dropdown-item destructive-action Copy",
- "div.dropdown-divider ",
+ "li.dropdown-divider ",
"li.dropdown-item Paste"
]);
});
@@ -174,12 +174,13 @@ describe("contextMenu", () => {
});
const shortcuts = [ ...menuElement()?.querySelectorAll("li.dropdown-item > span") ?? [] ]
- .map(row => row.lastElementChild?.outerHTML);
+ .map(row => row.lastElementChild)
+ .map(last => last?.id.endsWith("-title") ? `title ${last.textContent}` : last?.outerHTML);
expect(shortcuts).toEqual([
`<span class="keyboard-shortcut"><kbd>Ctrl</kbd>+<kbd>C</kbd>,<kbd>Ctrl</kbd>+<kbd>Insert</kbd></span>`,
"<kbd>Ctrl+V</kbd>",
- // An action with no shortcut assigned shows none.
- `<span>Cut</span>`
+ // An action with no shortcut assigned shows none: the row ends with its title.
+ "title Cut"
]);
});
@@ -207,7 +208,7 @@ describe("contextMenu", () => {
const rows = menuRows().map(row => `${row.tagName.toLowerCase()}.${row.className}`);
expect(rows).toEqual([
- "li.dropdown-item", "div.dropdown-divider", "li.dropdown-custom-item", "div.dropdown-divider", "li.dropdown-item"
+ "li.dropdown-item", "li.dropdown-divider", "li.dropdown-custom-item", "li.dropdown-divider", "li.dropdown-item"
]);
const custom = menuElement()?.querySelector<HTMLElement>(".dropdown-custom-item");
expect(custom?.innerHTML).toBe(`<div class="swatches"><button type="button">red</button></div>`);
@@ -268,12 +269,16 @@ describe("contextMenu", () => {
]
});
- const rows = menuRows()
- .map(row => `${row.tagName.toLowerCase()}.${row.className} ${row.innerHTML.includes("<b>") ? "(markup)" : row.textContent}`);
+ const rows = menuRows().map((row) => {
+ // A header is a list item holding the heading, as `FormListHeader` draws one.
+ const shown = row.querySelector(":scope > h6.dropdown-header") ?? row;
+ const tag = shown === row ? `li.${row.className}` : "h6.dropdown-header";
+ return `${tag} ${shown.innerHTML.includes("<b>") ? "(markup)" : shown.textContent}`;
+ });
expect(rows).toEqual([
"h6.dropdown-header <b>Doing</b>",
"li.dropdown-item Move left",
- "div.dropdown-divider ",
+ "li.dropdown-divider ",
"h6.dropdown-header Colour",
"li.dropdown-item Red"
]);
@@ -312,6 +317,65 @@ describe("contextMenu", () => {
expect(doing).toHaveLength(3);
});
+ it("draws its rows as the list items of <menu> elements, at every level", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler: () => {},
+ items: [
+ { kind: "header", title: "Colour" },
+ { kind: "custom", componentFn: () => null },
+ { kind: "separator" },
+ { title: "Insert child note", columns: 2, items: [
+ { title: "Text" }, { kind: "separator" }, { title: "Code" }, { title: "Weekly" }
+ ] }
+ ]
+ });
+ const list = menuElement()?.querySelector(":scope > .tn-menu-scroll");
+ expect(list?.tagName).toBe("MENU");
+ // Out of the accessibility tree, so the rows are the menu's own.
+ expect(list?.getAttribute("role")).toBe("none");
+ expect(menuRows().map((row) => `${row.tagName} ${row.getAttribute("role")}`)).toEqual([
+ "LI none", "LI none", "LI separator", "LI menuitem"
+ ]);
+
+ const parent = menuRows().at(-1);
+ parent?.dispatchEvent(new PointerEvent("pointerenter"));
+ await vi.waitFor(() => expect(menuElement()?.querySelector(".tn-menu-columns")).not.toBeNull());
+ const columns = menuElement()?.querySelector(".tn-menu-columns");
+ expect([ columns?.tagName, columns?.getAttribute("role") ]).toEqual([ "MENU", "none" ]);
+ // A group a column must not break is a list item holding a list of its own.
+ const group = columns?.querySelector(":scope > .dropdown-no-break");
+ expect([ group?.tagName, group?.getAttribute("role") ]).toEqual([ "LI", "none" ]);
+ expect(group?.querySelector(":scope > menu")?.getAttribute("role")).toBe("none");
+ for (const element of menuElement()?.querySelectorAll("menu") ?? []) {
+ expect([ ...element.children ].every((child) => child.tagName === "LI")).toBe(true);
+ }
+ });
+
+ it("tells assistive technology which rows are checked and what each submenu belongs to", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler: () => {},
+ items: [
+ { title: "Bold", checked: true },
+ { title: "Italic", checked: false },
+ { title: "Templates", items: [ { title: "Meeting" } ] }
+ ]
+ });
+ expect(menuRows().map((row) => [ row.getAttribute("role"), row.getAttribute("aria-checked") ])).toEqual([
+ [ "menuitemcheckbox", "true" ], [ "menuitemcheckbox", "false" ], [ "menuitem", null ]
+ ]);
+
+ menuRows()[2]?.dispatchEvent(new PointerEvent("pointerenter"));
+ await vi.waitFor(() => expect(menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu")).not.toBeNull());
+ const layer = menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu");
+ expect(layer?.getAttribute("role")).toBe("menu");
+ const label = document.getElementById(layer?.getAttribute("aria-labelledby") ?? "");
+ expect(label?.textContent).toBe("Templates");
+ });
+
it("runs an item pressed with the primary button, then hides", async () => {
buildPage();
const contextMenu = await buildContextMenu();
@@ -368,7 +432,7 @@ describe("contextMenu", () => {
/** The submenu layers standing open, each as the titles of its rows. */
function layers() {
- return [ ...menuElement()?.querySelectorAll(":scope > div.dropdown-submenu > ul.dropdown-menu") ?? [] ]
+ return [ ...menuElement()?.querySelectorAll(":scope > div.dropdown-submenu > .dropdown-menu") ?? [] ]
.map((layer) => [ ...layer.querySelectorAll(":scope > .tn-menu-scroll > li") ].map((item) => item.textContent));
}
@@ -416,7 +480,7 @@ describe("contextMenu", () => {
await vi.waitFor(() => expect(layers()).toEqual([ [ "Meeting" ] ]));
expect(scroller?.querySelector("div.dropdown-submenu")).toBeNull();
// A layer scrolls its rows the same way, so its own blur stays behind them too.
- const layer = menuElement()?.querySelector("div.dropdown-submenu > ul");
+ const layer = menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu");
expect([ ...layer?.children ?? [] ].map((child) => child.className)).toEqual([ "tn-menu-scroll" ]);
});
@@ -424,8 +488,14 @@ describe("contextMenu", () => {
vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(1000);
vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);
// Only the layer: a size unlike its rect would read to Floating UI as a CSS scale.
- vi.spyOn(HTMLUListElement.prototype, "offsetWidth", "get").mockReturnValue(150);
- vi.spyOn(HTMLUListElement.prototype, "offsetHeight", "get").mockReturnValue(100);
+ const layerSize = { width: 150, height: 100 };
+ const isLayer = (element: HTMLElement) => element.matches("div.dropdown-submenu > .dropdown-menu");
+ vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockImplementation(function (this: HTMLElement) {
+ return isLayer(this) ? layerSize.width : 0;
+ });
+ vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockImplementation(function (this: HTMLElement) {
+ return isLayer(this) ? layerSize.height : 0;
+ });
await openMenu();
const root = menuElement()?.querySelector<HTMLElement>(":scope > .tn-menu-scroll");
if (!root) throw new Error("expected the menu to render");
@@ -440,7 +510,7 @@ describe("contextMenu", () => {
vi.spyOn(parent, "getBoundingClientRect").mockImplementation(() => rowRect);
hover(parent);
- const layer = () => menuElement()?.querySelector<HTMLElement>("div.dropdown-submenu > ul");
+ const layer = () => menuElement()?.querySelector<HTMLElement>("div.dropdown-submenu > .dropdown-menu");
// Overlapping the row by 2px, its top at the row's.
await vi.waitFor(() => expect([ layer()?.style.left, layer()?.style.top ]).toEqual([ "208px", "100px" ]));
expect(layer()?.style.visibility).toBe("visible");
@@ -457,7 +527,7 @@ describe("contextMenu", () => {
// Too wide for either side of its row, it is shifted inside the viewport rather than
// left clipped where it opens.
- vi.spyOn(HTMLUListElement.prototype, "offsetWidth", "get").mockReturnValue(900);
+ layerSize.width = 900;
rowRect = DOMRect.fromRect({ x: 400, y: 100, width: 200, height: 24 });
root.dispatchEvent(new Event("scroll"));
await vi.waitFor(() => {
@@ -469,7 +539,7 @@ describe("contextMenu", () => {
it("waits before the first submenu it opens under the pointer, and not before one that replaces it", async () => {
/** Whether the open layer skips the stylesheet's opening delay and fade. */
- const immediate = () => menuElement()?.querySelector("div.dropdown-submenu > ul")?.classList.contains("tn-menu-immediate");
+ const immediate = () => menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu")?.classList.contains("tn-menu-immediate");
await openMenu();
// The pointer might only be passing over the row.
@@ -520,7 +590,7 @@ describe("contextMenu", () => {
});
hover(row("Insert child note"));
- await vi.waitFor(() => expect(menuElement()?.querySelector("div.dropdown-submenu > ul > .tn-menu-scroll > .tn-menu-columns")).not.toBeNull());
+ await vi.waitFor(() => expect(menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu > .tn-menu-scroll > .tn-menu-columns")).not.toBeNull());
const columns = menuElement()?.querySelector<HTMLElement>(".tn-menu-columns");
expect(columns?.style.columnCount).toBe("2");
expect(columns?.textContent).toBe("TextCode");
@@ -545,7 +615,7 @@ describe("contextMenu", () => {
/** The layer's rows, a group of rows that must not break as the list of its rows. */
const layout = (list: Element | null | undefined) => [ ...list?.children ?? [] ].map((child) =>
child.classList.contains("dropdown-no-break")
- ? [ ...child.children ].map((grouped) => grouped.textContent || "---")
+ ? [ ...child.querySelector(":scope > menu")?.children ?? [] ].map((grouped) => grouped.textContent || "---")
: child.textContent || "---");
hover(row("Insert child note"));
@@ -557,7 +627,7 @@ describe("contextMenu", () => {
// A single column has no breaks to avoid, so nothing is grouped.
hover(row("Insert note after"));
await vi.waitFor(() => expect(menuElement()?.querySelector(".tn-menu-columns")).toBeNull());
- expect(layout(menuElement()?.querySelector("div.dropdown-submenu > ul > .tn-menu-scroll"))).toEqual([
+ expect(layout(menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu > .tn-menu-scroll"))).toEqual([
"Text", "Code", "---", "Templates", "Meeting", "Weekly"
]);
});
@@ -747,7 +817,7 @@ describe("contextMenu", () => {
key("ArrowRight");
await vi.waitFor(() => expect(activeRow()).toBe("Meeting"));
// A key is a choice, so the submenu shows without the stylesheet's opening delay.
- expect(menuElement()?.querySelector("div.dropdown-submenu > ul")?.classList.contains("tn-menu-immediate")).toBe(true);
+ expect(menuElement()?.querySelector("div.dropdown-submenu > .dropdown-menu")?.classList.contains("tn-menu-immediate")).toBe(true);
key("ArrowLeft");
await vi.waitFor(() => expect(activeRow()).toBe("Templates"));
diff --git a/apps/client/src/widgets/react/Menu.css b/apps/client/src/widgets/react/Menu.css
index c301aa3bc1..f3ff5e1bca 100644
--- a/apps/client/src/widgets/react/Menu.css
+++ b/apps/client/src/widgets/react/Menu.css
@@ -16,6 +16,16 @@
flex-direction: column;
}
+/*
+ * The lists that hold the rows take none of the browser's list styling. `:where()` keeps this
+ * below `.tn-menu-scroll`, whose inline padding it must not undo.
+ */
+:where(.tn-menu) menu {
+ margin: 0;
+ padding: 0;
+ list-style: none;
+}
+
.tn-menu-scroll {
/* Matches the menu's own padding in the theme's `.dropdown-menu:not(.static)` rule. */
--tn-menu-inline-padding: var(--padding, var(--menu-padding-size));
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index 0d71671a99..618bf0aed6 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -292,10 +292,11 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
}}
>
{/* The rows scroll in here rather than the menu itself, so the theme's blur on the
- menu's `::before` stays behind them. */}
- <div className="tn-menu-scroll">
+ menu's `::before` stays behind them. A `<menu>`, as rows are its list items, out of
+ the accessibility tree so they read as the menu's own. */}
+ <menu className="tn-menu-scroll" role="none">
<MenuList level={0} items={items} state={state} />
- </div>
+ </menu>
{/* Inside the menu, so the rules scoped to it apply, but none inside another: a fixed
layer escapes a scrolling menu only while no ancestor carries a filter. */}
{submenus.map((submenu, index) => (
@@ -310,7 +311,7 @@ export default function Menu<T>({ id, className, x, y, orientation, bottomSheet,
* so a scrolling menu neither clips it nor scrolls it away.
*/
function SubmenuLayer<T>({ level, submenu, state }: { level: number, submenu: OpenSubmenu<T>, state: MenuState<T> }) {
- const layerRef = useRef<HTMLUListElement>(null);
+ const layerRef = useRef<HTMLDivElement>(null);
const columns = (submenu.item.columns ?? 1) > 1 ? submenu.item.columns : undefined;
const items = submenu.item.items ?? [];
@@ -325,20 +326,27 @@ function SubmenuLayer<T>({ level, submenu, state }: { level: number, submenu: Op
// In a `.dropdown-submenu`, so the theme's submenu rules apply.
return (
<div className="dropdown-submenu">
- <ul ref={layerRef} className={clsx("dropdown-menu show tn-menu", submenu.immediate && "tn-menu-immediate")} role="menu">
+ <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. */}
- <div className="tn-menu-scroll">
- {columns
- // The columns go on an inner element of their full height, so a capped menu
- // scrolls them rather than growing more columns to the side.
- ? (
- <div className="tn-menu-columns" style={{ columnCount: columns }}>
+ {columns
+ // 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: columns }}>
<MenuList level={level} items={items} state={state} columns />
- </div>
- )
- : <MenuList level={level} items={items} state={state} />}
- </div>
- </ul>
+ </menu>
+ </div>
+ )
+ : (
+ <menu className="tn-menu-scroll" role="none">
+ <MenuList level={level} items={items} state={state} />
+ </menu>
+ )}
+ </div>
</div>
);
}
@@ -356,9 +364,11 @@ function MenuList<T>({ level, items, state, columns }: {
return <>
{unbreakableGroups(rows).map((group, groupIndex) => (group.length > 1
? (
- <div key={groupIndex} className="dropdown-no-break">
- {group.map((row, index) => <MenuListRow key={index} level={level} row={row} state={state} />)}
- </div>
+ <li key={groupIndex} className="dropdown-no-break" role="none">
+ <menu role="none">
+ {group.map((row, index) => <MenuListRow key={index} level={level} row={row} state={state} />)}
+ </menu>
+ </li>
)
: <MenuListRow key={groupIndex} level={level} row={group[0]} state={state} />
))}
@@ -367,11 +377,14 @@ function MenuList<T>({ level, items, state, columns }: {
function MenuListRow<T>({ level, row, state }: { level: number, row: MenuItem<T>, state: MenuState<T> }) {
if (!("kind" in row)) return <MenuRow level={level} item={row} state={state} />;
- if (row.kind === "separator") return <div className="dropdown-divider" role="separator" />;
- if (row.kind === "header") return <h6 className="dropdown-header">{row.title}</h6>;
+ if (row.kind === "separator") return <li className="dropdown-divider" role="separator" />;
+ // As `FormListHeader` draws one.
+ if (row.kind === "header") return <li role="none"><h6 className="dropdown-header">{row.title}</h6></li>;
return (
<li
className="dropdown-custom-item"
+ // Its content carries the roles of what it acts with.
+ role="none"
ref={(element) => {
if (element) state.rows.set(row, element);
else state.rows.delete(row);
@@ -395,6 +408,8 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandIt
const open = isMobile() ? unfolded : state.openItems[level] === item;
const active = state.active?.level === level && state.active.item === item;
const id = useId();
+ // As `MenuIconSlot` reads it: an item with a `checked` key is one that can be checked.
+ const checkable = "checked" in item;
function onPointed(e: { currentTarget: HTMLLIElement }) {
if (isMobile()) return;
@@ -412,7 +427,8 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandIt
}}
className={clsx("dropdown-item", hasSubmenu && "dropdown-submenu", open && "submenu-open",
active && "tn-menu-active", disabled && "disabled", item.className)}
- role="menuitem"
+ role={checkable ? "menuitemcheckbox" : "menuitem"}
+ aria-checked={checkable ? !!item.checked : undefined}
aria-disabled={disabled || undefined}
aria-haspopup={hasSubmenu ? "menu" : undefined}
aria-expanded={hasSubmenu ? open : undefined}
@@ -448,7 +464,7 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandIt
<MenuIconSlot item={item} />
<span className="tn-menu-gap" />
{/* Callers pass HTML: titles escaped with `escapeHtml()` or boxed by `menuName()`. */}
- <span dangerouslySetInnerHTML={{ __html: item.title }} />
+ <span id={titleId(id)} dangerouslySetInnerHTML={{ __html: item.title }} />
{item.badges?.map((badge, index) => (
<span key={index} className={clsx("badge", badge.className)}>{badge.title}</span>
))}
@@ -456,7 +472,7 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandIt
{item.trailingIcon && <span className={clsx(item.trailingIcon, "tn-icon", "menu-trailing-icon")} />}
</span>
{hasSubmenu && isMobile() && (
- <ul className={clsx("dropdown-menu", unfolded && "show")} role="menu">
+ <ul className={clsx("dropdown-menu", unfolded && "show")} role="menu" aria-labelledby={titleId(id)}>
{unfolded && <MenuList level={level + 1} items={item.items ?? []} state={state} />}
</ul>
)}
@@ -464,6 +480,11 @@ function MenuRow<T>({ level, item, state }: { level: number, item: MenuCommandIt
);
}
+/** The id of a row's title, which names the submenu it opens. */
+function titleId(rowId: string) {
+ return `${rowId}-title`;
+}
+
/**
* The shortcuts of the item's `keyboardShortcut` action as the user configured them, or else its
* literal `shortcut`. Read synchronously, so the menu is placed at its final width.