import React, { useState } from 'react'; import { Check, Copy } from 'lucide-react'; interface CopyButtonProps { /** Text to copy to clipboard */ text: string; /** Background color the gradient fades into (must match parent surface) */ bgColor?: string; /** Render as inline element instead of absolute overlay */ inline?: boolean; } /** * Copy-to-clipboard button with two modes: * * **Overlay** (default): Absolute-positioned in top-right corner, visible on * group hover. A horizontal gradient fades from transparent to `bgColor` so * text behind the button isn't abruptly covered. * Requires an ancestor with `group` and `relative` classes. * * **Inline** (`inline`): Normal-flow button for use inside headers/toolbars. */ export const CopyButton: React.FC = ({ text, bgColor = 'var(--code-bg)', inline = false, }) => { const [isCopied, setIsCopied] = useState(false); const handleCopy = async (): Promise => { try { await navigator.clipboard.writeText(text); setIsCopied(true); setTimeout(() => setIsCopied(false), 2000); } catch { // Silently fail — clipboard API may be unavailable } }; const icon = isCopied ? ( ) : ( ); if (inline) { return ( ); } return (
{/* Gradient fade from transparent to bgColor so text isn't obscured */}
{/* Solid background holding the button */}
); };