073
LVL 03 — MID DEVELOPERSESSION 073DAY 73

DRAG & DROP UPLOAD

🎫 PIXELCRAFT-060
Feature | 🟡 Medium | Priority: 🟠 High

Users want to drag images directly from their desktop onto the PixelCraft canvas. No more clicking "Browse files." Build a visual dropzone with multi-file support.
CONCEPTS.UNLOCKED
🖱️
HTML Drag and Drop API
Native browser API for drag interactions. No libraries needed. The browser fires drag events when the user drags files from their OS into the browser window.
📡
dragover & drop Events
Two critical events. dragover fires continuously while dragging over an element (must preventDefault to allow drop). drop fires when the user releases — access the files here.
📦
DataTransfer
The payload of a drag event. event.dataTransfer.files gives you the dropped files. event.dataTransfer.items gives richer access — check types before accepting.
🛡️
File Validation on Drop
Check before you accept. Validate MIME type (image/png, image/jpeg), file size limits, and dimensions. Reject non-images with a clear error — don't silently fail.
Visual Dropzone
Show the user where to drop. Dashed border, background change, "Drop here" text — visual feedback on dragenter, reset on dragleave. The UX that makes it feel polished.
📁
FileReader API
Read dropped files in the browser. FileReader.readAsDataURL() converts the file to a base64 string you can display as a preview or load onto the canvas.
HANDS-ON.TASKS
01
Build the Dropzone Component
function DropZone({ onFilesAccepted }) { const [isDragging, setIsDragging] = useState(false); const handleDragOver = (e) => { e.preventDefault(); e.dataTransfer.dropEffect = 'copy'; setIsDragging(true); }; const handleDragLeave = (e) => { if (!e.currentTarget.contains( e.relatedTarget)) { setIsDragging(false); } }; const handleDrop = (e) => { e.preventDefault(); setIsDragging(false); const files = [...e.dataTransfer.files] .filter(f => f.type.startsWith('image/')); if (files.length === 0) { alert('Only image files accepted'); return; } onFilesAccepted(files); }; return ( <div className={`dropzone ${isDragging ? 'active' : ''}`} onDragOver={handleDragOver} onDragLeave={handleDragLeave} onDrop={handleDrop} > {isDragging ? '📸 Drop images here!' : 'Drag images here or click'} </div> ); }
02
File Validation & Preview
function validateAndPreview(files) { const MAX_SIZE = 10 * 1024 * 1024; // 10MB const ALLOWED = [ 'image/png', 'image/jpeg', 'image/webp', 'image/gif' ]; return files .filter(file => { if (!ALLOWED.includes(file.type)) { console.warn( `Rejected: ${file.name} — ` + `invalid type`); return false; } if (file.size > MAX_SIZE) { console.warn( `Rejected: ${file.name} — ` + `exceeds 10MB`); return false; } return true; }) .map(file => ({ file, previewUrl: URL.createObjectURL(file) })); }
03
Load Dropped Image onto Canvas
function loadImageToCanvas(file, canvas) { const ctx = canvas.getContext('2d'); const img = new Image(); img.onload = () => { canvas.width = img.width; canvas.height = img.height; ctx.drawImage(img, 0, 0); URL.revokeObjectURL(img.src); }; img.src = URL.createObjectURL(file); }
Always revoke object URLs after use to free memory. createObjectURL is fast but leaks if you forget to clean up.
04
Multi-File Drop with Thumbnails

Drop multiple images at once → show thumbnail previews in a strip below the canvas. Click a thumbnail → load that image onto the main canvas. Show file name and size under each thumbnail.

05
Close the Ticket
git switch -c feature/PIXELCRAFT-060-drag-drop git add src/ git commit -m "Add drag-and-drop image upload with validation (PIXELCRAFT-060)" git push origin feature/PIXELCRAFT-060-drag-drop # PR → Review → Merge → Close ticket ✅
CS.DEEP-DIVE

The browser is a rich application platform.

Drag and Drop is one of many native Web APIs that eliminate the need for external libraries.

// Native APIs you already have:

Drag and Drop API
  Dragging files, elements, data

File API + FileReader
  Read files entirely in the browser

URL.createObjectURL()
  Display files without uploading

Clipboard API
  Copy/paste images and text

Web Workers
  Heavy computation off main thread

// Before reaching for a library,
// check MDN. The platform is more
// capable than most devs realize.
"Drop Lab"
[A]Add paste support: Ctrl+V an image from clipboard onto the canvas. Use the Clipboard API (navigator.clipboard.read()) alongside drag-and-drop for maximum convenience.
[B]Add upload progress: when multiple large files are dropped, show a progress bar for each. Use XMLHttpRequest (not fetch) for upload progress events.
[C]Research: how do tools like Figma and Canva handle drag-and-drop? What about dragging layers within the canvas (sortable drag)? Explore dnd-kit library for complex interactions.
REF.MATERIAL
ARTICLE
MDN Web Docs
Complete reference: drag events, DataTransfer, drop zones, and drag effects. The definitive guide to native drag-and-drop.
DNDMDNESSENTIAL
ARTICLE
MDN Web Docs
FileReader, Blob URLs, and reading files in the browser. Everything you need to handle user-uploaded files client-side.
FILE APIMDNOFFICIAL
VIDEO
Web Dev Simplified
Building a drag-and-drop file uploader from scratch: dropzone styling, event handling, and preview generation.
DNDTUTORIAL
ARTICLE
Claudéric Demers
Modern drag-and-drop toolkit for React: sortable lists, drag handles, sensors, and accessibility-first design.
DND-KITREACT
VIDEO
Fireship
Client-side file handling, validation patterns, and upload strategies. The practical guide to production file uploads.
UPLOADQUICK
// LEAVE EXCITED BECAUSE
Drag a photo from your desktop → it appears instantly on the canvas. No file picker, no browse button — just drop and go. The app feels like a native desktop tool now.