Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

feat(sdk,embed-js): Enhance getEventTarget to support TouchEvent for improved mobile compatibility when showing a drill popup (#71636)

Timofey Kachalov2aae5c688bee
diff --git a/e2e/support/helpers/e2e-mobile-device-helpers.ts b/e2e/support/helpers/e2e-mobile-device-helpers.ts
index 16b022e0305..2eceb8e42b3 100644
--- a/e2e/support/helpers/e2e-mobile-device-helpers.ts
+++ b/e2e/support/helpers/e2e-mobile-device-helpers.ts
@@ -1,3 +1,5 @@
+const LONG_PRESS_MS = 600;
+
export function enableTouchEmulation() {
cy.then(() =>
Cypress.automation("remote:debugger:protocol", {
@@ -6,6 +8,14 @@ export function enableTouchEmulation() {
}),
);
+ // Make CSS @media (hover: none) match on emulated touch devices
+ cy.then(() =>
+ Cypress.automation("remote:debugger:protocol", {
+ command: "Emulation.setEmulatedMedia",
+ params: { features: [{ name: "hover", value: "none" }] },
+ }),
+ );
+
cy.window().its("navigator.maxTouchPoints").should("be.greaterThan", 0);
}
@@ -16,4 +26,55 @@ export function disableTouchEmulation() {
params: { enabled: false },
}),
);
+
+ cy.then(() =>
+ Cypress.automation("remote:debugger:protocol", {
+ command: "Emulation.setEmulatedMedia",
+ params: { features: [] },
+ }),
+ );
+}
+
+export function longPressAndDrag(
+ selector: string,
+ startX: number,
+ startY: number,
+ endX: number,
+ holdMs = LONG_PRESS_MS,
+) {
+ cy.findByTestId(selector).trigger("pointerdown", startX, startY, {
+ force: true,
+ isPrimary: true,
+ button: 0,
+ });
+
+ cy.wait(holdMs);
+
+ cy.findByTestId(selector)
+ .trigger("mousemove", endX, startY)
+ .trigger("mouseup", endX, startY);
+}
+
+export function quickSwipe(
+ selector: string,
+ startX: number,
+ startY: number,
+ endX: number,
+) {
+ cy.findByTestId(selector)
+ .trigger("pointerdown", startX, startY, {
+ force: true,
+ isPrimary: true,
+ button: 0,
+ })
+ .trigger("pointermove", endX, startY, {
+ force: true,
+ isPrimary: true,
+ button: 0,
+ })
+ .trigger("pointerup", endX, startY, {
+ force: true,
+ isPrimary: true,
+ button: 0,
+ });
}
diff --git a/e2e/test-component/scenarios/embedding-sdk/touch-chart-drill.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/touch-chart-drill.cy.spec.tsx
new file mode 100644
index 00000000000..060550035ac
--- /dev/null
+++ b/e2e/test-component/scenarios/embedding-sdk/touch-chart-drill.cy.spec.tsx
@@ -0,0 +1,154 @@
+import { InteractiveDashboard } from "@metabase/embedding-sdk-react";
+
+const { H } = cy;
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import { popover } from "e2e/support/helpers";
+import { getSdkRoot } from "e2e/support/helpers/e2e-embedding-sdk-helpers";
+import {
+ disableTouchEmulation,
+ enableTouchEmulation,
+} from "e2e/support/helpers/e2e-mobile-device-helpers";
+import { mountSdkContent } from "e2e/support/helpers/embedding-sdk-component-testing/component-embedding-sdk-helpers";
+import { signInAsAdminAndEnableEmbeddingSdk } from "e2e/support/helpers/embedding-sdk-testing";
+import { mockAuthProviderAndJwtSignIn } from "e2e/support/helpers/embedding-sdk-testing/embedding-sdk-helpers";
+
+const { ORDERS, ORDERS_ID } = SAMPLE_DATABASE;
+
+const CIRCLE_PATH = "M1 0A1 1 0 1 1 1 -0.0001";
+
+function touchTapOnDataPoint(
+ $container: JQuery<HTMLElement>,
+ win: Cypress.AUTWindow,
+) {
+ const circle = $container[0].querySelector(`path[d="${CIRCLE_PATH}"]`);
+ expect(circle, "chart should have a data point circle").to.not.be.null;
+
+ const circleRect = circle!.getBoundingClientRect();
+ const clientX = circleRect.left + circleRect.width / 2;
+ const clientY = circleRect.top + circleRect.height / 2;
+
+ const touch = new win.Touch({
+ identifier: 0,
+ target: circle!,
+ clientX,
+ clientY,
+ });
+
+ // touchstart — zrender reads targetTouches[0] for this event type
+ circle!.dispatchEvent(
+ new win.TouchEvent("touchstart", {
+ bubbles: true,
+ cancelable: true,
+ touches: [touch],
+ targetTouches: [touch],
+ changedTouches: [touch],
+ }),
+ );
+
+ circle!.dispatchEvent(
+ new win.TouchEvent("touchend", {
+ bubbles: true,
+ cancelable: true,
+ touches: [],
+ targetTouches: [],
+ changedTouches: [touch],
+ }),
+ );
+
+ return { clientX, clientY };
+}
+
+describe("scenarios > embedding-sdk > touch chart drill popover", () => {
+ beforeEach(() => {
+ signInAsAdminAndEnableEmbeddingSdk();
+
+ H.createDashboardWithQuestions({
+ dashboardName: "Touch drill test dashboard",
+ questions: [
+ {
+ name: "Line chart for touch drill",
+ display: "line",
+ query: {
+ "source-table": ORDERS_ID,
+ aggregation: [["count"]],
+ breakout: [
+ ["field", ORDERS.CREATED_AT, { "temporal-unit": "month" }],
+ ],
+ limit: 5,
+ },
+ },
+ ],
+ }).then(({ dashboard }) => {
+ cy.wrap(dashboard.id).as("dashboardId");
+ });
+
+ cy.signOut();
+ mockAuthProviderAndJwtSignIn();
+
+ cy.intercept("GET", "/api/dashboard/*").as("getDashboard");
+ cy.intercept("POST", "/api/dashboard/*/dashcard/*/card/*/query").as(
+ "dashcardQuery",
+ );
+ });
+
+ describe("touch device", () => {
+ beforeEach(() => {
+ cy.viewport("iphone-x");
+ enableTouchEmulation();
+
+ cy.window().then((win) => {
+ if (!("ontouchstart" in win)) {
+ (win as any).ontouchstart = null;
+ }
+ });
+ });
+
+ afterEach(() => {
+ disableTouchEmulation();
+ });
+
+ it("should show drill popover near the tapped data point, not offscreen", () => {
+ cy.get<string>("@dashboardId").then((dashboardId) => {
+ mountSdkContent(<InteractiveDashboard dashboardId={dashboardId} />);
+ });
+
+ cy.wait("@dashcardQuery");
+
+ let tapCoords: { clientX: number; clientY: number };
+
+ getSdkRoot().within(() => {
+ H.getDashboardCard(0).within(() => {
+ cy.findByTestId("chart-container").then(($container) => {
+ // Wait for circles to render
+ cy.wrap($container)
+ .find(`path[d="${CIRCLE_PATH}"]`)
+ .should("exist");
+
+ cy.window().then((win) => {
+ tapCoords = touchTapOnDataPoint($container, win);
+ });
+ });
+ });
+
+ popover()
+ .should("be.visible")
+ .then(($popover) => {
+ const popoverRect = $popover[0].getBoundingClientRect();
+
+ const MAX_DISTANCE = 300;
+ const popoverCenterX = popoverRect.left + popoverRect.width / 2;
+ const popoverCenterY = popoverRect.top + popoverRect.height / 2;
+
+ const distance = Math.sqrt(
+ (popoverCenterX - tapCoords.clientX) ** 2 +
+ (popoverCenterY - tapCoords.clientY) ** 2,
+ );
+ expect(
+ distance,
+ "popover should be near the tapped data point",
+ ).to.be.lessThan(MAX_DISTANCE);
+ });
+ });
+ });
+ });
+});
diff --git a/frontend/src/metabase/lib/dom.ts b/frontend/src/metabase/lib/dom.ts
index e6aa463326b..f62a5336383 100644
--- a/frontend/src/metabase/lib/dom.ts
+++ b/frontend/src/metabase/lib/dom.ts
@@ -420,7 +420,7 @@ export function isSmallScreen(): boolean {
}
export const getEventTarget = (
- event: MouseEvent | React.MouseEvent,
+ event: MouseEvent | React.MouseEvent | TouchEvent,
): HTMLElement => {
let target = document.getElementById("popover-event-target");
if (!target) {
@@ -428,8 +428,21 @@ export const getEventTarget = (
target.id = "popover-event-target";
document.body.appendChild(target);
}
- target.style.left = event.clientX - 3 + "px";
- target.style.top = event.clientY - 3 + "px";
+
+ // TouchEvent (e.g. from iOS Safari via ECharts/zrender) doesn't have
+ // clientX/clientY directly — they live on individual Touch objects.
+ let clientX: number;
+ let clientY: number;
+ if ("changedTouches" in event && event.changedTouches?.length) {
+ clientX = event.changedTouches[0].clientX;
+ clientY = event.changedTouches[0].clientY;
+ } else {
+ clientX = (event as MouseEvent).clientX;
+ clientY = (event as MouseEvent).clientY;
+ }
+
+ target.style.left = clientX - 3 + "px";
+ target.style.top = clientY - 3 + "px";
return target;
};