/** * Inline copy-link button — click to copy the current page URL. * Shows a brief "Copied" tooltip on success. * Styled as a subtle icon that appears on hover of the parent. */ import { useState, useCallback } from "react"; interface CopyLinkButtonProps { url?: string; // defaults to window.location.href className?: string; } export default function CopyLinkButton({ url, className = "" }: CopyLinkButtonProps) { const [copied, setCopied] = useState(false); const handleCopy = useCallback(async (e: React.MouseEvent) => { e.preventDefault(); e.stopPropagation(); const target = url || window.location.href; try { await navigator.clipboard.writeText(target); } catch { // Fallback for non-HTTPS const ta = document.createElement("textarea"); ta.value = target; ta.style.position = "fixed"; ta.style.opacity = "0"; document.body.appendChild(ta); ta.select(); document.execCommand("copy"); document.body.removeChild(ta); } setCopied(true); setTimeout(() => setCopied(false), 1500); }, [url]); return ( ); }