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
69 lines (61 loc) · 2.44 KB
/
Copy pathcopy-button.tsx
File metadata and controls
69 lines (61 loc) · 2.44 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
// <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}
className={[
"shell-docs-radius-control cursor-pointer border border-[var(--border)] px-2 py-0.5 text-[10px] leading-[1.2] transition-colors",
copied
? "bg-[var(--accent-light)] text-[var(--accent)]"
: error
? "bg-[var(--bg-elevated)] text-[var(--text)]"
: "bg-[var(--bg-surface)] text-[var(--text-muted)] hover:bg-[var(--bg-elevated)] hover:text-[var(--text)]",
].join(" ")}
>
{label}
</button>
);
}