Options Reference
All configuration options for the useDropup hook.
Validation Options
accept
File types to accept.
// Single MIME type
useDropup({ accept: 'image/png' });
// Multiple MIME types
useDropup({ accept: 'image/png, image/jpeg, image/gif' });
// Wildcard
useDropup({ accept: 'image/*' });
// By extension
useDropup({ accept: '.pdf, .doc, .docx' });
// Array format
useDropup({ accept: ['image/*', 'application/pdf', '.txt'] });
| Type | Default |
|---|---|
string | string[] | undefined (all files) |
maxSize
Maximum file size in bytes.
useDropup({ maxSize: 10 * 1024 * 1024 }); // 10MB
| Type | Default |
|---|---|
number | undefined (no limit) |
minSize
Minimum file size in bytes.
useDropup({ minSize: 1024 }); // 1KB minimum
| Type | Default |
|---|---|
number | undefined (no limit) |
maxFiles
Maximum number of files allowed.
useDropup({ maxFiles: 5 });
| Type | Default |
|---|---|
number | undefined (no limit) |
maxWidth / maxHeight
Maximum image dimensions in pixels.
useDropup({
accept: 'image/*',
maxWidth: 4096,
maxHeight: 4096,
});
| Type | Default |
|---|---|
number | undefined (no limit) |
minWidth / minHeight
Minimum image dimensions in pixels.
useDropup({
accept: 'image/*',
minWidth: 100,
minHeight: 100,
});
| Type | Default |
|---|---|
number | undefined (no limit) |
customRules
Custom validation functions.
useDropup({
customRules: [
// Sync validation
(file) => {
if (file.name.includes('draft')) {
return 'Draft files not allowed';
}
return true;
},
// Async validation
async (file) => {
const exists = await checkServerDuplicate(file);
return exists ? 'File already exists' : true;
},
],
});
| Type | Default |
|---|---|
ValidationRule[] | [] |
ValidationRule type:
type ValidationRule = (file: File) => boolean | string | Promise<boolean | string>;
Behavior Options
multiple
Allow multiple file selection.
useDropup({ multiple: true }); // Multiple files
useDropup({ multiple: false }); // Single file only
| Type | Default |
|---|---|
boolean | true |
disabled
Disable the dropzone.
useDropup({ disabled: true });
| Type | Default |
|---|---|
boolean | false |
autoUpload
Automatically start upload when files are added.
useDropup({
upload: { url: '/api/upload' },
autoUpload: true,
});
| Type | Default |
|---|---|
boolean | false |
generatePreviews
Generate preview URLs for image files.
useDropup({ generatePreviews: true });
// Access preview
files[0].preview // "blob:http://..."
| Type | Default |
|---|---|
boolean | true |
Upload Configuration
upload
Configure the upload destination and behavior.
URL-based Configuration
useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': 'Bearer token',
},
fieldName: 'file',
withCredentials: true,
timeout: 30000,
},
});
UploadConfig properties:
| Property | Type | Default | Description |
|---|---|---|---|
url | string | Required | Upload endpoint URL |
method | string | 'POST' | HTTP method |
headers | Record<string, string> | {} | Request headers |
fieldName | string | 'file' | Form field name for file |
withCredentials | boolean | false | Include cookies |
timeout | number | 0 | Request timeout (ms) |
data | Record<string, unknown> | {} | Additional form data |
Custom Uploader
useDropup({
upload: async (file, options) => {
const formData = new FormData();
formData.append('file', file.file);
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
signal: options.signal,
});
const data = await response.json();
return { url: data.url };
},
});
CustomUploader type:
type CustomUploader = (
file: DropupFile,
options: UploadOptions
) => Promise<UploadResult>;
interface UploadOptions {
signal: AbortSignal;
onProgress: (progress: number) => void;
}
interface UploadResult {
url?: string;
response?: unknown;
}
Cloud Uploaders
import { createS3Uploader } from '@samithahansaka/dropup/cloud/s3';
useDropup({
upload: createS3Uploader({
getPresignedUrl: async (file) => {
const res = await fetch('/api/s3-presign', {
method: 'POST',
body: JSON.stringify({ filename: file.name, type: file.type }),
});
return res.json();
},
}),
});
Callback Options
onFilesAdded
Called when files are added (after validation).
useDropup({
onFilesAdded: (files) => {
console.log('Added:', files.map(f => f.name));
},
});
| Type |
|---|
(files: DropupFile[]) => void |
onFileRemoved
Called when a file is removed.
useDropup({
onFileRemoved: (file) => {
console.log('Removed:', file.name);
},
});
| Type |
|---|
(file: DropupFile) => void |
onValidationError
Called when files fail validation.
useDropup({
accept: 'image/*',
onValidationError: (errors) => {
errors.forEach(({ file, errors }) => {
console.log(`${file.name}: ${errors.join(', ')}`);
});
},
});
| Type |
|---|
(errors: ValidationError[]) => void |
onUploadStart
Called when a file starts uploading.
useDropup({
onUploadStart: (file) => {
console.log('Starting upload:', file.name);
},
});
| Type |
|---|
(file: DropupFile) => void |
onUploadProgress
Called during upload progress.
useDropup({
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
});
| Type |
|---|
(file: DropupFile, progress: number) => void |
onUploadComplete
Called when a file upload completes.
useDropup({
onUploadComplete: (file, response) => {
console.log('Uploaded:', file.uploadedUrl);
console.log('Server response:', response);
},
});
| Type |
|---|
(file: DropupFile, response: unknown) => void |
onUploadError
Called when a file upload fails.
useDropup({
onUploadError: (file, error) => {
console.error(`Failed: ${file.name}`, error.message);
},
});
| Type |
|---|
(file: DropupFile, error: DropupError) => void |
onAllComplete
Called when all files finish uploading.
useDropup({
onAllComplete: (files) => {
const successful = files.filter(f => f.status === 'complete');
console.log(`${successful.length}/${files.length} uploaded successfully`);
},
});
| Type |
|---|
(files: DropupFile[]) => void |
Complete Example
const { files, actions, state, getDropProps, getInputProps } = useDropup({
// Validation
accept: ['image/*', 'application/pdf'],
maxSize: 10 * 1024 * 1024,
maxFiles: 5,
// Behavior
multiple: true,
autoUpload: false,
generatePreviews: true,
// Upload
upload: {
url: '/api/upload',
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
},
},
// Callbacks
onFilesAdded: (files) => {
toast.info(`${files.length} file(s) added`);
},
onValidationError: (errors) => {
toast.error(`${errors.length} file(s) rejected`);
},
onUploadProgress: (file, progress) => {
console.log(`${file.name}: ${progress}%`);
},
onUploadComplete: (file) => {
toast.success(`${file.name} uploaded!`);
},
onUploadError: (file, error) => {
toast.error(`${file.name} failed: ${error.message}`);
},
onAllComplete: (files) => {
const count = files.filter(f => f.status === 'complete').length;
toast.success(`All done! ${count} files uploaded.`);
},
});