Blox/
File Upload
A drag-and-drop file upload component with progress tracking, file type icons, and upload status indicators. Perfect for document management and media uploads.
Drag & DropProgress BarFile TypesMulti-file
Installation
Terminal
TSXReactTailwind
npx @uiblox/cli add file-uploadPreview
Drop files or click to upload
Images, documents up to 50MB
design-specs.pdf
2.3 MB
Done
screenshot.png
825.2 KB
67%
Use Cases
Document Management
Upload and organize documents, PDFs, spreadsheets with automatic file type detection.
Media Libraries
Bulk upload images and videos with thumbnail previews and progress tracking.
Form Attachments
Allow users to attach supporting documents to forms and applications.
Cloud Storage
Build file storage interfaces with upload queues and batch processing.
Source Code
file-upload.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200"use client"; import { useState, useCallback } from "react"; import { cn } from "@/lib/utils"; interface UploadedFile { id: string; name: string; size: number; type: string; progress: number; status: "uploading" | "complete" | "error"; } export function FileUpload() { const [files, setFiles] = useState<UploadedFile[]>([]); const [isDragging, setIsDragging] = useState(false); const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(true); }, []); const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); }, []); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); setIsDragging(false); const droppedFiles = Array.from(e.dataTransfer.files); handleFiles(droppedFiles); }, []); const handleFileInput = (e: React.ChangeEvent<HTMLInputElement>) => { if (e.target.files) { handleFiles(Array.from(e.target.files)); } }; const handleFiles = (newFiles: File[]) => { const uploadFiles: UploadedFile[] = newFiles.map((file) => ({ id: Math.random().toString(36).substr(2, 9), name: file.name, size: file.size, type: file.type, progress: 0, status: "uploading" as const, })); setFiles((prev) => [...prev, ...uploadFiles]); // Simulate upload progress uploadFiles.forEach((file) => { simulateUpload(file.id); }); }; const simulateUpload = (fileId: string) => { let progress = 0; const interval = setInterval(() => { progress += Math.random() * 30; if (progress >= 100) { progress = 100; clearInterval(interval); setFiles((prev) => prev.map((f) => (f.id === fileId ? { ...f, progress: 100, status: "complete" } : f)) ); } else { setFiles((prev) => prev.map((f) => (f.id === fileId ? { ...f, progress } : f)) ); } }, 500); }; const removeFile = (fileId: string) => { setFiles((prev) => prev.filter((f) => f.id !== fileId)); }; const formatFileSize = (bytes: number) => { if (bytes === 0) return "0 Bytes"; const k = 1024; const sizes = ["Bytes", "KB", "MB", "GB"]; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; }; return ( <div className="max-w-2xl mx-auto p-6"> {/* Drop Zone */} <div onDragOver={handleDragOver} onDragLeave={handleDragLeave} onDrop={handleDrop} className={cn( "relative border-2 border-dashed rounded-2xl p-12 text-center transition-all duration-300", isDragging ? "border-purple-500 bg-purple-50 dark:bg-purple-900/20" : "border-slate-300 dark:border-slate-700 hover:border-purple-400 dark:hover:border-purple-600" )} > <input type="file" multiple onChange={handleFileInput} className="absolute inset-0 w-full h-full opacity-0 cursor-pointer" /> <div className="space-y-4"> <div className="w-16 h-16 mx-auto bg-purple-100 dark:bg-purple-900/30 rounded-full flex items-center justify-center"> <UploadIcon className="w-8 h-8 text-purple-600" /> </div> <div> <p className="text-lg font-medium text-slate-900 dark:text-white"> Drop files here or click to upload </p> <p className="text-sm text-slate-500 mt-1"> Support for images, documents, and videos up to 50MB </p> </div> <button className="px-6 py-2.5 bg-purple-600 hover:bg-purple-700 text-white font-medium rounded-xl transition-colors"> Browse Files </button> </div> </div> {/* File List */} {files.length > 0 && ( <div className="mt-6 space-y-3"> <h3 className="text-sm font-semibold text-slate-900 dark:text-white"> Uploaded Files({files.length}) </h3> {files.map((file) => ( <div key={file.id} className="flex items-center gap-4 p-4 bg-white dark:bg-slate-900 rounded-xl border border-slate-200 dark:border-slate-800" > <div className="w-10 h-10 bg-slate-100 dark:bg-slate-800 rounded-lg flex items-center justify-center"> <FileIcon type={file.type} /> </div> <div className="flex-1 min-w-0"> <p className="text-sm font-medium text-slate-900 dark:text-white truncate"> {file.name} </p> <p className="text-xs text-slate-500">{formatFileSize(file.size)}</p> {file.status === "uploading" && ( <div className="mt-2 h-1.5 bg-slate-200 dark:bg-slate-700 rounded-full overflow-hidden"> <div className="h-full bg-purple-600 rounded-full transition-all duration-300" style={{ width: `${file.progress}%` }} /> </div> )} </div> <div className="flex items-center gap-2"> {file.status === "complete" && ( <span className="flex items-center gap-1 text-emerald-600 text-sm"> <CheckIcon /> Done </span> )} {file.status === "uploading" && ( <span className="text-sm text-slate-500">{Math.round(file.progress)}%</span> )} <button onClick={() => removeFile(file.id)} className="p-1.5 text-slate-400 hover:text-red-500 hover:bg-red-50 dark:hover:bg-red-900/20 rounded-lg transition-colors" > <XIcon /> </button> </div> </div> ))} </div> )} </div> ); } function UploadIcon({ className }: { className?: string }) { return <svg className={className} fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12" /></svg>; } function FileIcon({ type }: { type: string }) { if (type.startsWith("image/")) { return <svg className="w-5 h-5 text-purple-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" /></svg>; } if (type.includes("pdf")) { return <svg className="w-5 h-5 text-red-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z" /></svg>; } return <svg className="w-5 h-5 text-slate-500" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" /></svg>; } function CheckIcon() { return <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" /></svg>; } function XIcon() { return <svg className="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" /></svg>; }