Skip to content

Build your personal knowledge base with Trilium Notes

feat(menu): keep focus on a press and run a menu row on its release

Elian Doran1f79cda7d763
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index 87860c8a96..86a0da9f3f 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -247,8 +247,9 @@ describe("contextMenu", () => {
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.
+ // The stylesheets keep the pointer off it; a click that still arrives does nothing.
rows[0].dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 }));
+ rows[0].dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 }));
expect(handler).not.toHaveBeenCalled();
expect(selectMenuItemHandler).not.toHaveBeenCalled();
expect(contextMenu.isShown).toBe(true);
@@ -416,12 +417,17 @@ describe("contextMenu", () => {
expect(calls).toEqual([]);
expect(contextMenu.isShown).toBe(true);
+ // The press does not move focus, so a text editor keeps its selection for the command,
+ // and runs nothing: the row runs on its release, as in a native menu.
const press = new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 });
row.dispatchEvent(press);
+ expect(press.defaultPrevented).toBe(true);
+ expect(calls).toEqual([]);
+ expect(contextMenu.isShown).toBe(true);
+
+ row.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 }));
expect(calls).toEqual([ "handler", "select Copy" ]);
expect(contextMenu.isShown).toBe(false);
- // The press does not move focus, so a text editor keeps its selection for the command.
- expect(press.defaultPrevented).toBe(true);
});
});
@@ -459,8 +465,11 @@ describe("contextMenu", () => {
}
const hover = (element: HTMLElement) => element.dispatchEvent(new PointerEvent("pointerenter"));
- const press = (element: HTMLElement) =>
+ /** A press and its release, which runs a row. */
+ const press = (element: HTMLElement) => {
element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 }));
+ element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 }));
+ };
afterEach(() => {
layout.onMobile = false;
diff --git a/apps/client/src/widgets/react/FormList.spec.tsx b/apps/client/src/widgets/react/FormList.spec.tsx
index 0b2548f8ec..ed0c76fda4 100644
--- a/apps/client/src/widgets/react/FormList.spec.tsx
+++ b/apps/client/src/widgets/react/FormList.spec.tsx
@@ -75,6 +75,56 @@ describe("FormListItem", () => {
});
});
+describe("FormListItem presses", () => {
+ function renderRow(props: { disabled?: boolean } = {}) {
+ const host = document.createElement("div");
+ const onClick = vi.fn();
+ render((
+ <menu>
+ <FormListItem {...props} onClick={onClick}>Size <input className="size" /><input type="checkbox" /></FormListItem>
+ </menu>
+ ), host);
+ const row = host.querySelector<HTMLElement>("li.dropdown-item");
+ if (!row) throw new Error("expected the row");
+ return { row, onClick };
+ }
+ const mousedown = (target: Element, button = 0) => {
+ const event = new MouseEvent("mousedown", { bubbles: true, cancelable: true, button });
+ target.dispatchEvent(event);
+ return event;
+ };
+
+ it("keeps focus where it was on a press, and runs on its release", () => {
+ const { row, onClick } = renderRow();
+
+ expect(mousedown(row).defaultPrevented).toBe(true);
+ // As a switch, which works by its click.
+ const checkbox = row.querySelector("input[type=checkbox]");
+ if (!checkbox) throw new Error("expected the checkbox");
+ expect(mousedown(checkbox).defaultPrevented).toBe(true);
+ expect(onClick).not.toHaveBeenCalled();
+
+ row.click();
+ expect(onClick).toHaveBeenCalledTimes(1);
+ });
+
+ it("lets a field in the row take focus, and a button other than the primary one be", () => {
+ const { row } = renderRow();
+ const field = row.querySelector(".size");
+ if (!field) throw new Error("expected the field");
+
+ expect(mousedown(field).defaultPrevented).toBe(false);
+ expect(mousedown(row, 2).defaultPrevented).toBe(false);
+ });
+
+ it("runs nothing when disabled, though a click reaches it", () => {
+ const { row, onClick } = renderRow({ disabled: true });
+
+ row.click();
+ expect(onClick).not.toHaveBeenCalled();
+ });
+});
+
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.tsx
index a4ce398401..4f1c2e5d9c 100644
--- a/apps/client/src/widgets/react/FormList.tsx
+++ b/apps/client/src/widgets/react/FormList.tsx
@@ -157,6 +157,9 @@ export interface FormListItemOpts {
trailingIcon?: string;
}
+/** What in a row takes focus from a press: the controls that are typed into. */
+const TEXT_ENTRY = "input:not([type='checkbox'], [type='radio']), textarea, select, [contenteditable='true']";
+
const TOOLTIP_CONFIG: Partial<Tooltip.Options> = {
placement: handleRightToLeftPlacement("right"),
fallbackPlacements: [ handleRightToLeftPlacement("right") ],
@@ -220,7 +223,18 @@ export function FormListItem({ className, icon, iconClassName, value, title, act
onPointerMove={menu ? (e) => {
if (!isActive && pointerMoved(e)) onPointed(e);
} : undefined}
- onClick={onClick}
+ // A press keeps focus where it was, as in a native menu: a text editor keeps the
+ // selection a command such as a spelling fix acts on, and a field beside a dropdown
+ // stays in focus. A field inside the row takes focus as it would anywhere.
+ onMouseDown={(e) => {
+ if (e.button === 0 && !(e.target instanceof Element && e.target.closest(TEXT_ENTRY))) e.preventDefault();
+ }}
+ // The row runs on the release, so a press can still be taken back by moving off.
+ onClick={(e) => {
+ if (disabled) return;
+ if (closeOnSelect !== false) menu?.close();
+ onClick?.(e);
+ }}
data-trigger-command={triggerCommand}
dir={rtl ? "rtl" : undefined}
>
diff --git a/apps/client/src/widgets/react/Menu.spec.tsx b/apps/client/src/widgets/react/Menu.spec.tsx
index 470d64f4ca..508b552cc3 100644
--- a/apps/client/src/widgets/react/Menu.spec.tsx
+++ b/apps/client/src/widgets/react/Menu.spec.tsx
@@ -87,8 +87,11 @@ describe("Menu with declared rows", () => {
.map((layer) => [ ...layer.querySelectorAll(":scope > .tn-menu-scroll > li") ].map((item) => item.textContent));
}
- const press = (element: HTMLElement) =>
+ /** A press and its release, which runs a row. */
+ const press = (element: HTMLElement) => {
element.dispatchEvent(new MouseEvent("mousedown", { bubbles: true, cancelable: true, button: 0 }));
+ element.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, button: 0 }));
+ };
const key = (menu: HTMLElement, name: string) =>
menu.dispatchEvent(new KeyboardEvent("keydown", { key: name, bubbles: true, cancelable: true }));