Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than f85898f point at it.

chore(eslint.config.mjs): update 'eslint-plugin-react-hooks', 'eslint-plugin-react-compiler' config (#3043)Wonsuk Choibefore adoptionf85898f

lines 1–47why, one step back
1import { useState, useCallback, useRef } from 'react'
2import { copyToClipboard } from '../utils/copy-to-clipboard'34/*5Isolated logic for the entire copy functionality instead6of a separate button component and with the added utility7*/8export default function CopyButton({ code, ...props }) {9 const [isCopied, setIsCopied] = useState(false)10 const timer = useRef()1112 const handleCopy = useCallback(() => {13 clearTimeout(timer.current)14 copyToClipboard(code).then(() => {15 setIsCopied(true)16 timer.current = setTimeout(() => setIsCopied(false), 3000)17 })18 }, [code])1920 return (21 <>22 <button className="copy-button" onClick={handleCopy} {...props}>23 {isCopied ? (24 'Copied!'25 ) : (26 <>27 <svg28 xmlns="http://www.w3.org/2000/svg"29 width={16}30 height={16}31 viewBox="0 0 24 24"32 fill="none"33 stroke="currentColor"34 strokeWidth={2}35 strokeLinecap="round"36 strokeLinejoin="round"37 {...props}38 >39 <rect x={9} y={9} width={13} height={13} rx={2} ry={2} />40 <path d="M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1" />41 </svg>42 </>43 )}44 </button>45 </>46 )47}