forked from Lilly-Protocol/lily-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopyButton.tsx
More file actions
74 lines (69 loc) · 1.87 KB
/
Copy pathCopyButton.tsx
File metadata and controls
74 lines (69 loc) · 1.87 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use client";
import { useState } from "react";
import { copyText } from "@/lib/clipboard";
interface CopyButtonProps {
text: string;
label?: string;
copiedLabel?: string;
className?: string;
}
export function CopyButton({
text,
label = "Copy",
copiedLabel = "Copied!",
className = "",
}: CopyButtonProps) {
const [copied, setCopied] = useState(false);
const handleClick = async () => {
const ok = await copyText(text);
if (ok) {
setCopied(true);
setTimeout(() => setCopied(false), 2000);
}
};
return (
<button
type="button"
onClick={handleClick}
className={`inline-flex items-center gap-1.5 rounded border border-[var(--color-line)] bg-[var(--color-surface)] px-3 py-1.5 text-sm transition hover:bg-[var(--color-accent)]/10 ${className}`}
aria-live="polite"
>
{copied ? (
<>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4 text-green-600"
aria-hidden="true"
>
<polyline points="20 6 9 17 4 12" />
</svg>
<span>{copiedLabel}</span>
</>
) : (
<>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className="h-4 w-4"
aria-hidden="true"
>
<rect x="9" y="9" width="13" height="13" rx="2" ry="2" />
<path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />
</svg>
<span>{label}</span>
</>
)}
</button>
);
}