Skip to content

Build your personal knowledge base with Trilium Notes

fix(popup): keep a dismissing tap's click from what the cover hid

Elian Doran400353c0fa7a
diff --git a/apps/client/src/widgets/react/Popup.spec.tsx b/apps/client/src/widgets/react/Popup.spec.tsx
index 76c71e4b5d..3935ae06d6 100644
--- a/apps/client/src/widgets/react/Popup.spec.tsx
+++ b/apps/client/src/widgets/react/Popup.spec.tsx
@@ -138,6 +138,37 @@ describe("Popup", () => {
outside.remove();
});
+ it("keeps the click of a dismissing press from what the press uncovered, but not from what it pressed", async () => {
+ anchorAt(100, 50);
+ const popup = await open({ onDismiss: vi.fn() });
+ const press = (target: Element) => target.dispatchEvent(new PointerEvent("pointerdown", { bubbles: true }));
+ const click = (target: Element) => target.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true }));
+ // A phone's cover, which the popup takes away before the tap's click, and an icon under it.
+ const cover = document.createElement("div");
+ const icon = document.createElement("button");
+ const iconClicked = vi.fn();
+ icon.addEventListener("click", iconClicked);
+ document.body.append(cover, icon);
+
+ press(cover);
+ click(icon);
+ expect(iconClicked).not.toHaveBeenCalled();
+ // Only the one click.
+ click(icon);
+ expect(iconClicked).toHaveBeenCalledTimes(1);
+
+ press(icon);
+ click(icon);
+ expect(iconClicked).toHaveBeenCalledTimes(2);
+
+ // A press inside dismisses nothing, so its click goes where it lands.
+ press(popup);
+ click(icon);
+ expect(iconClicked).toHaveBeenCalledTimes(3);
+ cover.remove();
+ icon.remove();
+ });
+
it("counts a popup opened inside it as inside, and leaves Escape to that popup", async () => {
anchorAt(100, 50);
const onOuterDismiss = vi.fn();
diff --git a/apps/client/src/widgets/react/Popup.tsx b/apps/client/src/widgets/react/Popup.tsx
index 75573c972f..938b0676c2 100644
--- a/apps/client/src/widgets/react/Popup.tsx
+++ b/apps/client/src/widgets/react/Popup.tsx
@@ -133,6 +133,7 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gap
if (layer.contains(target)) return;
if (anchor instanceof HTMLElement && anchor.contains(target)) return;
dismiss.current?.("outside");
+ swallowStrayClick(target);
};
const onKeyDown = (e: KeyboardEvent) => {
// A popup open inside this one takes Escape first.
@@ -170,6 +171,27 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gap
container ?? document.fullscreenElement ?? document.body);
}
+/**
+ * Drops the click that follows a press dismissing a popup when it lands on something other than
+ * `pressed`. A cover or a backdrop goes away with the popup before a tap's click, which then lands
+ * on whatever stood under it, as an icon under the icon picker's menu.
+ */
+function swallowStrayClick(pressed: Node) {
+ const onClick = (e: MouseEvent) => {
+ stop();
+ if (e.target instanceof Node && pressed.contains(e.target)) return;
+ e.preventDefault();
+ e.stopPropagation();
+ };
+ // A press that makes no click, as one that scrolls, leaves nothing for the next press's.
+ const stop = () => {
+ window.removeEventListener("click", onClick, true);
+ window.removeEventListener("pointerdown", stop, true);
+ };
+ window.addEventListener("click", onClick, true);
+ window.addEventListener("pointerdown", stop, true);
+}
+
/** A popup, as the one it opened inside knows it. */
interface Layer {
/** Whether `target` is in the popup or in a popup opened inside it. */