mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-06-05 20:15:31 -04:00
66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import copy from "copy-to-clipboard";
|
|
import { CheckIcon, CopyIcon } from "lucide-react";
|
|
import Button from "@/components/ui/button";
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import { cn } from "@/lib/utils";
|
|
|
|
function CopyButton({
|
|
value,
|
|
className,
|
|
variant = "ghost",
|
|
tooltip = "Copy to Clipboard",
|
|
...props
|
|
}: React.ComponentProps<typeof Button> & {
|
|
value: string;
|
|
tooltip?: string;
|
|
}) {
|
|
const [hasCopied, setHasCopied] = React.useState(false);
|
|
const timeoutRef = React.useRef<NodeJS.Timeout | undefined>(undefined);
|
|
|
|
React.useEffect(() => {
|
|
return () => {
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
const handleCopy = () => {
|
|
copy(value);
|
|
setHasCopied(true);
|
|
|
|
if (timeoutRef.current) {
|
|
clearTimeout(timeoutRef.current);
|
|
}
|
|
timeoutRef.current = setTimeout(() => setHasCopied(false), 2000);
|
|
};
|
|
|
|
return (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<Button
|
|
data-slot="copy-button"
|
|
data-copied={hasCopied}
|
|
size="icon"
|
|
variant={variant}
|
|
className={cn(
|
|
"bg-code absolute top-3 right-2 z-10 size-7 hover:opacity-100 focus-visible:opacity-100",
|
|
className
|
|
)}
|
|
onClick={handleCopy}
|
|
aria-label={hasCopied ? "Copied" : tooltip}
|
|
{...props}
|
|
>
|
|
{hasCopied ? <CheckIcon aria-hidden="true" /> : <CopyIcon aria-hidden="true" />}
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>{hasCopied ? "Copied" : tooltip}</TooltipContent>
|
|
</Tooltip>
|
|
);
|
|
}
|
|
|
|
export default CopyButton;
|