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
43 lines (41 loc) · 1.16 KB
/
MessageCopyButton.tsx
File metadata and controls
43 lines (41 loc) · 1.16 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
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";
export const MessageCopyButton = memo(function MessageCopyButton({
text,
title = "Copy message",
size = "xs",
variant = "outline",
className,
onCopy,
onError,
}: {
text: string;
title?: string;
size?: "xs" | "icon-xs";
variant?: "outline" | "ghost";
className?: string;
onCopy?: () => void;
onError?: (error: Error) => void;
}) {
const { copyToClipboard, isCopied } = useCopyToClipboard<void>({
...(onCopy ? { onCopy: () => onCopy() } : {}),
...(onError ? { onError: (error: Error) => onError(error) } : {}),
});
const buttonTitle = isCopied ? "Copied" : title;
return (
<Button
type="button"
size={size}
variant={variant}
className={cn(className)}
onClick={() => copyToClipboard(text, undefined)}
title={buttonTitle}
aria-label={buttonTitle}
>
{isCopied ? <CheckIcon className="size-3 text-success" /> : <CopyIcon className="size-3" />}
</Button>
);
});