import { useRef, useState, type DragEvent } from "react"; import { cn } from "../lib/cn"; import { ImagePlusIcon, SpinnerIcon } from "./icons"; interface DropzoneProps { onFile: (file: File) => void; loading?: boolean; error?: string | null; } export function Dropzone({ onFile, loading = false, error }: DropzoneProps) { const inputRef = useRef(null); const [dragging, setDragging] = useState(false); function handleFiles(files: FileList | null) { const file = files?.[0]; if (file) onFile(file); } function onDrop(e: DragEvent) { e.preventDefault(); setDragging(false); handleFiles(e.dataTransfer.files); } return (
inputRef.current?.click()} onKeyDown={(e) => (e.key === "Enter" || e.key === " ") && inputRef.current?.click()} onDragOver={(e) => { e.preventDefault(); setDragging(true); }} onDragLeave={() => setDragging(false)} onDrop={onDrop} className={cn( "group relative flex w-full max-w-xl cursor-pointer flex-col items-center justify-center gap-4", "rounded-2xl border-2 border-dashed px-8 py-16 text-center transition-transform", dragging ? "border-accent-500 bg-accent-500/10" : "border-zinc-700 bg-zinc-900/60 hover:border-zinc-600 hover:bg-zinc-900", )} >
{loading ? : }

{loading ? "Loading image…" : dragging ? "Drop to load" : "Drop an image here"}

or click to browse · paste from your clipboard

JPEG · PNG · WebP · AVIF · GIF · BMP · SVG

{error && (

{error}

)} { handleFiles(e.target.files); e.target.value = ""; }} />
); }