mirror of
https://github.com/pmndrs/zustand.git
synced 2025-12-08 19:45:52 +00:00
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:
parent
7470e9c6e8
commit
c0e074f19a
@ -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 (
|
||||
<>
|
||||
|
||||
@ -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)
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user