copy-button.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use client";
  2. import { useState } from "react";
  3. interface CopyButtonProps {
  4. text: string;
  5. className?: string;
  6. }
  7. export function CopyButton({ text, className = "" }: CopyButtonProps) {
  8. const [copied, setCopied] = useState(false);
  9. const handleCopy = async () => {
  10. await navigator.clipboard.writeText(text);
  11. setCopied(true);
  12. setTimeout(() => setCopied(false), 2000);
  13. };
  14. return (
  15. <button
  16. onClick={handleCopy}
  17. className={`p-1.5 rounded hover:bg-white/10 transition-colors ${className}`}
  18. aria-label="Copy code"
  19. >
  20. {copied ? (
  21. <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
  22. <polyline points="20 6 9 17 4 12" />
  23. </svg>
  24. ) : (
  25. <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
  26. <rect x="9" y="9" width="13" height="13" rx="2" ry="2" />
  27. <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />
  28. </svg>
  29. )}
  30. </button>
  31. );
  32. }