49 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-10-28 18:23:21 +05:30
import React, { useState } from 'react';
2024-10-25 05:41:04 +05:30
import { QRCodeSVG } from 'qrcode.react';
2024-10-28 18:23:21 +05:30
import { Button } from "@/components/ui/button";
import { Check, Copy } from "lucide-react";
2024-10-25 05:41:04 +05:30
interface QRCodeProps {
text: string;
width?: number;
height?: number;
2024-10-25 05:41:04 +05:30
}
const QRCode: React.FC<QRCodeProps> = ({ text, width = 256, height = 256 }) => {
2024-10-28 18:23:21 +05:30
const [copied, setCopied] = useState(false);
2024-11-04 21:57:12 +05:30
const isMobile = window.innerWidth < 640;
2024-10-28 18:23:21 +05:30
const handleCopy = async () => {
await navigator.clipboard.writeText(text);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
2024-10-25 05:41:04 +05:30
return (
<div className="flex flex-col items-center space-y-4">
2024-11-04 21:57:12 +05:30
<QRCodeSVG
value={text}
size={isMobile ? Math.min(width * 0.8, window.innerWidth - 64) : Math.min(width, height)}
/>
<div className="flex items-center space-x-2 w-full max-w-[300px]">
2024-10-28 18:23:21 +05:30
<input
type="text"
value={text}
readOnly
2024-11-04 21:57:12 +05:30
className="flex-1 px-3 py-2 text-xs sm:text-sm border rounded-md bg-muted truncate"
2024-10-28 18:23:21 +05:30
/>
<Button
variant="outline"
size="icon"
onClick={handleCopy}
2024-11-04 21:57:12 +05:30
className="shrink-0"
2024-10-28 18:23:21 +05:30
>
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
</Button>
</div>
2024-10-25 05:41:04 +05:30
</div>
);
};
export default QRCode;