mirror of
https://github.com/coder/coder.git
synced 2025-07-09 11:45:56 +00:00
fix: add copy fallback for insecure contexts (#2044)
This commit is contained in:
@ -29,16 +29,29 @@ export const CopyButton: React.FC<CopyButtonProps> = ({
|
||||
try {
|
||||
await window.navigator.clipboard.writeText(text)
|
||||
setIsCopied(true)
|
||||
|
||||
window.setTimeout(() => {
|
||||
setIsCopied(false)
|
||||
}, 1000)
|
||||
} catch (err) {
|
||||
const wrappedErr = new Error("copyToClipboard: failed to copy text to clipboard")
|
||||
if (err instanceof Error) {
|
||||
wrappedErr.stack = err.stack
|
||||
const input = document.createElement("input")
|
||||
input.value = text
|
||||
document.body.appendChild(input)
|
||||
input.focus()
|
||||
input.select()
|
||||
const result = document.execCommand("copy")
|
||||
document.body.removeChild(input)
|
||||
if (result) {
|
||||
setIsCopied(true)
|
||||
window.setTimeout(() => {
|
||||
setIsCopied(false)
|
||||
}, 1000)
|
||||
} else {
|
||||
const wrappedErr = new Error("copyToClipboard: failed to copy text to clipboard")
|
||||
if (err instanceof Error) {
|
||||
wrappedErr.stack = err.stack
|
||||
}
|
||||
console.error(wrappedErr)
|
||||
}
|
||||
console.error(wrappedErr)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user