copy-button.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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-black/10 dark:hover:bg-white/10 transition-colors ${className}`}
  18. aria-label="Copy code"
  19. >
  20. {copied ? (
  21. <svg
  22. width="14"
  23. height="14"
  24. viewBox="0 0 24 24"
  25. fill="none"
  26. stroke="currentColor"
  27. strokeWidth="2"
  28. strokeLinecap="round"
  29. strokeLinejoin="round"
  30. >
  31. <polyline points="20 6 9 17 4 12" />
  32. </svg>
  33. ) : (
  34. <svg
  35. width="14"
  36. height="14"
  37. viewBox="0 0 24 24"
  38. fill="none"
  39. stroke="currentColor"
  40. strokeWidth="2"
  41. strokeLinecap="round"
  42. strokeLinejoin="round"
  43. >
  44. <rect x="9" y="9" width="13" height="13" rx="2" ry="2" />
  45. <path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1" />
  46. </svg>
  47. )}
  48. </button>
  49. );
  50. }