Skip to content

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

Drop react-copy-to-clipboard (#65674)

Fraser6d349fd1b2fb
diff --git a/e2e/embedding-sdk-host-apps/angular-20-host-app/package-lock.json b/e2e/embedding-sdk-host-apps/angular-20-host-app/package-lock.json
index 1a6b9a4af2a..684f98c26ff 100644
--- a/e2e/embedding-sdk-host-apps/angular-20-host-app/package-lock.json
+++ b/e2e/embedding-sdk-host-apps/angular-20-host-app/package-lock.json
@@ -107,7 +107,6 @@
"prop-types": "^15.5.7",
"re-reselect": "^4.0.1",
"react-color": "^2.14.1",
- "react-copy-to-clipboard": "^5.0.1",
"react-dnd": "4",
"react-dnd-html5-backend": "4",
"react-draggable": "^4.4.5",
diff --git a/e2e/test/scenarios/admin/troubleshooting.cy.spec.js b/e2e/test/scenarios/admin/troubleshooting.cy.spec.js
index 1297cf68a77..4643fe673e2 100644
--- a/e2e/test/scenarios/admin/troubleshooting.cy.spec.js
+++ b/e2e/test/scenarios/admin/troubleshooting.cy.spec.js
@@ -287,14 +287,12 @@ describe("scenarios > admin > tools > tasks", () => {
cy.log("copy button");
cy.window().then((window) => {
- window.clipboardData = {
- setData: cy.stub(),
- };
+ cy.stub(window.navigator.clipboard, "writeText").resolves();
});
cy.icon("copy").click();
cy.window()
- .its("clipboardData.setData")
- .should("be.calledWith", "text", formattedTaskJson);
+ .its("navigator.clipboard.writeText")
+ .should("be.calledWith", formattedTaskJson);
cy.findByRole("tooltip").should("have.text", "Copied!");
cy.log("download button");
diff --git a/enterprise/frontend/src/metabase-enterprise/user_provisioning/components/UserProvisioning.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/user_provisioning/components/UserProvisioning.unit.spec.tsx
index a565114564e..2d08402ff73 100644
--- a/enterprise/frontend/src/metabase-enterprise/user_provisioning/components/UserProvisioning.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/user_provisioning/components/UserProvisioning.unit.spec.tsx
@@ -125,15 +125,19 @@ describe("SCIM User Provisioning Settings", () => {
it("should allow copying the scim URL", async () => {
window.prompt = jest.fn(); // no idea what this does, but jsdom says it's not implemented
+ Object.assign(window.navigator, {
+ clipboard: {
+ writeText: jest.fn().mockImplementation(() => Promise.resolve()),
+ },
+ });
await setup({
settings: {
"scim-enabled": true,
},
});
- const copyButton = await screen.findByTestId("copy-button");
+ await userEvent.click(screen.getByLabelText("copy icon"));
- await userEvent.click(copyButton);
expect(await screen.findByText("Copied!")).toBeInTheDocument();
});
diff --git a/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx b/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx
index 0149a67f4b3..4ec14b97c06 100644
--- a/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx
+++ b/frontend/src/metabase/common/components/CopyButton/CopyButton.tsx
@@ -1,7 +1,6 @@
+import { useClipboard } from "@mantine/hooks";
import cx from "classnames";
-import { useCallback, useRef, useState } from "react";
-import CopyToClipboard from "react-copy-to-clipboard";
-import { useUnmount } from "react-use";
+import { useCallback } from "react";
import { t } from "ttag";
import { isPlainKey } from "metabase/common/utils/keyboard";
@@ -15,7 +14,7 @@ export const COPY_BUTTON_ICON = (
);
type CopyButtonProps = {
- value: CopyToClipboard.Props["text"];
+ value: string;
onCopy?: () => void;
className?: string;
style?: object;
@@ -30,20 +29,12 @@ export const CopyButton = ({
style,
target = COPY_BUTTON_ICON,
}: CopyButtonProps) => {
- const [copied, setCopied] = useState(false);
- const timeoutIdRef = useRef<number>();
-
- useUnmount(() => {
- window.clearTimeout(timeoutIdRef.current);
- });
+ const clipboard = useClipboard({ timeout: 2000 });
const onCopyValue = useCallback(() => {
- setCopied(true);
-
- window.clearTimeout(timeoutIdRef.current);
- timeoutIdRef.current = window.setTimeout(() => setCopied(false), 2000);
+ clipboard.copy(value);
onCopy?.();
- }, [onCopy]);
+ }, [clipboard, value, onCopy]);
const copyOnEnter = useCallback(
(e: React.KeyboardEvent<HTMLElement>) => {
@@ -55,15 +46,15 @@ export const CopyButton = ({
);
return (
- <CopyToClipboard text={value} onCopy={onCopyValue}>
- <div className={className} style={style} data-testid="copy-button">
- <Tooltip
- label={<Text fw={700} c="inherit">{t`Copied!`}</Text>}
- opened={copied}
- >
- <span onKeyDown={copyOnEnter}>{target}</span>
- </Tooltip>
- </div>
- </CopyToClipboard>
+ <div className={className} style={style} data-testid="copy-button">
+ <Tooltip
+ label={<Text fw={700} c="inherit">{t`Copied!`}</Text>}
+ opened={clipboard.copied}
+ >
+ <span onClick={onCopyValue} onKeyDown={copyOnEnter}>
+ {target}
+ </span>
+ </Tooltip>
+ </div>
);
};
diff --git a/frontend/src/metabase/embedding/components/PublicLinkPopover/PublicLinkPopover.unit.spec.tsx b/frontend/src/metabase/embedding/components/PublicLinkPopover/PublicLinkPopover.unit.spec.tsx
index 814cf88e34c..3bb9e8bc8bf 100644
--- a/frontend/src/metabase/embedding/components/PublicLinkPopover/PublicLinkPopover.unit.spec.tsx
+++ b/frontend/src/metabase/embedding/components/PublicLinkPopover/PublicLinkPopover.unit.spec.tsx
@@ -9,9 +9,6 @@ import { createMockState } from "metabase-types/store/mocks";
import type { PublicLinkPopoverProps } from "./PublicLinkPopover";
import { PublicLinkPopover } from "./PublicLinkPopover";
-// https://github.com/nkbt/react-copy-to-clipboard/issues/106#issuecomment-605227151
-jest.mock("copy-to-clipboard", () => jest.fn());
-
const TestComponent = ({
createPublicLink,
deletePublicLink,
diff --git a/frontend/test/jest-setup-env.js b/frontend/test/jest-setup-env.js
index 6cd65f0be0b..847347361ae 100644
--- a/frontend/test/jest-setup-env.js
+++ b/frontend/test/jest-setup-env.js
@@ -2,6 +2,13 @@ import "@testing-library/jest-dom";
import { cleanup } from "@testing-library/react";
import fetchMock from "fetch-mock";
+// Mock clipboard API for tests
+Object.assign(navigator, {
+ clipboard: {
+ writeText: jest.fn(() => Promise.resolve()),
+ },
+});
+
beforeEach(() => {
fetchMock.mockGlobal();
});
diff --git a/package.json b/package.json
index 78ed5f01533..604503df687 100644
--- a/package.json
+++ b/package.json
@@ -110,7 +110,6 @@
"react": "^18.2.0",
"react-ansi-style": "^1.0.0",
"react-color": "^2.14.1",
- "react-copy-to-clipboard": "^5.0.1",
"react-dnd": "4",
"react-dnd-html5-backend": "4",
"react-dom": "^18.2.0",
@@ -231,7 +230,6 @@
"@types/raf": "^3.4.0",
"@types/react": "^18.3.3",
"@types/react-color": "^2.17.6",
- "@types/react-copy-to-clipboard": "^5.0.2",
"@types/react-dom": "^18.2.17",
"@types/react-grid-layout": "^1.3.5",
"@types/react-is": "^18.2.4",
diff --git a/yarn.lock b/yarn.lock
index 345c9e58c0b..3a19fe42d15 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -5609,13 +5609,6 @@
"@types/react" "*"
"@types/reactcss" "*"
-"@types/react-copy-to-clipboard@^5.0.2":
- version "5.0.2"
- resolved "https://registry.yarnpkg.com/@types/react-copy-to-clipboard/-/react-copy-to-clipboard-5.0.2.tgz#c29690b472a54edff35916f0d1c6c797ad0fd34b"
- integrity sha512-O29AThfxrkUFRsZXjfSWR2yaWo0ppB1yLEnHA+Oh24oNetjBAwTDu1PmolIqdJKzsZiO4J1jn6R6TmO96uBvGg==
- dependencies:
- "@types/react" "*"
-
"@types/react-dom@^18.2.17":
version "18.2.25"
resolved "https://registry.yarnpkg.com/@types/react-dom/-/react-dom-18.2.25.tgz#2946a30081f53e7c8d585eb138277245caedc521"
@@ -8607,7 +8600,7 @@ copy-anything@^2.0.1:
dependencies:
is-what "^3.14.1"
-copy-to-clipboard@^3, copy-to-clipboard@^3.3.1:
+copy-to-clipboard@^3.3.1:
version "3.3.3"
resolved "https://registry.yarnpkg.com/copy-to-clipboard/-/copy-to-clipboard-3.3.3.tgz#55ac43a1db8ae639a4bd99511c148cdd1b83a1b0"
integrity sha512-2KV8NhB5JqC3ky0r9PMCAZKbUHSwtEo4CwCs0KXgruG43gX5PMqDEBbVU4OUzw2MuAWUfsuFmWvEKG5QRfSnJA==
@@ -17500,7 +17493,7 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.5"
[email protected], prop-types@^15.0.0, prop-types@^15.5.0, prop-types@^15.5.10, prop-types@^15.5.7, prop-types@^15.5.8, prop-types@^15.6.0, prop-types@^15.6.2, prop-types@^15.7.2, prop-types@^15.8.1:
[email protected], prop-types@^15.0.0, prop-types@^15.5.0, prop-types@^15.5.10, prop-types@^15.5.7, prop-types@^15.6.0, prop-types@^15.6.2, prop-types@^15.7.2, prop-types@^15.8.1:
version "15.8.1"
resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.8.1.tgz#67d87bf1a694f48435cf332c24af10214a3140b5"
integrity sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==
@@ -17942,14 +17935,6 @@ react-color@^2.14.1:
reactcss "^1.2.0"
tinycolor2 "^1.4.1"
-react-copy-to-clipboard@^5.0.1:
- version "5.0.2"
- resolved "https://registry.yarnpkg.com/react-copy-to-clipboard/-/react-copy-to-clipboard-5.0.2.tgz#d82a437e081e68dfca3761fbd57dbf2abdda1316"
- integrity sha512-/2t5mLMMPuN5GmdXo6TebFa8IoFxZ+KTDDqYhcDm0PhkgEzSxVvIX26G20s1EB02A4h2UZgwtfymZ3lGJm0OLg==
- dependencies:
- copy-to-clipboard "^3"
- prop-types "^15.5.8"
-
react-devtools-core@^4.19.1:
version "4.28.5"
resolved "https://registry.yarnpkg.com/react-devtools-core/-/react-devtools-core-4.28.5.tgz#c8442b91f068cdf0c899c543907f7f27d79c2508"