"use client"; import React, { useState, useEffect, useRef } from 'react'; import { QRCodeCanvas } from 'qrcode.react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Slider } from '@/components/ui/slider'; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; import { Download, ImagePlus, Palette, Settings, Trash2, XCircle } from 'lucide-react'; import { AppLogoIcon } from '@/components/icons/app-logo-icon'; import { useToast } from "@/hooks/use-toast"; type ErrorCorrectionLevel = 'L' | 'M' | 'Q' | 'H'; export default function QRCodeCanvasPage() { const [text, setText] = useState('hello qr'); const [fgColor, setFgColor] = useState('#805CFF'); // Default to new dark primary const [bgColor, setBgColor] = useState('#FFFFFF'); const [level, setLevel] = useState('M'); const [qrSize, setQrSize] = useState(256); const [logoSrc, setLogoSrc] = useState(null); const [logoFile, setLogoFile] = useState(null); const [mounted, setMounted] = useState(false); const { toast } = useToast(); const fileInputRef = useRef(null); useEffect(() => { setMounted(true); }, []); const handleLogoChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setLogoFile(file); const reader = new FileReader(); reader.onloadend = () => { setLogoSrc(reader.result as string); }; reader.readAsDataURL(file); } }; const removeLogo = () => { setLogoSrc(null); setLogoFile(null); if (fileInputRef.current) { fileInputRef.current.value = ''; } }; const downloadQRCode = () => { if (!text) { toast({ title: "Error", description: "Please enter text or URL to generate QR code.", variant: "destructive", }); return; } const canvas = document.getElementById('qr-canvas') as HTMLCanvasElement; if (canvas) { try { const pngUrl = canvas.toDataURL('image/png'); const downloadLink = document.createElement('a'); downloadLink.href = pngUrl; downloadLink.download = 'qrcode.png'; document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink); toast({ title: "Success!", description: "QR Code downloaded successfully.", }); } catch (error) { console.error("Failed to download QR Code:", error); toast({ title: "Error", description: "Failed to download QR Code. The logo might be from a restricted source if hosted externally. Try uploading it directly.", variant: "destructive", }); } } }; const imageSettings = logoSrc ? { src: logoSrc, height: qrSize * 0.22, width: qrSize * 0.22, excavate: true, x: undefined, y: undefined, } : undefined; const qrKey = JSON.stringify({ text, fgColor, bgColor, level, qrSize, logoSrc }); return (

QRCode Canvas

Create and customize your QR codes with ease.

Customize Tailor your QR code to match your style.