import { useCallback, useEffect, useRef, useState } from "react"; import { cn } from "@/lib/utils"; import { copyTextToClipboard } from "@/lib/clipboard"; interface CopyTextProps { text: string; /** What to display. Defaults to `text`. */ children?: React.ReactNode; containerClassName?: string; className?: string; ariaLabel?: string; title?: string; /** Tooltip message shown after copying. Default: "Copied!" */ copiedLabel?: string; } export function CopyText({ text, children, containerClassName, className, ariaLabel, title, copiedLabel = "Copied!", }: CopyTextProps) { const [visible, setVisible] = useState(false); const [label, setLabel] = useState(copiedLabel); const timerRef = useRef>(undefined); const triggerRef = useRef(null); useEffect(() => () => clearTimeout(timerRef.current), []); const handleClick = useCallback(async () => { try { await copyTextToClipboard(text); setLabel(copiedLabel); } catch { setLabel("Copy failed"); } clearTimeout(timerRef.current); setVisible(true); timerRef.current = setTimeout(() => setVisible(false), 1500); }, [copiedLabel, text]); return ( {label} ); }