useDrag
useDrag turns an element into a file drop zone. It tracks whether files are dragged over it and hands you the
dropped files that match accept. For list reordering or moving UI elements, use a dedicated library.Hooks
Signature
const { isDragging, events } = useDrag({ accept, single, onDragOver, onDragLeave, onDrop });Name | Type | Description |
|---|---|---|
accept | string[] | Accepted types, as in the HTML accept attribute: .pdf, image/*, text/csv. |
single | boolean | Delivers only the first accepted file. |
onDragOver | () => void | Runs when a drag enters the zone. |
onDragLeave | () => void | Runs when the drag leaves or ends. |
onDrop | (files: File[]) => void | Receives the accepted files. |
returns | { isDragging, events } | events holds onDragOver, onDragLeave, onDrop, onDragEnd; spread it. |
Scrolls sideways inside the table. The page does not.
Example
import { useDrag } from '@apollion-dsi/core/hooks';
function Dropzone({ onFiles }: { onFiles: (files: File[]) => void }) {
const { isDragging, events } = useDrag({ accept: ['image/*', '.pdf'], onDrop: onFiles });
return (
<div {...events} style={{ border: '2px dashed', borderColor: isDragging ? 'blue' : 'gray', padding: 24 }}>
Drop files here
</div>
);
}Pitfalls
- Without
accept, every dropped file is accepted. When no dropped file is accepted,onDropis not called. events.onDragEndis deprecated:dragendnever fires on a drop zone.- Files outside
acceptare dropped without notice;onDropmay receive an empty array. - A drop zone alone is not keyboard accessible. Pair it with a file input — see
useFileDialog.