CopyButton.jsx
This file
Path
examples/demo/src/components/CopyButton.jsx
47 lines · 1.3 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import { useState, useCallback, useRef } from 'react'import { copyToClipboard } from '../utils/copy-to-clipboard'
/*Isolated logic for the entire copy functionality insteadof a separate button component and with the added utility*/export default function CopyButton({ code, ...props }) { const [isCopied, setIsCopied] = useState(false) const timer = useRef()
const handleCopy = useCallback(() => { clearTimeout(timer.current) copyToClipboard(code).then(() => { setIsCopied(true) timer.current = setTimeout(() => setIsCopied(false), 3000) }) }, [code])
return ( <> <button className="copy-button" onClick={handleCopy} {...props}> {isCopied ? ( 'Copied!' ) : ( <> <svg xmlns="http://www.w3.org/2000/svg" width={16} height={16} viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth={2} strokeLinecap="round" strokeLinejoin="round" {...props} > <rect x={9} y={9} width={13} height={13} rx={2} ry={2} /> <path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1" /> </svg> </> )} </button> </> )}