The weeb for the next gen discord boat - Wamellow wamellow.com
bot discord
at master 1.2 kB view raw
1"use client"; 2 3import { cn } from "@/utils/cn"; 4import { useRef, useState } from "react"; 5 6import { Button } from "./ui/button"; 7 8interface Props { 9 className?: string; 10 icon?: React.ReactNode; 11 text: string; 12 title?: string; 13 needsWait?: boolean; 14} 15 16export function CopyToClipboardButton({ 17 className, 18 icon, 19 text, 20 title 21}: Props) { 22 const timeoutRef = useRef<NodeJS.Timeout | null>(null); 23 24 const [saved, setSaved] = useState<boolean>(false); 25 26 function handleCopy(t: string) { 27 navigator.clipboard.writeText(t); 28 setSaved(true); 29 30 if (timeoutRef.current) clearInterval(timeoutRef.current); 31 timeoutRef.current = setTimeout(() => setSaved(false), 1_000 * 2); 32 } 33 34 return ( 35 <Button 36 className={cn("w-full justify-start! truncate", className)} 37 variant={saved 38 ? "secondary" 39 : undefined 40 } 41 onClick={() => handleCopy(text)} 42 > 43 {icon} 44 {saved 45 ? "Copied to clipboard" 46 : (title || "Copy to clipboard") 47 } 48 </Button> 49 ); 50}