Skip to content

Commit 8b26144

Browse files
committed
fix: add copy fallback for insecure contexts
1 parent 0585372 commit 8b26144

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

site/src/components/CopyButton/CopyButton.tsx

+14-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
2929
try {
3030
await window.navigator.clipboard.writeText(text)
3131
setIsCopied(true)
32-
3332
window.setTimeout(() => {
3433
setIsCopied(false)
3534
}, 1000)
3635
} catch (err) {
37-
const wrappedErr = new Error("copyToClipboard: failed to copy text to clipboard")
38-
if (err instanceof Error) {
39-
wrappedErr.stack = err.stack
36+
const input = document.createElement("input")
37+
input.hidden = true
38+
input.value = text
39+
document.body.appendChild(input)
40+
input.focus()
41+
input.select()
42+
const result = document.execCommand("copy")
43+
document.body.removeChild(input)
44+
if (!result) {
45+
const wrappedErr = new Error("copyToClipboard: failed to copy text to clipboard")
46+
if (err instanceof Error) {
47+
wrappedErr.stack = err.stack
48+
}
49+
console.error(wrappedErr)
4050
}
41-
console.error(wrappedErr)
4251
}
4352
}
4453

0 commit comments

Comments
 (0)