Skip to main content

useDropup Hook

The main hook for file uploads with drag-and-drop support.

Import

import { useDropup } from '@samithahansaka/dropup';

Basic Usage

const {
files,
state,
actions,
getDropProps,
getInputProps,
openFileDialog,
} = useDropup(options);

Parameters

options (optional)

See Options for full details.

interface UseDropupOptions {
// Validation
accept?: string | string[];
maxSize?: number;
minSize?: number;
maxFiles?: number;
maxWidth?: number;
maxHeight?: number;
minWidth?: number;
minHeight?: number;
customRules?: ValidationRule[];

// Behavior
multiple?: boolean;
disabled?: boolean;
autoUpload?: boolean;
generatePreviews?: boolean;

// Upload configuration
upload?: UploadConfig | CustomUploader;

// Callbacks
onFilesAdded?: (files: DropupFile[]) => void;
onFileRemoved?: (file: DropupFile) => void;
onValidationError?: (errors: ValidationError[]) => void;
onUploadStart?: (file: DropupFile) => void;
onUploadProgress?: (file: DropupFile, progress: number) => void;
onUploadComplete?: (file: DropupFile, response: unknown) => void;
onUploadError?: (file: DropupFile, error: DropupError) => void;
onAllComplete?: (files: DropupFile[]) => void;
}

Return Value

See Return Values for full details.

interface UseDropupReturn {
// State
files: DropupFile[];
state: DropupState;

// Actions
actions: DropupActions;

// Prop getters
getDropProps: <E extends HTMLElement>(props?: HTMLAttributes<E>) => DropZoneProps<E>;
getInputProps: (props?: InputHTMLAttributes) => InputProps;

// Utilities
openFileDialog: () => void;
}

Quick Reference

Files Array

const { files } = useDropup();

// Each file has:
files[0].id // Unique ID
files[0].file // Original File object
files[0].name // File name
files[0].size // Size in bytes
files[0].type // MIME type
files[0].preview // Preview URL (images)
files[0].status // 'idle' | 'uploading' | 'complete' | 'error'
files[0].progress // 0-100
files[0].uploadedUrl // URL after upload
files[0].error // Error if failed

State Object

const { state } = useDropup();

state.isDragging // Files being dragged over
state.isDragAccept // Dragged files are valid
state.isDragReject // Dragged files are invalid
state.isUploading // Any file uploading
state.progress // Overall progress 0-100
state.status // 'idle' | 'uploading' | 'complete' | 'error'

Actions Object

const { actions } = useDropup();

actions.upload(fileIds?) // Start upload
actions.cancel(fileId?) // Cancel upload
actions.remove(fileId) // Remove file
actions.reset() // Clear all
actions.retry(fileIds?) // Retry failed
actions.addFiles(files) // Add files programmatically
actions.updateFileMeta(id, meta) // Update metadata

Prop Getters

const { getDropProps, getInputProps } = useDropup();

// Apply to your drop zone
<div {...getDropProps()}>
<input {...getInputProps()} />
Drop here
</div>

// With custom props
<div {...getDropProps({ className: 'my-dropzone', onClick: handleClick })}>
...
</div>

Examples

Minimal Example

function Uploader() {
const { files, getDropProps, getInputProps } = useDropup();

return (
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>Drop files here</p>
<ul>
{files.map(f => <li key={f.id}>{f.name}</li>)}
</ul>
</div>
);
}

With Upload

function Uploader() {
const { files, actions, getDropProps, getInputProps } = useDropup({
upload: {
url: '/api/upload',
method: 'POST',
headers: { 'Authorization': 'Bearer token' },
},
});

return (
<div>
<div {...getDropProps()}>
<input {...getInputProps()} />
<p>Drop files here</p>
</div>

{files.map(f => (
<div key={f.id}>
{f.name} - {f.status}
{f.status === 'uploading' && ` ${f.progress}%`}
</div>
))}

<button onClick={() => actions.upload()}>Upload All</button>
</div>
);
}

With All Options

function FullFeaturedUploader() {
const {
files,
state,
actions,
getDropProps,
getInputProps,
} = useDropup({
// Validation
accept: 'image/*',
maxSize: 10 * 1024 * 1024,
maxFiles: 5,

// Behavior
multiple: true,
autoUpload: true,
generatePreviews: true,

// Upload
upload: {
url: '/api/upload',
method: 'POST',
},

// Callbacks
onFilesAdded: (files) => console.log('Added:', files),
onUploadComplete: (file) => console.log('Done:', file.uploadedUrl),
onUploadError: (file, err) => console.error('Error:', err),
onAllComplete: () => console.log('All uploads complete!'),
});

return (
<div>
<div
{...getDropProps()}
style={{
border: `2px dashed ${state.isDragAccept ? 'green' : state.isDragReject ? 'red' : 'gray'}`,
padding: 40,
}}
>
<input {...getInputProps()} />
<p>Drop images here (max 5, 10MB each)</p>
</div>

{files.map(file => (
<div key={file.id}>
{file.preview && <img src={file.preview} width={50} />}
<span>{file.name}</span>
<span>{file.status}</span>
{file.status === 'uploading' && <span>{file.progress}%</span>}
<button onClick={() => actions.remove(file.id)}>×</button>
</div>
))}

{state.isUploading && <p>Uploading... {state.progress}%</p>}
</div>
);
}