mirror of
https://github.com/MacRimi/ProxMenux.git
synced 2025-06-28 04:06:54 +00:00
Create CopyableCode.tsx
This commit is contained in:
parent
c9976ad34f
commit
04f929103b
41
web/app/components/CopyableCode.tsx
Normal file
41
web/app/components/CopyableCode.tsx
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import type React from "react"
|
||||||
|
import { useState } from "react"
|
||||||
|
import { Copy, Check } from "lucide-react"
|
||||||
|
|
||||||
|
interface CopyableCodeProps {
|
||||||
|
code: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const CopyableCode: React.FC<CopyableCodeProps> = ({ code }) => {
|
||||||
|
const [isCopied, setIsCopied] = useState(false)
|
||||||
|
|
||||||
|
const copyToClipboard = async () => {
|
||||||
|
try {
|
||||||
|
await navigator.clipboard.writeText(code)
|
||||||
|
setIsCopied(true)
|
||||||
|
setTimeout(() => setIsCopied(false), 2000)
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Failed to copy text: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="relative">
|
||||||
|
<pre className="bg-gray-100 p-4 rounded-md overflow-x-auto">
|
||||||
|
<code>{code}</code>
|
||||||
|
</pre>
|
||||||
|
<button
|
||||||
|
onClick={copyToClipboard}
|
||||||
|
className="absolute top-2 right-2 p-2 bg-white rounded-md shadow-sm hover:bg-gray-100 transition-colors"
|
||||||
|
aria-label="Copy to clipboard"
|
||||||
|
>
|
||||||
|
{isCopied ? <Check className="h-5 w-5 text-green-500" /> : <Copy className="h-5 w-5 text-gray-500" />}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CopyableCode
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user