"use client"; import { forwardRef, useEffect, useRef, useState, type ButtonHTMLAttributes, } from "react"; import { Mark } from "../ui/Mark.tsx"; async function copyText(text: string): Promise { try { await navigator.clipboard.writeText(text); return true; } catch { try { const ta = document.createElement("textarea"); ta.value = text; ta.style.position = "fixed"; ta.style.opacity = "0"; document.body.appendChild(ta); ta.select(); try { return document.execCommand("copy"); } finally { ta.remove(); } } catch { return false; } } } export const CopyButton = forwardRef< HTMLButtonElement, ButtonHTMLAttributes & { text: string; feedback?: string } >(function CopyButton( { text, feedback = "Copied", className = "", children, onClick, ...props }, ref, ) { const [copied, setCopied] = useState(false); const [failed, setFailed] = useState(false); const timer = useRef(undefined); useEffect(() => () => window.clearTimeout(timer.current), []); const onCopy = async () => { const ok = await copyText(text); window.clearTimeout(timer.current); setCopied(ok); setFailed(!ok); if (ok) timer.current = window.setTimeout(() => setCopied(false), 1400); }; return ( ); });