forked from Lilly-Protocol/lily-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-button.tsx
More file actions
58 lines (48 loc) · 1.57 KB
/
Copy pathcopy-button.tsx
File metadata and controls
58 lines (48 loc) · 1.57 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
"use client";
import { useState } from "react";
import { copyText } from "@/lib/clipboard";
type CopyButtonProps = {
readonly text: string;
readonly label?: string;
readonly copiedLabel?: string;
readonly failedLabel?: string;
readonly className?: string;
};
type CopyState = "idle" | "copied" | "failed";
export function CopyButton({
text,
label = "Copy",
copiedLabel = "Copied",
failedLabel = "Copy failed",
className,
}: CopyButtonProps) {
const [copyState, setCopyState] = useState<CopyState>("idle");
async function handleCopy() {
const result = await copyText(text);
setCopyState(result.ok ? "copied" : "failed");
}
const buttonLabel =
copyState === "copied"
? copiedLabel
: copyState === "failed"
? failedLabel
: label;
return (
<button
aria-live="polite"
className={cx(
"inline-flex min-h-10 items-center justify-center rounded-full border border-[var(--color-line)] px-4 py-2 text-sm font-medium text-[var(--color-ink)] transition-colors hover:border-[var(--color-accent)] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-[var(--color-accent)] disabled:cursor-not-allowed disabled:opacity-60 motion-reduce:transition-none",
copyState === "copied" &&
"border-[var(--color-accent)] bg-[var(--color-panel-muted)]",
className,
)}
onClick={handleCopy}
type="button"
>
{buttonLabel}
</button>
);
}
function cx(...classNames: Array<string | false | undefined>) {
return classNames.filter(Boolean).join(" ");
}