forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopy-button.tsx
More file actions
81 lines (73 loc) · 2.55 KB
/
Copy pathcopy-button.tsx
File metadata and controls
81 lines (73 loc) · 2.55 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
75
76
77
78
79
80
81
// <CopyButton> — minimal clipboard button used by <Snippet>.
// Lives on the client so it can access navigator.clipboard.
"use client";
import React, { useEffect, useRef, useState } from "react";
type CopyState = "idle" | "copied" | "error";
export function CopyButton({ text }: { text: string }) {
const [state, setState] = useState<CopyState>("idle");
// Track the pending reset timer so we can clear it on unmount (avoids the
// React 18 "state update on an unmounted component" warning) and also
// overwrite a stale timer when the user clicks again before it fires.
const resetTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
useEffect(
() => () => {
if (resetTimerRef.current) clearTimeout(resetTimerRef.current);
},
[],
);
const copied = state === "copied";
const error = state === "error";
let label: string;
if (copied) label = "Copied";
else if (error) label = "Copy blocked";
else label = "Copy";
return (
<button
type="button"
onClick={async (e) => {
e.preventDefault();
// Clear any in-flight reset from a previous click so rapid-fire copies
// don't race each other and flip the label back early.
if (resetTimerRef.current) {
clearTimeout(resetTimerRef.current);
resetTimerRef.current = null;
}
try {
await navigator.clipboard.writeText(text);
setState("copied");
resetTimerRef.current = setTimeout(() => setState("idle"), 1500);
} catch (err) {
// Clipboard blocked (permissions, insecure context, etc.) — surface
// the failure to the user so they know the button didn't work, and
// log enough for devs to debug rather than silently swallowing.
console.warn("[copy-button] clipboard write failed", err);
setState("error");
resetTimerRef.current = setTimeout(() => setState("idle"), 2000);
}
}}
aria-label={label}
style={{
padding: "2px 8px",
fontSize: "10px",
lineHeight: 1.2,
border: "1px solid var(--border)",
borderRadius: "4px",
background: copied
? "var(--accent-light)"
: error
? "var(--bg-elevated)"
: "var(--bg-surface)",
color: copied
? "var(--accent)"
: error
? "var(--text)"
: "var(--text-muted)",
cursor: "pointer",
fontFamily: "inherit",
transition: "background 120ms, color 120ms",
}}
>
{label}
</button>
);
}