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
fix(menu): keep the context menu up for a click beside the color picker's cells
Elian Doran30c0bd96f09b
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.tsindex c76b17d008..a1d228f97e 100644--- a/apps/client/src/menus/context_menu.spec.ts+++ b/apps/client/src/menus/context_menu.spec.ts@@ -183,7 +183,7 @@ describe("contextMenu", () => {]);});- it("renders a custom item's component in its place, and hides on a click inside it", async () => {+ it("renders a custom item's component in its place, and hides on a click on what it acts with", async () => {buildPage();const contextMenu = await buildContextMenu();const { h } = await import("preact");@@ -191,7 +191,7 @@ describe("contextMenu", () => {// A component with state of its own, as the colour pickers are.function Swatches() {const [ picked ] = useState("red");- return h("div", { className: "swatches" }, picked);+ return h("div", { className: "swatches" }, h("button", { type: "button" }, picked));}await contextMenu.show({@@ -210,10 +210,19 @@ describe("contextMenu", () => {"li.dropdown-item", "div.dropdown-divider", "li.dropdown-custom-item", "div.dropdown-divider", "li.dropdown-item"]);const custom = menuElement()?.querySelector<HTMLElement>(".dropdown-custom-item");- expect(custom?.innerHTML).toBe(`<div class="swatches">red</div>`);+ expect(custom?.innerHTML).toBe(`<div class="swatches"><button type="button">red</button></div>`);++ // A click that misses what the component acts with, such as one in the gaps between+ // the color picker's cells, picks nothing and leaves the menu up.+ const click = (target: Element | null | undefined) =>+ target?.dispatchEvent(new MouseEvent("click", { bubbles: true }));+ click(custom?.querySelector(".swatches"));+ click(custom);+ expect(contextMenu.isShown).toBe(true);- // On its own, without reaching the page's listener: on mobile only the cover has one.- custom?.dispatchEvent(new MouseEvent("click", { bubbles: false }));+ // One that acts closes the menu of itself, without the page's listener: on mobile only+ // the cover has one.+ custom?.querySelector("button")?.dispatchEvent(new MouseEvent("click", { bubbles: true }));expect(contextMenu.isShown).toBe(false);});diff --git a/apps/client/src/widgets/react/Menu.tsx b/apps/client/src/widgets/react/Menu.tsxindex 7e2542c9c7..05969c6c8f 100644--- a/apps/client/src/widgets/react/Menu.tsx+++ b/apps/client/src/widgets/react/Menu.tsx@@ -358,7 +358,11 @@ function MenuListRow<T>({ level, row, state }: { level: number, row: MenuItem<T>if (element) state.rows.set(row, element);else state.rows.delete(row);}}- onClick={state.onClose}+ // Only a click on what the row acts with closes the menu: one in the space around it,+ // such as between the color picker's cells, picks nothing.+ onClick={(e) => {+ if (actsOnClick(e.target, e.currentTarget)) state.onClose();+ }}><row.componentFn /></li>@@ -520,6 +524,17 @@ function isCustom<T>(row: MenuItem<T>): row is CustomMenuItem {return "kind" in row && row.kind === "custom";}+/** What a custom row's content acts with: the elements a click on does something. */+const ACTING_ELEMENTS = "button, a[href], input, select, textarea, [tabindex], "+ + "[role='button'], [role='option'], [role='menuitem'], [role='checkbox'], [role='radio'], [role='switch']";++/** Whether a click on `target` inside `row` landed on an enabled element that acts. */+function actsOnClick(target: EventTarget | null, row: HTMLElement) {+ const acting = target instanceof Element ? target.closest(ACTING_ELEMENTS) : null;+ return !!acting && row.contains(acting) && acting !== row+ && acting.getAttribute("aria-disabled") !== "true" && !acting.matches(":disabled");+}+/** Where a custom row takes focus: the element its content marks as its way in with `tabindex="0"`. */function focusTarget(row: HTMLElement | undefined) {return row?.querySelector<HTMLElement>("[tabindex='0']") ?? null;