aboutsummaryrefslogtreecommitdiff
path: root/packages/ui/copy-button.tsx
blob: 3e5fa75d5936e44ebd444cfefefdb01bb98f0b98 (plain) (blame)
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
"use client";

import { cn } from "@lib/utils";
import { Button, type buttonVariants } from "@ui/components/button";
import type { VariantProps } from "class-variance-authority";
import { CheckIcon, ClipboardIcon } from "lucide-react";
import * as React from "react";
import { useEffect } from "react";

interface CopyButtonProps
	extends React.ComponentProps<"button">,
		VariantProps<typeof buttonVariants> {
	value: string;
	src?: string;
}

export function CopyButton({
	value,
	className,
	src,
	variant = "ghost",
	...props
}: CopyButtonProps) {
	const [hasCopied, setHasCopied] = React.useState(false);

	useEffect(() => {
		setTimeout(() => {
			setHasCopied(false);
		}, 2000);
	}, []);

	return (
		<Button
			className={cn(
				"relative z-10 text-zinc-50 hover:bg-zinc-700 hover:text-zinc-50 [&_svg]:h-full [&_svg]:w-full",
				className,
			)}
			onClick={() => {
				navigator.clipboard.writeText(value);
				setHasCopied(true);
			}}
			size="icon"
			variant={variant}
			{...props}
		>
			<span className="sr-only">Copy</span>
			{hasCopied ? <CheckIcon /> : <ClipboardIcon />}
		</Button>
	);
}