Skip to content

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

fix(sdk): fix crashes on React 19 due to findDOMNode on Gauge and Modal (#67953)

Phoomparin Mano4ec8c0cb0e6c
diff --git a/e2e/test-component/scenarios/embedding-sdk/react-19-compatibility.cy.spec.tsx b/e2e/test-component/scenarios/embedding-sdk/react-19-compatibility.cy.spec.tsx
new file mode 100644
index 00000000000..4588c0f9a04
--- /dev/null
+++ b/e2e/test-component/scenarios/embedding-sdk/react-19-compatibility.cy.spec.tsx
@@ -0,0 +1,180 @@
+const { H } = cy;
+import {
+ EditableDashboard,
+ StaticQuestion,
+} from "@metabase/embedding-sdk-react";
+
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import { getSdkRoot } from "e2e/support/helpers/e2e-embedding-sdk-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";
+import { createMockParameter } from "metabase-types/api/mocks";
+
+const { PRODUCTS, PRODUCTS_ID } = SAMPLE_DATABASE;
+
+// In .github/workflows/embedding-sdk-component-tests.yml, we run tests
+// across multiple React versions, including React 19.
+describe("scenarios > embedding-sdk > React 19 crash regression tests", () => {
+ describe("Filter edit modal should not crash (EMB-1171)", () => {
+ const categoryParameter = createMockParameter({
+ id: "category-param",
+ name: "Category",
+ type: "string/=",
+ slug: "category",
+ sectionId: "string",
+ });
+
+ const vendorParameter = createMockParameter({
+ id: "vendor-param",
+ name: "Vendor",
+ type: "string/=",
+ slug: "vendor",
+ sectionId: "string",
+ });
+
+ const priceParameter = createMockParameter({
+ id: "price-param",
+ name: "Price",
+ type: "number/between",
+ slug: "price",
+ sectionId: "number",
+ });
+
+ beforeEach(() => {
+ signInAsAdminAndEnableEmbeddingSdk();
+
+ H.createQuestion({
+ name: "Products Question",
+ query: {
+ "source-table": PRODUCTS_ID,
+ limit: 10,
+ },
+ }).then(({ body: card }) => {
+ cy.wrap(card.id).as("questionId");
+ });
+
+ cy.get<number>("@questionId").then((questionId) => {
+ H.createDashboard({
+ name: "Dashboard with Filters",
+ parameters: [categoryParameter, vendorParameter, priceParameter],
+ }).then(({ body: dashboard }) => {
+ cy.wrap(dashboard.id).as("dashboardId");
+
+ // Add the question to the dashboard with parameter mappings
+ H.updateDashboardCards({
+ dashboard_id: dashboard.id,
+ cards: [
+ {
+ id: -1,
+ card_id: questionId,
+ row: 0,
+ col: 0,
+ size_x: 16,
+ size_y: 8,
+ parameter_mappings: [
+ {
+ parameter_id: categoryParameter.id,
+ card_id: questionId,
+ target: ["dimension", ["field", PRODUCTS.CATEGORY, null]],
+ },
+ ],
+ },
+ ],
+ });
+ });
+ });
+
+ cy.signOut();
+ mockAuthProviderAndJwtSignIn();
+ });
+
+ it("should allow editing filter settings without crashing (EMB-1171)", () => {
+ cy.get<number>("@dashboardId").then((dashboardId) => {
+ mountSdkContent(<EditableDashboard dashboardId={dashboardId} />);
+ });
+
+ getSdkRoot().within(() => {
+ cy.button("Edit dashboard").click();
+
+ H.filterWidget({ isEditing: true }).contains("Category").click();
+
+ H.dashboardParameterSidebar()
+ .should("be.visible")
+ .findByText("Edit")
+ .click();
+
+ cy.findByText("Selectable values for Category").should("be.visible");
+ });
+ });
+ });
+
+ describe("Gauge visualization with long labels should not crash (EMB-1171)", () => {
+ const SHORT_LABEL = "i will appear";
+
+ const LONG_LABEL =
+ "I AM AN EXTREMELY LONG LABEL THAT SHOULD OVERFLOW THE SVG BOUNDARIES";
+
+ beforeEach(() => {
+ signInAsAdminAndEnableEmbeddingSdk();
+
+ // Create a gauge question with segments that have very long labels
+ H.createQuestion({
+ name: "Gauge with Long Labels",
+ query: {
+ "source-table": PRODUCTS_ID,
+ aggregation: [["count"]],
+ },
+ display: "gauge",
+ visualization_settings: {
+ "gauge.segments": [
+ {
+ min: 0,
+ max: 50,
+ color: "#ED6E6E",
+ label: SHORT_LABEL,
+ },
+ {
+ min: 50,
+ max: 100,
+ color: "#F9CF48",
+ label: LONG_LABEL,
+ },
+ {
+ min: 100,
+ max: 200,
+ color: "#84BB4C",
+ label: LONG_LABEL,
+ },
+ ],
+ },
+ }).then(({ body: card }) => {
+ cy.wrap(card.id).as("gaugeQuestionId");
+ });
+
+ cy.signOut();
+ mockAuthProviderAndJwtSignIn();
+ });
+
+ it("should clip gauge segments with long labels without crashing (EMB-1171)", () => {
+ cy.get<number>("@gaugeQuestionId").then((questionId) => {
+ mountSdkContent(<StaticQuestion questionId={questionId} />);
+ });
+
+ getSdkRoot().within(() => {
+ cy.get("svg").should("be.visible");
+
+ cy.log("gauge arcs should be rendered");
+ cy.findByTestId("gauge-arc-0").should("be.visible");
+ cy.findByTestId("gauge-arc-1").should("be.visible");
+ cy.findByTestId("gauge-arc-2").should("be.visible");
+
+ cy.log("short labels should be visible");
+ cy.findByText(SHORT_LABEL).should("be.visible");
+
+ cy.log("long labels should be clipped out");
+ cy.findByText(/EXTREMELY LONG LABEL/).should("not.exist");
+ });
+ });
+ });
+});
diff --git a/frontend/src/metabase/parameters/components/ValuesSourceSettings/ValuesSourceSettings.tsx b/frontend/src/metabase/parameters/components/ValuesSourceSettings/ValuesSourceSettings.tsx
index 26f817117dd..934a8daa0d1 100644
--- a/frontend/src/metabase/parameters/components/ValuesSourceSettings/ValuesSourceSettings.tsx
+++ b/frontend/src/metabase/parameters/components/ValuesSourceSettings/ValuesSourceSettings.tsx
@@ -1,8 +1,7 @@
import { useState } from "react";
import { t } from "ttag";
-import Modal from "metabase/common/components/Modal";
-import { Box, Button, Flex, Radio, Stack, Tooltip } from "metabase/ui";
+import { Box, Button, Flex, Modal, Radio, Stack, Tooltip } from "metabase/ui";
import { getQueryType } from "metabase-lib/v1/parameters/utils/parameter-source";
import type {
Parameter,
@@ -66,15 +65,19 @@ export function ValuesSourceSettings({
/>
</Stack>
</Radio.Group>
- {isModalOpened && (
- <Modal medium onClose={closeModal}>
- <ValuesSourceModal
- parameter={parameter}
- onSubmit={onChangeSourceSettings}
- onClose={closeModal}
- />
- </Modal>
- )}
+ <Modal
+ opened={isModalOpened}
+ onClose={closeModal}
+ size="65%"
+ padding={0}
+ withCloseButton={false}
+ >
+ <ValuesSourceModal
+ parameter={parameter}
+ onSubmit={onChangeSourceSettings}
+ onClose={closeModal}
+ />
+ </Modal>
</>
);
}
diff --git a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
index 6af094bda5e..99665065f3f 100644
--- a/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
+++ b/frontend/src/metabase/visualizations/visualizations/Gauge/Gauge.jsx
@@ -1,9 +1,8 @@
/* eslint-disable react/prop-types */
import cx from "classnames";
import * as d3 from "d3";
-import { Component } from "react";
+import { Component, useCallback, useEffect, useRef, useState } from "react";
import * as React from "react";
-import ReactDOM from "react-dom";
import { t } from "ttag";
import _ from "underscore";
@@ -312,7 +311,7 @@ export default class Gauge extends Component {
{/* TEXT LABELS */}
{showLabels &&
textLabels.map(({ label, value }, index) => (
- <HideIfOverlowingSVG key={index}>
+ <HideIfOverflowingSVG key={index}>
<GaugeSegmentLabel
position={valuePosition(
value,
@@ -324,7 +323,7 @@ export default class Gauge extends Component {
>
{label}
</GaugeSegmentLabel>
- </HideIfOverlowingSVG>
+ </HideIfOverflowingSVG>
))}
{/* CENTER LABEL */}
{/* NOTE: can't be a component because ref doesn't work? */}
@@ -450,35 +449,43 @@ const GaugeSegmentLabel = ({ position: [x, y], style = {}, children }) => (
</text>
);
-class HideIfOverlowingSVG extends React.Component {
- componentDidMount() {
- this._hideIfClipped();
- }
- componentDidUpdate() {
- this._hideIfClipped();
- }
- _hideIfClipped() {
- const element = ReactDOM.findDOMNode(this);
+const HideIfOverflowingSVG = ({ children }) => {
+ const elementRef = useRef(null);
+ const [isHidden, setIsHidden] = useState(false);
+
+ const hideIfClipped = useCallback(() => {
+ const element = elementRef.current;
+
if (element) {
let svg = element;
- while (svg.nodeName.toLowerCase() !== "svg") {
+
+ while (svg && svg.nodeName.toLowerCase() !== "svg") {
svg = svg.parentNode;
}
- const svgRect = svg.getBoundingClientRect();
- const elementRect = element.getBoundingClientRect();
- if (
- elementRect.left >= svgRect.left &&
- elementRect.right <= svgRect.right &&
- elementRect.top >= svgRect.top &&
- elementRect.bottom <= svgRect.bottom
- ) {
- element.classList.remove(CS.hidden);
- } else {
- element.classList.add(CS.hidden);
+
+ if (svg) {
+ const svgRect = svg.getBoundingClientRect();
+ const elementRect = element.getBoundingClientRect();
+
+ const shouldBeHidden = !(
+ elementRect.left >= svgRect.left &&
+ elementRect.right <= svgRect.right &&
+ elementRect.top >= svgRect.top &&
+ elementRect.bottom <= svgRect.bottom
+ );
+
+ setIsHidden(shouldBeHidden);
}
}
- }
- render() {
- return this.props.children;
- }
-}
+ }, []);
+
+ useEffect(() => {
+ hideIfClipped();
+ });
+
+ return (
+ <g ref={elementRef} className={isHidden ? CS.hidden : undefined}>
+ {children}
+ </g>
+ );
+};