Skip to content

Commit bb1c44d

Browse files
authored
fix(copy-button): fall back when Clipboard API unavailable (onyx-dot-app#10080)
1 parent f26ecaf commit bb1c44d

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

web/package-lock.json

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"clsx": "^2.1.1",
6666
"cmdk": "^1.0.0",
6767
"cookies-next": "^5.1.0",
68+
"copy-to-clipboard": "^3.3.3",
6869
"date-fns": "^3.6.0",
6970
"docx-preview": "^0.3.7",
7071
"favicon-fetch": "^1.0.0",

web/src/refresh-components/buttons/CopyIconButton.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"use client";
22

33
import { useEffect, useRef, useState } from "react";
4+
import copy from "copy-to-clipboard";
45
import { Button, ButtonProps } from "@opal/components";
56
import { SvgAlertTriangle, SvgCheck, SvgCopy } from "@opal/icons";
67

@@ -40,26 +41,19 @@ export default function CopyIconButton({
4041
}
4142

4243
try {
43-
// Check if Clipboard API is available
44-
if (!navigator.clipboard) {
45-
throw new Error("Clipboard API not available");
46-
}
47-
48-
// If HTML content getter is provided, copy both HTML and plain text
49-
if (getHtmlContent) {
44+
if (navigator.clipboard && getHtmlContent) {
5045
const htmlContent = getHtmlContent();
5146
const clipboardItem = new ClipboardItem({
5247
"text/html": new Blob([htmlContent], { type: "text/html" }),
5348
"text/plain": new Blob([text], { type: "text/plain" }),
5449
});
5550
await navigator.clipboard.write([clipboardItem]);
56-
}
57-
// Default: plain text only
58-
else {
51+
} else if (navigator.clipboard) {
5952
await navigator.clipboard.writeText(text);
53+
} else if (!copy(text)) {
54+
throw new Error("copy-to-clipboard returned false");
6055
}
6156

62-
// Show "copied" state
6357
setCopyState("copied");
6458
} catch (err) {
6559
console.error("Failed to copy:", err);

0 commit comments

Comments
 (0)