mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
40 lines
935 B
TypeScript
40 lines
935 B
TypeScript
"use client";
|
|
|
|
import { useEffect, useState } from "react";
|
|
import { CheckIcon, Copy } from "lucide-react";
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
export async function copyToClipboardWithMeta(value: string) {
|
|
navigator.clipboard.writeText(value);
|
|
}
|
|
|
|
export type CopyButtonProps = {
|
|
value: string;
|
|
className?: string;
|
|
};
|
|
|
|
export const CopyButton = (props: CopyButtonProps) => {
|
|
const { value } = props;
|
|
|
|
const [hasCopied, setHasCopied] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setTimeout(() => {
|
|
setHasCopied(false);
|
|
}, 2000);
|
|
}, [hasCopied]);
|
|
|
|
return (
|
|
<Button
|
|
onClick={() => {
|
|
copyToClipboardWithMeta(value);
|
|
setHasCopied(true);
|
|
}}
|
|
{...props}
|
|
>
|
|
<span className="mr-2">Copy</span>
|
|
{hasCopied ? <CheckIcon /> : <Copy size="18" />}
|
|
</Button>
|
|
);
|
|
};
|