Skip to content

Commit

Permalink
refactor(example): updated with the latest version of the clipboard A…
Browse files Browse the repository at this point in the history
…PI (pmndrs#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 <[email protected]>
  • Loading branch information
tbor00 and dai-shi authored Jul 11, 2022
1 parent 7470e9c commit c0e074f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
8 changes: 5 additions & 3 deletions examples/src/components/CopyButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down
13 changes: 2 additions & 11 deletions examples/src/utils/copy-to-clipboard.js
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit c0e074f

Please sign in to comment.