summaryrefslogtreecommitdiff
path: root/src/components/CopyButton.tsx
blob: 18a74339efb5a66a3485d6f5df1b29fdfb6d26f0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { useState } from 'react';
import { copyToClipboard } from '../lib/clipboard';

interface Props {
  text: string;
  label?: string;
  className?: string;
}

export function CopyButton({ text, label = 'Copy', className = '' }: Props) {
  const [copied, setCopied] = useState(false);

  const handle = async () => {
    const ok = await copyToClipboard(text);
    if (ok) {
      setCopied(true);
      setTimeout(() => setCopied(false), 1500);
    }
  };

  return (
    <button
      type="button"
      className={`copy-btn${copied ? ' copied' : ''} ${className}`}
      onClick={handle}
      title={label}
    >
      {copied ? '✓ Copied' : label}
    </button>
  );
}