From fb6cff2fe3101e80d2932285c0b03e0d5be872f8 Mon Sep 17 00:00:00 2001
From: Sebastien Castiel <sebastien@castiel.me>
Date: Thu, 7 Dec 2023 19:38:03 -0500
Subject: Share button

---
 src/components/copy-button.tsx | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 src/components/copy-button.tsx

(limited to 'src/components/copy-button.tsx')

diff --git a/src/components/copy-button.tsx b/src/components/copy-button.tsx
new file mode 100644
index 0000000..4220353
--- /dev/null
+++ b/src/components/copy-button.tsx
@@ -0,0 +1,34 @@
+'use client'
+import { Button } from '@/components/ui/button'
+import { Check, Copy } from 'lucide-react'
+import { useEffect, useState } from 'react'
+
+type Props = { text: string }
+
+export function CopyButton({ text }: Props) {
+  const [copied, setCopied] = useState(false)
+
+  useEffect(() => {
+    if (copied) {
+      let timeout = setTimeout(() => setCopied(false), 1000)
+      return () => {
+        setCopied(false)
+        clearTimeout(timeout)
+      }
+    }
+  }, [copied])
+
+  return (
+    <Button
+      size="icon"
+      variant="secondary"
+      type="button"
+      onClick={() => {
+        navigator.clipboard.writeText(text)
+        setCopied(true)
+      }}
+    >
+      {copied ? <Check className="w-4 h-4" /> : <Copy className="w-4 h-4" />}
+    </Button>
+  )
+}
-- 
cgit v1.3

