File size: 949 Bytes
8c1b9fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
"use client";
import { useState } from "react";

// Small copy-to-clipboard button with transient "Copied" feedback. Takes a
// getter so it works for both message text and (via innerText) code blocks.
export function CopyButton({
  getText,
  className = "",
  label = "Copy",
}: {
  getText: () => string;
  className?: string;
  label?: string;
}) {
  const [copied, setCopied] = useState(false);
  async function copy() {
    try {
      await navigator.clipboard.writeText(getText());
      setCopied(true);
      setTimeout(() => setCopied(false), 1400);
    } catch {
      /* clipboard blocked (insecure context); silently ignore */
    }
  }
  return (
    <button
      type="button"
      onClick={copy}
      aria-label={copied ? "Copied" : label}
      className={`inline-flex items-center gap-1 text-xs text-slate-400 transition hover:text-brand ${className}`}
    >
      {copied ? "✓ Copied" : `⧉ ${label}`}
    </button>
  );
}