45 lines
985 B
TypeScript
45 lines
985 B
TypeScript
/**
|
|
* 複製按鈕元件
|
|
*/
|
|
|
|
import { Copy, Check } from "lucide-react";
|
|
import { useState } from "react";
|
|
import { Button } from "../ui/button";
|
|
import { toast } from "sonner@2.0.3";
|
|
|
|
interface CopyButtonProps {
|
|
text: string;
|
|
label?: string;
|
|
}
|
|
|
|
export function CopyButton({ text, label }: CopyButtonProps) {
|
|
const [copied, setCopied] = useState(false);
|
|
|
|
const handleCopy = async () => {
|
|
try {
|
|
await navigator.clipboard.writeText(text);
|
|
setCopied(true);
|
|
toast.success("已複製到剪貼簿");
|
|
setTimeout(() => setCopied(false), 2000);
|
|
} catch (err) {
|
|
toast.error("複製失敗");
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={handleCopy}
|
|
className="h-6 w-6 p-0 hover:bg-accent"
|
|
title={label || "複製"}
|
|
>
|
|
{copied ? (
|
|
<Check className="h-3 w-3 text-green-600" />
|
|
) : (
|
|
<Copy className="h-3 w-3 text-muted-foreground" />
|
|
)}
|
|
</Button>
|
|
);
|
|
}
|