import { useRef, useState } from "react"; const ACCEPTED = ["image/jpeg", "image/png", "image/webp"]; const MAX_BYTES = 10 * 1024 * 1024; export default function UploadZone({ onFileSelected, disabled = false }) { const inputRef = useRef(null); const [dragActive, setDragActive] = useState(false); const validate = (file) => { if (!file) return "No file selected."; if (!ACCEPTED.includes(file.type)) { return "Unsupported format. Use JPG, PNG, or WEBP."; } if (file.size > MAX_BYTES) { return "File exceeds 10 MB limit."; } return null; }; const handleFile = (file) => { const error = validate(file); onFileSelected(file ?? null, error); }; const onDrop = (e) => { e.preventDefault(); setDragActive(false); if (disabled) return; const file = e.dataTransfer?.files?.[0]; if (file) handleFile(file); }; const onDragOver = (e) => { e.preventDefault(); if (!disabled) setDragActive(true); }; const onDragLeave = (e) => { e.preventDefault(); setDragActive(false); }; const onChange = (e) => { const file = e.target.files?.[0]; if (file) handleFile(file); e.target.value = ""; }; return (
{dragActive ? "Drop to upload" : "Drag & drop an image"}
or click to browse — JPG, PNG, WEBP · max 10 MB