refactor(example): updated with the latest version of the clipboard API (#1072)

* refactor: updated with the latest version of the clipboard API in example zustand

* Update examples/src/utils/copy-to-clipboard.js

Co-authored-by: Daishi Kato <dai-shi@users.noreply.github.com>
This commit is contained in:
Timoteo 2022-07-11 05:54:18 -03:00 committed by GitHub
parent 7470e9c6e8
commit c0e074f19a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 14 deletions

View File

@ -9,10 +9,12 @@ export default function CopyButton({ code, ...props }) {
const [isCopied, setIsCopied] = useState(false)
const handleCopy = () => {
copyToClipboard(code)
setIsCopied(true)
setTimeout(() => setIsCopied(false), 3000)
copyToClipboard(code).then(() => {
setIsCopied(true)
setTimeout(() => setIsCopied(false), 3000)
})
}
return (
<>

View File

@ -1,12 +1,3 @@
export const copyToClipboard = (str) => {
const el = document.createElement('textarea')
el.value = str
el.setAttribute('readonly', '')
el.style.position = 'absolute'
el.style.left = '-9999px'
document.body.appendChild(el)
el.select()
// FIXME: might need to update this with the clipboard API in the future
document.execCommand('copy')
document.body.removeChild(el)
}
return navigator.clipboard.writeText(str)
}