| 3 | import { cn } from '@/lib/utils' |
| 4 | |
| 5 | export function CopyButton({ |
| 6 | value, |
| 7 | className, |
| 8 | }: { |
| 9 | value: string |
| 10 | className?: string |
| 11 | }) { |
| 12 | let [copyCount, setCopyCount] = useState(0) |
| 13 | let copied = copyCount > 0 |
| 14 | |
| 15 | useEffect(() => { |
| 16 | if (copyCount > 0) { |
| 17 | let timeout = setTimeout(() => setCopyCount(0), 1000) |
| 18 | return () => { |
| 19 | clearTimeout(timeout) |
| 20 | } |
| 21 | } |
| 22 | }, [copyCount]) |
| 23 | |
| 24 | return ( |
| 25 | <button |
| 26 | type="button" |
| 27 | className={cn( |
| 28 | `group/button absolute right-2 top-2 overflow-hidden rounded-full border border-gray-200 py-1 pl-2 pr-3 text-2xs font-medium backdrop-blur transition focus:opacity-100 dark:border-gray-600 ${className}`, |
| 29 | copied |
| 30 | ? 'bg-red-400/10 ring-1 ring-inset ring-red-400/20' |
| 31 | : 'bg-white/5 hover:bg-white/7.5 dark:bg-white/2.5 dark:hover:bg-white/5', |
| 32 | )} |
| 33 | onClick={() => { |
| 34 | window.navigator.clipboard.writeText(value).then(() => { |
| 35 | setCopyCount((count) => count + 1) |
| 36 | }) |
| 37 | }} |
| 38 | > |
| 39 | <span |
| 40 | aria-hidden={copied} |
| 41 | className={cn( |
| 42 | 'pointer-events-none flex items-center gap-0.5 text-zinc-400 transition duration-300', |
| 43 | copied && '-translate-y-1.5 opacity-0', |
| 44 | )} |
| 45 | > |
| 46 | <ClipboardIcon className="h-5 w-5 fill-zinc-500/20 stroke-zinc-500 transition-colors group-hover/button:stroke-zinc-400" /> |
| 47 | Copy |
| 48 | </span> |
| 49 | <span |
| 50 | aria-hidden={!copied} |
| 51 | className={cn( |
| 52 | 'pointer-events-none absolute inset-0 flex items-center justify-center text-red-400 transition duration-300', |
| 53 | !copied && 'translate-y-1.5 opacity-0', |
| 54 | )} |
| 55 | > |
| 56 | Copied! |
| 57 | </span> |
| 58 | </button> |
| 59 | ) |
| 60 | } |
| 61 | |
| 62 | function ClipboardIcon(props: React.ComponentPropsWithoutRef<'svg'>) { |