smart-dropzone-react
Version:
๐ A production-ready React dropzone component with smart defaults, drag & drop reordering, chunked uploads, resume functionality, and comprehensive provider support (Cloudinary, AWS S3, Supabase)
485 lines (386 loc) โข 12.1 kB
Markdown
# ๐ Smart Dropzone
[](https://badge.fury.io/js/smart-dropzone-react)
[](https://opensource.org/licenses/MIT)
[](https://www.typescriptlang.org/)
[](https://reactjs.org/)
[](./coverage)
A **production-ready** React dropzone component with smart defaults, advanced features, and comprehensive provider support. Built for modern applications that need reliable file uploads with excellent user experience.
## โจ **Key Features**
- ๐ฏ **Smart Defaults** - Zero configuration to get started, extensive customization available
- ๐ **Chunked Uploads** - Handle large files with resume capability and progress tracking
- ๐จ **Drag & Drop Reordering** - Intuitive file reordering with visual feedback
- ๐ **Multi-Provider Support** - Cloudinary, AWS S3, Supabase, and extensible architecture
- โฟ **Accessibility First** - WCAG compliant with screen reader support and keyboard navigation
- ๐ **Internationalization** - Built-in i18n support with multiple languages
- ๐ฑ **Responsive Design** - Works seamlessly across desktop and mobile devices
- ๐ **Performance Optimized** - Lazy loading, tree shaking, and minimal bundle impact
- ๐ก๏ธ **Type Safe** - Full TypeScript support with comprehensive type definitions
- ๐ **Production Ready** - 83.7% test coverage with extensive edge case handling
## ๐ฆ **Installation**
```bash
# npm
npm install smart-dropzone-react
# yarn
yarn add smart-dropzone-react
# pnpm
pnpm add smart-dropzone-react
```
### Peer Dependencies
```bash
npm install react react-dom
```
**React Version Support:**
- โ
React 18.x (Full support)
- โ
React 19.x (Full support with latest features)
- ๐ React 17.x (Legacy support - may have compatibility issues)
## ๐ **Quick Start**
### Zero Configuration (Cloudinary)
```tsx
import { SimpleUpload } from "@tanflare/smart-dropzone";
function App() {
return (
<SimpleUpload
cloudinary={{
cloudName: "your-cloud-name",
uploadPreset: "your-upload-preset",
}}
onFilesAdded={(files) => console.log("Files uploaded:", files)}
/>
);
}
```
### With Smart Presets
```tsx
import { SmartDropzoneSimple } from "smart-dropzone-react";
// Gallery preset - optimized for images
function ImageGallery() {
return (
<SmartDropzoneSimple
preset="gallery"
cloudinary={{
cloudName: "your-cloud-name",
uploadPreset: "your-upload-preset",
}}
/>
);
}
// Document preset - optimized for files
function DocumentUpload() {
return (
<SmartDropzoneSimple
preset="documents"
cloudinary={{
cloudName: "your-cloud-name",
uploadPreset: "your-upload-preset",
}}
/>
);
}
```
### Advanced Configuration
```tsx
import { SmartDropzone, CloudinaryProvider } from "smart-dropzone-react";
function AdvancedUpload() {
const provider = new CloudinaryProvider({
cloudName: "your-cloud-name",
uploadPreset: "your-upload-preset",
defaultFolder: "uploads",
});
return (
<SmartDropzone
provider={provider}
maxFiles={10}
maxFileSize={50 * 1024 * 1024} // 50MB
allowedTypes={["image/*", "application/pdf"]}
enableReorder={true}
enableResume={true}
showPreview={true}
theme="dark"
onFilesAdded={(files) => console.log("Files uploaded:", files)}
onError={(error) => console.error("Upload error:", error)}
onProgress={(fileId, progress) =>
console.log(`File ${fileId}: ${progress}%`)
}
/>
);
}
```
## ๐จ **Available Presets**
| Preset | Use Case | Max Files | File Types | Features |
| ------------ | ------------------- | --------- | ------------------------------- | ---------------------------- |
| `simple` | Basic uploads | 5 | `image/*`, `application/pdf` | Preview, Progress |
| `gallery` | Image galleries | 20 | `image/*` | Preview, Reorder, Thumbnails |
| `documents` | Document management | 10 | `application/*`, `text/*` | File info, Validation |
| `media` | Media content | 15 | `image/*`, `video/*`, `audio/*` | Preview, Player |
| `enterprise` | Large-scale uploads | 50 | All types | All features, Analytics |
## ๐ **Provider Support**
### Cloudinary
```tsx
import { CloudinaryProvider } from "smart-dropzone-react";
const provider = new CloudinaryProvider({
cloudName: "your-cloud-name",
uploadPreset: "your-upload-preset",
defaultFolder: "uploads",
// Optional: Add transformations
transformations: {
quality: "auto",
fetch_format: "auto",
},
});
```
### AWS S3 (Coming Soon)
```tsx
import { S3Provider } from "smart-dropzone-react";
const provider = new S3Provider({
accessKeyId: "your-access-key",
secretAccessKey: "your-secret-key",
bucket: "your-bucket-name",
region: "us-east-1",
});
```
### Custom Provider
```tsx
import { BaseProvider } from "smart-dropzone-react";
class CustomProvider extends BaseProvider {
async uploadFile(file: File, options?: UploadOptions) {
// Your custom upload logic
return {
id: "file-id",
url: "https://your-cdn.com/file.jpg",
uploadedAt: new Date(),
};
}
// Implement other required methods...
}
```
## ๐ฏ **Real-World Examples**
### E-commerce Product Images
```tsx
import { GalleryUpload } from "smart-dropzone-react";
function ProductImageUpload({ productId }) {
return (
<GalleryUpload
cloudinary={{
cloudName: "your-cloud-name",
uploadPreset: "product-images",
defaultFolder: `products/${productId}`,
}}
maxFiles={8}
onFilesAdded={(files) => {
// Update product images in your database
updateProductImages(productId, files);
}}
overrides={{
showFileSize: true,
enableReorder: true,
theme: "light",
}}
/>
);
}
```
### User Profile Avatar
```tsx
import { SimpleUpload } from "@tanflare/smart-dropzone";
function AvatarUpload({ userId }) {
return (
<div className="w-32 h-32">
<SimpleUpload
cloudinary={{
cloudName: "your-cloud-name",
uploadPreset: "avatars",
defaultFolder: "users/avatars",
}}
maxFiles={1}
allowedTypes={["image/*"]}
onFilesAdded={([file]) => {
// Update user avatar
updateUserAvatar(userId, file.url);
}}
overrides={{
variant: "filled",
size: "sm",
}}
/>
</div>
);
}
```
### Document Management
```tsx
import { DocumentUpload } from "smart-dropzone-react";
function DocumentManager() {
const [documents, setDocuments] = useState([]);
return (
<DocumentUpload
cloudinary={{
cloudName: "your-cloud-name",
uploadPreset: "documents",
}}
onFilesAdded={(newFiles) => {
setDocuments((prev) => [...prev, ...newFiles]);
// Send to your document management system
indexDocuments(newFiles);
}}
overrides={{
maxFileSize: 100 * 1024 * 1024, // 100MB
showFileType: true,
showFileSize: true,
}}
/>
);
}
```
## ๐ ๏ธ **Advanced Features**
### Chunked Uploads with Resume
```tsx
<SmartDropzone
provider={cloudinaryProvider}
enableResume={true}
chunkSize={1024 * 1024} // 1MB chunks
onChunkProgress={(fileId, chunkIndex, totalChunks) => {
console.log(`File ${fileId}: chunk ${chunkIndex}/${totalChunks}`);
}}
onResumeUpload={(fileId) => {
console.log(`Resuming upload for ${fileId}`);
}}
/>
```
### Accessibility & Internationalization
```tsx
<SmartDropzone
provider={cloudinaryProvider}
accessibility={{
highContrast: true,
reducedMotion: false,
screenReaderSupport: true,
}}
i18n={{
locale: "es",
customTranslations: {
"dropzone.dragActive": "Suelta los archivos aquรญ...",
},
}}
/>
```
### Performance Monitoring
```tsx
<SmartDropzone
provider={cloudinaryProvider}
enablePerformanceMonitoring={true}
onPerformanceData={(metrics) => {
console.log("Performance metrics:", {
renderTime: metrics.renderTime,
uploadSpeed: metrics.uploadSpeed,
memoryUsage: metrics.memoryUsage,
});
}}
/>
```
## ๐จ **Styling & Theming**
### Built-in Themes
```tsx
// Light theme (default)
<SmartDropzone theme="light" />
// Dark theme
<SmartDropzone theme="dark" />
// Custom theme
<SmartDropzone
theme={{
colors: {
primary: "#007bff",
secondary: "#6c757d",
success: "#28a745",
error: "#dc3545"
},
borderRadius: "8px",
spacing: "1rem"
}}
/>
```
### CSS Custom Properties
```css
:root {
--smart-dropzone-primary: #007bff;
--smart-dropzone-border-radius: 8px;
--smart-dropzone-transition: all 0.2s ease;
}
```
### Tailwind CSS Integration
```tsx
<SmartDropzone
className="border-dashed border-2 border-blue-300 hover:border-blue-500 rounded-lg p-8"
provider={provider}
/>
```
## ๐ **API Reference**
### Components
- **`SmartDropzone`** - Main component with full feature set
- **`SmartDropzoneSimple`** - Simplified component with preset support
- **`SimpleUpload`** - Zero-config component for basic uploads
- **`GalleryUpload`** - Optimized for image galleries
- **`DocumentUpload`** - Optimized for document management
- **`MediaUpload`** - Optimized for media files
- **`EnterpriseUpload`** - Full-featured for enterprise use
### Providers
- **`CloudinaryProvider`** - Cloudinary upload service
- **`S3Provider`** - Amazon S3 (coming soon)
- **`SupabaseProvider`** - Supabase storage (coming soon)
- **`BaseProvider`** - Base class for custom providers
### Hooks
- **`useUpload`** - Core upload management hook
- **`useFilePreview`** - File preview generation
- **`useDragReorder`** - Drag and drop reordering
- **`useAccessibility`** - Accessibility features
For complete API documentation, see [API.md](./docs/API.md).
## ๐งช **Testing**
The package includes comprehensive tests with 83.7% coverage:
```bash
# Run tests
npm run test
# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
# Run all tests (unit + e2e + type checking)
npm run test:all
```
## ๐ **Performance**
- **Bundle Size**: ~45KB gzipped (tree-shakeable)
- **First Paint**: <100ms for initial render
- **Upload Performance**: Optimized chunking for large files
- **Memory Usage**: Efficient cleanup and garbage collection
- **Browser Support**: Modern browsers (ES2020+)
## ๐ **Browser Support**
| Browser | Version |
| ------- | ------- |
| Chrome | โฅ88 |
| Firefox | โฅ85 |
| Safari | โฅ14 |
| Edge | โฅ88 |
## ๐ค **Contributing**
We welcome contributions! Please see our [Contributing Guide](./CONTRIBUTING.md) for details.
### Development Setup
```bash
# Clone the repository
git clone https://github.com/harishkhojare/smart-dropzone.git
# Install dependencies
pnpm install
# Start development
pnpm dev
# Run tests
pnpm test
# Build package
pnpm build
```
## ๐ **License**
MIT ยฉ [Tanflare Team](https://github.com/tanflare)
## ๐ **Support**
- ๐ [Documentation](https://github.com/tanflare/smart-dropzone/blob/main/README.md)
- ๐ [Issue Tracker](https://github.com/tanflare/smart-dropzone/issues)
- ๐ฌ [Discussions](https://github.com/tanflare/smart-dropzone/discussions)
- ๐ง [Email Support](mailto:support@tanflare.com)
## ๐ **Sponsors**
Special thanks to our sponsors and contributors who make this project possible!
---
**Made with โค๏ธ by the Tanflare Team**