Skip to content

Build your personal knowledge base with Trilium Notes

fix(popup): send a tap Safari takes as a double tap where it pressed

Elian Doran893c7dac4636
diff --git a/apps/client/src/widgets/react/Popup.spec.tsx b/apps/client/src/widgets/react/Popup.spec.tsx
index e9c6ea135e..79805171ce 100644
--- a/apps/client/src/widgets/react/Popup.spec.tsx
+++ b/apps/client/src/widgets/react/Popup.spec.tsx
@@ -318,6 +318,18 @@ describe("Popup", () => {
press(glyph);
click(icon);
expect(iconClicked).toHaveBeenCalledTimes(4);
+
+ // WebKit takes a tap close to the one before as a double tap, and clicks where that one
+ // landed instead of where this one did.
+ const other = document.createElement("button");
+ const otherClicked = vi.fn();
+ other.addEventListener("click", otherClicked);
+ document.body.append(other);
+ press(glyph);
+ other.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 2 }));
+ expect(otherClicked).not.toHaveBeenCalled();
+ expect(iconClicked).toHaveBeenCalledTimes(5);
+ other.remove();
cover.remove();
icon.remove();
});
diff --git a/apps/client/src/widgets/react/Popup.tsx b/apps/client/src/widgets/react/Popup.tsx
index 5a521bb8ca..2556c4062f 100644
--- a/apps/client/src/widgets/react/Popup.tsx
+++ b/apps/client/src/widgets/react/Popup.tsx
@@ -214,6 +214,9 @@ export default function Popup({ anchor, placement = "bottom-start", offset: gap
* `pressed` or an element containing it. A cover or backdrop is removed with the popup before a
* tap's click, so the click lands on the element under it, such as an icon under the icon picker's
* menu. A tap that ends outside `pressed` clicks the element that contains both ends of the tap.
+ *
+ * WebKit on iOS takes a tap close to the one before it as a double tap, and replaces its click
+ * with one where the first tap landed, its `detail` the tap count. That click goes to `pressed`.
*/
function swallowStrayClick(pressed: Node) {
const onClick = (e: MouseEvent) => {
@@ -223,6 +226,9 @@ function swallowStrayClick(pressed: Node) {
}
e.preventDefault();
e.stopPropagation();
+ if (e.detail > 1 && pressed.isConnected) {
+ pressed.dispatchEvent(new MouseEvent("click", { bubbles: true, cancelable: true, detail: 1 }));
+ }
};
// The next press removes the listener, so a press without a click, such as a scroll, cannot
// drop a later click.