Skip to content

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

Catch drag events on Dashcard placeholders (#71274)

Nick Fitzpatrickcfa96f3fe052
diff --git a/e2e/test/scenarios/dashboard-cards/dashboard-sections.cy.spec.js b/e2e/test/scenarios/dashboard-cards/dashboard-sections.cy.spec.js
index d8676f27430..ee85589d58a 100644
--- a/e2e/test/scenarios/dashboard-cards/dashboard-sections.cy.spec.js
+++ b/e2e/test/scenarios/dashboard-cards/dashboard-sections.cy.spec.js
@@ -55,15 +55,18 @@ describe("scenarios > dashboard cards > sections", () => {
section_layout: "kpi_grid",
});
+ // Verify placeholder cards can be dragged (metabase#UXW-3387)
+ assertPlaceholderCardCanBeDragged();
+
selectQuestion("Orders, Count, Grouped by Created At (year)");
overwriteDashCardTitle(
- 2,
+ 1,
"Orders, Count, Grouped by Created At (year)",
"Line chart",
);
// TODO: if the mapping is done before the title is changed, the mapping is lost
- mapDashCardToFilter(H.getDashboardCard(2), "Category");
+ mapDashCardToFilter(H.getDashboardCard(1), "Category");
H.goToTab("Tab 1");
H.saveDashboard();
@@ -101,7 +104,7 @@ describe("scenarios > dashboard cards > sections", () => {
// Ensure parameter mapping is persisted
H.editDashboard();
filterPanel().findByText("Category").click();
- H.getDashboardCard(2).findByText("Product.Category").should("exist");
+ H.getDashboardCard(1).findByText("Product.Category").should("exist");
});
});
@@ -163,3 +166,42 @@ function mapDashCardToFilter(dashcardElement, filterName) {
H.selectDashboardFilter(dashcardElement, filterName);
H.sidebar().button("Done").click();
}
+
+function assertPlaceholderCardCanBeDragged() {
+ H.getDashboardCards().then(($cards) => {
+ const initialLeftValues = [...$cards].map(
+ (card) => card.getBoundingClientRect().left,
+ );
+
+ const $target = $cards
+ .filter((_i, card) =>
+ card
+ .querySelector("[data-testid='dashcard']")
+ ?.textContent?.includes("Select question"),
+ )
+ .first();
+
+ const rect = $target[0].getBoundingClientRect();
+
+ cy.wrap($target)
+ .trigger("mousedown", { button: 0, x: 10, y: 50 })
+ .trigger("mousemove", {
+ button: 0,
+ clientX: rect.left + 900,
+ clientY: rect.top + 200,
+ })
+ .wait(100)
+ .trigger("mouseup");
+
+ H.getDashboardCards().then(($afterCards) => {
+ const afterLeftValues = [...$afterCards].map(
+ (card) => card.getBoundingClientRect().left,
+ );
+
+ const someCardMoved = afterLeftValues.some(
+ (left, i) => left !== initialLeftValues[i],
+ );
+ expect(someCardMoved).to.eq(true);
+ });
+ });
+}
diff --git a/frontend/src/metabase/visualizations/visualizations/DashCardPlaceholder.tsx b/frontend/src/metabase/visualizations/visualizations/DashCardPlaceholder.tsx
index b21c5de9d2a..9102756878b 100644
--- a/frontend/src/metabase/visualizations/visualizations/DashCardPlaceholder.tsx
+++ b/frontend/src/metabase/visualizations/visualizations/DashCardPlaceholder.tsx
@@ -75,31 +75,38 @@ function DashCardPlaceholderInner({
>
<Button
onClick={() => setQuestionPickerOpen(true)}
- onMouseDown={preventDragging}
style={{ pointerEvents }}
+ onMouseDown={preventDragging}
+ onPointerDown={preventDragging}
>{t`Select question`}</Button>
</Flex>
)}
</Flex>
{isQuestionPickerOpen && (
- <QuestionPickerModal
- title={t`Pick what you want to replace this with`}
- value={
- dashboard.collection_id
- ? {
- id: dashboard.collection_id,
- model: "collection",
- }
- : undefined
- }
- options={{ hasConfirmButtons: false }}
- // TODO: account for restrictions on adding personal
- // questions to public dashboards
- models={["card", "dataset", "metric", "dashboard"]}
- onChange={handleSelectQuestion}
- onClose={() => setQuestionPickerOpen(false)}
- isDisabledItem={shouldDisableItem}
- />
+ <div
+ style={{ display: "contents" }}
+ onMouseDown={preventDragging}
+ onPointerDown={preventDragging}
+ >
+ <QuestionPickerModal
+ title={t`Pick what you want to replace this with`}
+ value={
+ dashboard.collection_id
+ ? {
+ id: dashboard.collection_id,
+ model: "collection",
+ }
+ : undefined
+ }
+ options={{ hasConfirmButtons: false }}
+ // TODO: account for restrictions on adding personal
+ // questions to public dashboards
+ models={["card", "dataset", "metric", "dashboard"]}
+ onChange={handleSelectQuestion}
+ onClose={() => setQuestionPickerOpen(false)}
+ isDisabledItem={shouldDisableItem}
+ />
+ </div>
)}
</>
);
@@ -107,7 +114,17 @@ function DashCardPlaceholderInner({
DashCardPlaceholderInner.displayName = "DashCardPlaceholder";
-function preventDragging(e: React.MouseEvent<HTMLButtonElement>) {
+/**
+ * Prevents React portal event bubbling from triggering grid item drags.
+ *
+ * React portals (used by modals) bubble synthetic events through the React
+ * component tree, not the DOM tree. This means clicks inside a modal rendered
+ * by this component would bubble up to DraggableCore on the grid item and
+ * initiate a drag. We stop both mousedown and pointerdown because Cypress
+ * (and browsers) dispatch both events, and React processes them as separate
+ * synthetic event dispatches — stopPropagation on one doesn't affect the other.
+ */
+function preventDragging(e: React.SyntheticEvent) {
e.stopPropagation();
}