"use client"; import { useEffect, useRef, useState } from "react"; import "./copy-check.css"; /** * Copy Check * A copy button that answers: the two sheets of the clipboard slide together and fold into a * tick, the label rolls from "Copy" to "Copied", and after a moment it all rolls back. */ type Props = { text: string; label?: string; doneLabel?: string; className?: string }; export function CopyCheck({ text, label = "Copy", doneLabel = "Copied", className = "" }: Props) { const [done, setDone] = useState(false); const t = useRef>(undefined); useEffect(() => () => clearTimeout(t.current), []); const copy = async () => { // Confirm straight away; the write itself can take a moment (or wait on a permission). setDone(true); clearTimeout(t.current); t.current = setTimeout(() => setDone(false), 1800); try { await navigator.clipboard.writeText(text); } catch { const ta = document.createElement("textarea"); ta.value = text; document.body.appendChild(ta); ta.select(); document.execCommand("copy"); ta.remove(); } }; return ( ); }