forked from pingdotgg/t3code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageCopyButton.tsx
More file actions
64 lines (61 loc) · 1.59 KB
/
MessageCopyButton.tsx
File metadata and controls
64 lines (61 loc) · 1.59 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
import { memo } from "react";
import { CopyIcon, CheckIcon } from "lucide-react";
import { Button } from "../ui/button";
import { useCopyToClipboard } from "~/hooks/useCopyToClipboard";
import { cn } from "~/lib/utils";
type CopyCallbacks = {
onCopy?: () => void;
onError?: (error: Error) => void;
};
export const MessageCopyButton = memo(function MessageCopyButton({
text,
label,
title = "Copy message",
disabled = false,
disabledTitle,
size = "xs",
variant = "outline",
className,
onCopy,
onError,
}: {
text: string;
label?: string;
title?: string;
disabled?: boolean;
disabledTitle?: string;
size?: "xs" | "icon-xs";
variant?: "outline" | "ghost";
className?: string;
onCopy?: () => void;
onError?: (error: Error) => void;
}) {
const { copyToClipboard, isCopied } = useCopyToClipboard<CopyCallbacks>({
onCopy: (callbacks) => {
callbacks.onCopy?.();
},
onError: (error, callbacks) => {
callbacks.onError?.(error);
},
});
const buttonTitle = disabled ? (disabledTitle ?? title) : isCopied ? "Copied" : title;
const copyCallbacks = {
...(onCopy ? { onCopy } : {}),
...(onError ? { onError } : {}),
};
return (
<Button
type="button"
size={size}
variant={variant}
className={cn(className)}
disabled={disabled}
onClick={() => copyToClipboard(text, copyCallbacks)}
title={buttonTitle}
aria-label={buttonTitle}
>
{isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />}
{label ? <span>{label}</span> : null}
</Button>
);
});