Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): disable the context menu items that are not enabled

Elian Doranf3384a77c566
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 27c6a55e9c..02d263e7f1 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -194,6 +194,32 @@ describe("contextMenu", () => {
expect(contextMenu.isShown()).toBe(false);
});
+ it("marks an item that is not enabled as disabled, and does not run it", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ const handler = vi.fn();
+ const selectMenuItemHandler = vi.fn();
+
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler,
+ items: [
+ { title: "Delete", enabled: false, handler },
+ { title: "Export", enabled: true },
+ { title: "Import" }
+ ]
+ });
+
+ const rows = [ ...menuElement()?.querySelectorAll<HTMLElement>("li.dropdown-item") ?? [] ];
+ expect(rows.map(row => [ row.classList.contains("disabled"), row.getAttribute("aria-disabled") ]))
+ .toEqual([ [ true, "true" ], [ false, null ], [ false, null ] ]);
+
+ // The stylesheets keep the pointer off it; a press that still arrives does nothing.
+ rows[0].dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 }));
+ expect(handler).not.toHaveBeenCalled();
+ expect(selectMenuItemHandler).not.toHaveBeenCalled();
+ expect(contextMenu.isShown()).toBe(true);
+ });
+
it("runs an item pressed with the primary button, then hides", async () => {
buildPage();
const contextMenu = await buildContextMenu();
diff --git a/apps/client/src/menus/context_menu_legacy.ts b/apps/client/src/menus/context_menu_legacy.ts
index f544d1d695..4954d93833 100644
--- a/apps/client/src/menus/context_menu_legacy.ts
+++ b/apps/client/src/menus/context_menu_legacy.ts
@@ -284,10 +284,6 @@ class ContextMenu {
return false;
});
- if ("enabled" in item && item.enabled !== undefined && !item.enabled) {
- $item.addClass("disabled");
- }
-
if ("items" in item && item.items) {
$item.addClass("dropdown-submenu");
$link.addClass("dropdown-toggle");
diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsx
index a1c0e85e50..1b2f667bae 100644
--- a/apps/client/src/widgets/react/Menu.tsx
+++ b/apps/client/src/widgets/react/Menu.tsx
@@ -61,16 +61,19 @@ export default function Menu<T>({ id, className, x, y, orientation, items, onSel
}
function MenuRow<T>({ item, onSelect }: { item: MenuCommandItem<T>, onSelect: MenuProps<T>["onSelect"] }) {
+ const disabled = item.enabled === false;
+
return (
<li
- className={clsx("dropdown-item", item.className)}
+ className={clsx("dropdown-item", disabled && "disabled", item.className)}
role="menuitem"
+ aria-disabled={disabled || undefined}
// `mousedown` rather than `click`, and its default prevented, so the press does not move
// focus: a text editor keeps the selection that commands such as a spelling fix act on.
onMouseDown={(e) => {
if (e.button !== 0) return;
e.preventDefault();
- onSelect(item, e);
+ if (!disabled) onSelect(item, e);
}}
>
<span>