fix #180: Added copy button

Used ref to point to parent div and got inner text of the div and write
it to the clipboard.
This commit is contained in:
ritikprajapat21 2025-07-09 19:42:41 +05:30
parent 2d6fb98130
commit 8530226edb
2 changed files with 361 additions and 245 deletions

View file

@ -0,0 +1,33 @@
"use client";
import { useState } from "react";
import type { RefObject } from "react";
import { Button } from "./ui/button";
import { Copy, CopyCheck } from "lucide-react";
export default function CopyButton({
ref,
}: {
ref: RefObject<HTMLDivElement | null>;
}) {
const [copy, setCopy] = useState(false);
const handleClick = () => {
if (ref.current) {
const text = ref.current.innerText;
navigator.clipboard.writeText(text);
setCopy(true);
setTimeout(() => {
setCopy(false);
}, 500);
}
};
return (
<div className="w-full flex justify-end">
<Button variant="ghost" onClick={handleClick}>
{copy ? <CopyCheck /> : <Copy />}
</Button>
</div>
);
}