Skip to content

Build your personal knowledge base with Trilium Notes

fix(menu): keep a color picked in the context menu's custom cell

Elian Dorand40a28eff8d5
diff --git a/apps/client/src/menus/context_menu.spec.ts b/apps/client/src/menus/context_menu.spec.ts
index cd6b2e6c1c..52035cb029 100644
--- a/apps/client/src/menus/context_menu.spec.ts
+++ b/apps/client/src/menus/context_menu.spec.ts
@@ -376,6 +376,27 @@ describe("contextMenu", () => {
expect(label?.textContent).toBe("Templates");
});
+ it("stays up while the color picker's custom cell has the browser's picker open", async () => {
+ buildPage();
+ const contextMenu = await buildContextMenu();
+ const { default: ColorPicker } = await import("../widgets/react/ColorPicker");
+ // The browser's picker opens outside the page; happy-dom would bubble the click back.
+ const openPicker = vi.spyOn(HTMLInputElement.prototype, "click").mockImplementation(() => {});
+ await contextMenu.show({
+ x: 10, y: 10, selectMenuItemHandler: () => {},
+ items: [ { kind: "custom", componentFn: () => h(ColorPicker, { currentValue: null, onChange: () => {} }) } ]
+ });
+
+ const cell = menuElement()?.querySelector<HTMLElement>(".custom-color-cell");
+ if (!cell) throw new Error("expected the custom color cell");
+ cell.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ expect(openPicker).toHaveBeenCalled();
+ // Hiding would unmount the input, and the color picked in it would go nowhere.
+ expect(contextMenu.isShown).toBe(true);
+ expect(menuElement()?.querySelector("input[type=color]")).not.toBeNull();
+ openPicker.mockRestore();
+ });
+
it("runs an item pressed with the primary button, then hides", async () => {
buildPage();
const contextMenu = await buildContextMenu();
diff --git a/apps/client/src/widgets/react/ColorPicker.spec.tsx b/apps/client/src/widgets/react/ColorPicker.spec.tsx
index 012980567c..ec9db4ba02 100644
--- a/apps/client/src/widgets/react/ColorPicker.spec.tsx
+++ b/apps/client/src/widgets/react/ColorPicker.spec.tsx
@@ -2,8 +2,7 @@ import { ComponentChild } from "preact";
import { act } from "preact/test-utils";
import { beforeEach, describe, expect, it, vi } from "vitest";
-// `isMobile()` is read at render time inside CustomColorCell; mock the module so both the
-// desktop (no handler) and mobile (stopPropagation) branches can be exercised.
+// Mocked so a spec can check that a behavior holds on the desktop and on a phone alike.
const isMobileMock = vi.fn(() => false);
vi.mock("../../services/utils", async (importOriginal) => ({
...(await importOriginal<typeof import("../../services/utils")>()),
@@ -238,7 +237,7 @@ describe("ColorPicker", () => {
}
});
- it("stops click propagation on the custom cell only on mobile", () => {
+ it("stops click propagation on the custom cell on every platform", () => {
isMobileMock.mockReturnValue(true);
const mobileContainer = renderInto(<ColorPicker currentValue={null} onChange={vi.fn()} />);
const mobileWrapper = mobileContainer.querySelector(".custom-color-cell")?.parentElement;
@@ -253,7 +252,8 @@ describe("ColorPicker", () => {
const desktopEvent = new MouseEvent("click", { bubbles: true, cancelable: true });
const desktopStop = vi.spyOn(desktopEvent, "stopPropagation");
desktopWrapper?.dispatchEvent(desktopEvent);
- expect(desktopStop).not.toHaveBeenCalled();
+ // A menu the click reached would close, and take the input and its picked color with it.
+ expect(desktopStop).toHaveBeenCalled();
});
it("derives a contrasting foreground for light and dark custom colours", () => {
diff --git a/apps/client/src/widgets/react/ColorPicker.tsx b/apps/client/src/widgets/react/ColorPicker.tsx
index c13b3c3f0a..59ecec3104 100644
--- a/apps/client/src/widgets/react/ColorPicker.tsx
+++ b/apps/client/src/widgets/react/ColorPicker.tsx
@@ -6,7 +6,7 @@ import { ComponentChildren } from "preact";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { t } from "../../services/i18n";
-import { handleRightToLeftPlacement, isMobile } from "../../services/utils";
+import { handleRightToLeftPlacement } from "../../services/utils";
import Debouncer from "../../utils/debouncer";
/** Curated default preset palette, using Trilium note-color-friendly CSS colors. */
@@ -188,11 +188,11 @@ function CustomColorCell(props: ColorCellProps) {
}, [pickedColor]);
return <div style={`--foreground: ${getForegroundColor(props.color)};`}
- onClick={isMobile() ? (e) => {
- // The color picker dropdown will close on some browser if the parent context menu is
- // dismissed, so stop the click propagation to prevent dismissing the menu.
+ onClick={(e) => {
+ // A menu the click reached would close and unmount this cell, and with it the input
+ // whose picker is still open, so the color picked there would be lost.
e.stopPropagation();
- } : undefined}>
+ }}>
<ColorCell {...props}
color={pickedColor}
className={clsx("custom-color-cell", {