"use client"; import type { MouseEvent } from "react"; import { useRef, useState } from "react"; export function CopyCodeButton() { const [state, setState] = useState("idle"); const timer = useRef | null>(null); async function handleClick(event: MouseEvent) { const button = event.currentTarget; const block = button.closest("figure[data-rehype-pretty-code-figure], div[data-code-block]"); const code = block?.querySelector("code"); const text = code?.textContent ?? ""; if (!text) return; try { await navigator.clipboard.writeText(text); setState("copied"); } catch { setState("failed"); } if (timer.current) clearTimeout(timer.current); timer.current = setTimeout(() => setState("idle"), 1400); } const color = state === "copied" ? "text-success" : state === "failed" ? "text-danger" : "text-muted"; return ( ); }