Skip to Content
ohooks

useDropZone

Handles file drag-and-drop interactions with MIME/extension filtering via a target ref or getRootProps.

Loading preview...

The useDropZone hook manages drag-and-drop file uploads, tracks nested drag-enter depth so isOver does not flicker over child elements, and filters dropped files by MIME type or file extension.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useDropZone.json

Usage

Examples

Drop Zone with getRootProps

Spread getRootProps() onto your container element when you do not already have a separate RefObject.

Attaching to an Existing RefObject

Pass target in the options object to attach drag listeners directly to an existing element ref.

API Reference

UseDropZoneOptions

Configuration options and event callbacks for the drop zone.

Property
Specification
accept?
union
Accepted MIME types or file extensions (e.g., 'image/*,.pdf' or ['image/png', '.jpg'])
multiple?
uniondefault:true
Allow dropping multiple files at once
disabled?
uniondefault:false
Disable drag-and-drop handling
preventDefaultForUnhandled?
uniondefault:false
Prevent default browser behavior for drops outside the drop zone
onDrop?
function
Callback invoked when valid files are dropped
onDropRejected?
function
Callback invoked when dropped files fail MIME/extension or multiple validation
onDragEnter?
function
Callback invoked when a dragged item enters the drop zone
onDragOver?
function
Callback invoked while a dragged item moves over the drop zone
onDragLeave?
function
Callback invoked when a dragged item leaves the drop zone

DropZoneRootProps

Drag event handlers returned by getRootProps().

Property
Specification
onDragEnterrequired
function
Drag enter handler to attach to the drop zone element
onDragOverrequired
function
Drag over handler to attach to the drop zone element
onDragLeaverequired
function
Drag leave handler to attach to the drop zone element
onDroprequired
function
Drop handler to attach to the drop zone element
children?
ReactNode
dangerouslySetInnerHTML?
object
onCopy?
ClipboardEventHandler<object>
onCopyCapture?
ClipboardEventHandler<object>
onCut?
ClipboardEventHandler<object>
onCutCapture?
ClipboardEventHandler<object>
onPaste?
ClipboardEventHandler<object>
onPasteCapture?
ClipboardEventHandler<object>
onCompositionEnd?
CompositionEventHandler<object>
onCompositionEndCapture?
CompositionEventHandler<object>
onCompositionStart?
CompositionEventHandler<object>
onCompositionStartCapture?
CompositionEventHandler<object>
onCompositionUpdate?
CompositionEventHandler<object>
onCompositionUpdateCapture?
CompositionEventHandler<object>
onFocus?
FocusEventHandler<object>
onFocusCapture?
FocusEventHandler<object>
onBlur?
FocusEventHandler<object>
onBlurCapture?
FocusEventHandler<object>
onChange?
ChangeEventHandler<object, object>
onChangeCapture?
ChangeEventHandler<object, object>
onBeforeInput?
InputEventHandler<object>
onBeforeInputCapture?
InputEventHandler<object>
onInput?
InputEventHandler<object>
onInputCapture?
InputEventHandler<object>
onReset?
ReactEventHandler<object>
onResetCapture?
ReactEventHandler<object>
onSubmit?
SubmitEventHandler<object>
onSubmitCapture?
SubmitEventHandler<object>
onInvalid?
ReactEventHandler<object>
onInvalidCapture?
ReactEventHandler<object>
onLoad?
ReactEventHandler<object>
onLoadCapture?
ReactEventHandler<object>
onError?
ReactEventHandler<object>
onErrorCapture?
ReactEventHandler<object>
onKeyDown?
KeyboardEventHandler<object>
onKeyDownCapture?
KeyboardEventHandler<object>
onKeyPress?deprecated
KeyboardEventHandler<object>
onKeyPressCapture?deprecated
KeyboardEventHandler<object>
onKeyUp?
KeyboardEventHandler<object>
onKeyUpCapture?
KeyboardEventHandler<object>
onAbort?
ReactEventHandler<object>
onAbortCapture?
ReactEventHandler<object>
onCanPlay?
ReactEventHandler<object>
onCanPlayCapture?
ReactEventHandler<object>
onCanPlayThrough?
ReactEventHandler<object>
onCanPlayThroughCapture?
ReactEventHandler<object>
onDurationChange?
ReactEventHandler<object>
onDurationChangeCapture?
ReactEventHandler<object>
onEmptied?
ReactEventHandler<object>
onEmptiedCapture?
ReactEventHandler<object>
onEncrypted?
ReactEventHandler<object>
onEncryptedCapture?
ReactEventHandler<object>
onEnded?
ReactEventHandler<object>
onEndedCapture?
ReactEventHandler<object>
onLoadedData?
ReactEventHandler<object>
onLoadedDataCapture?
ReactEventHandler<object>
onLoadedMetadata?
ReactEventHandler<object>
onLoadedMetadataCapture?
ReactEventHandler<object>
onLoadStart?
ReactEventHandler<object>
onLoadStartCapture?
ReactEventHandler<object>
onPause?
ReactEventHandler<object>
onPauseCapture?
ReactEventHandler<object>
onPlay?
ReactEventHandler<object>
onPlayCapture?
ReactEventHandler<object>
onPlaying?
ReactEventHandler<object>
onPlayingCapture?
ReactEventHandler<object>
onProgress?
ReactEventHandler<object>
onProgressCapture?
ReactEventHandler<object>
onRateChange?
ReactEventHandler<object>
onRateChangeCapture?
ReactEventHandler<object>
onSeeked?
ReactEventHandler<object>
onSeekedCapture?
ReactEventHandler<object>
onSeeking?
ReactEventHandler<object>
onSeekingCapture?
ReactEventHandler<object>
onStalled?
ReactEventHandler<object>
onStalledCapture?
ReactEventHandler<object>
onSuspend?
ReactEventHandler<object>
onSuspendCapture?
ReactEventHandler<object>
onTimeUpdate?
ReactEventHandler<object>
onTimeUpdateCapture?
ReactEventHandler<object>
onVolumeChange?
ReactEventHandler<object>
onVolumeChangeCapture?
ReactEventHandler<object>
onWaiting?
ReactEventHandler<object>
onWaitingCapture?
ReactEventHandler<object>
onAuxClick?
MouseEventHandler<object>
onAuxClickCapture?
MouseEventHandler<object>
onClick?
MouseEventHandler<object>
onClickCapture?
MouseEventHandler<object>
onContextMenu?
MouseEventHandler<object>
onContextMenuCapture?
MouseEventHandler<object>
onDoubleClick?
MouseEventHandler<object>
onDoubleClickCapture?
MouseEventHandler<object>
onDrag?
DragEventHandler<object>
onDragCapture?
DragEventHandler<object>
onDragEnd?
DragEventHandler<object>
onDragEndCapture?
DragEventHandler<object>
onDragEnterCapture?
DragEventHandler<object>
onDragExit?
DragEventHandler<object>
onDragExitCapture?
DragEventHandler<object>
onDragLeaveCapture?
DragEventHandler<object>
onDragOverCapture?
DragEventHandler<object>
onDragStart?
DragEventHandler<object>
onDragStartCapture?
DragEventHandler<object>
onDropCapture?
DragEventHandler<object>
onMouseDown?
MouseEventHandler<object>
onMouseDownCapture?
MouseEventHandler<object>
onMouseEnter?
MouseEventHandler<object>
onMouseLeave?
MouseEventHandler<object>
onMouseMove?
MouseEventHandler<object>
onMouseMoveCapture?
MouseEventHandler<object>
onMouseOut?
MouseEventHandler<object>
onMouseOutCapture?
MouseEventHandler<object>
onMouseOver?
MouseEventHandler<object>
onMouseOverCapture?
MouseEventHandler<object>
onMouseUp?
MouseEventHandler<object>
onMouseUpCapture?
MouseEventHandler<object>
onSelect?
ReactEventHandler<object>
onSelectCapture?
ReactEventHandler<object>
onTouchCancel?
TouchEventHandler<object>
onTouchCancelCapture?
TouchEventHandler<object>
onTouchEnd?
TouchEventHandler<object>
onTouchEndCapture?
TouchEventHandler<object>
onTouchMove?
TouchEventHandler<object>
onTouchMoveCapture?
TouchEventHandler<object>
onTouchStart?
TouchEventHandler<object>
onTouchStartCapture?
TouchEventHandler<object>
onPointerDown?
PointerEventHandler<object>
onPointerDownCapture?
PointerEventHandler<object>
onPointerMove?
PointerEventHandler<object>
onPointerMoveCapture?
PointerEventHandler<object>
onPointerUp?
PointerEventHandler<object>
onPointerUpCapture?
PointerEventHandler<object>
onPointerCancel?
PointerEventHandler<object>
onPointerCancelCapture?
PointerEventHandler<object>
onPointerEnter?
PointerEventHandler<object>
onPointerLeave?
PointerEventHandler<object>
onPointerOver?
PointerEventHandler<object>
onPointerOverCapture?
PointerEventHandler<object>
onPointerOut?
PointerEventHandler<object>
onPointerOutCapture?
PointerEventHandler<object>
onGotPointerCapture?
PointerEventHandler<object>
onGotPointerCaptureCapture?
PointerEventHandler<object>
onLostPointerCapture?
PointerEventHandler<object>
onLostPointerCaptureCapture?
PointerEventHandler<object>
onScroll?
UIEventHandler<object>
onScrollCapture?
UIEventHandler<object>
onScrollEnd?
UIEventHandler<object>
onScrollEndCapture?
UIEventHandler<object>
onWheel?
WheelEventHandler<object>
onWheelCapture?
WheelEventHandler<object>
onAnimationStart?
AnimationEventHandler<object>
onAnimationStartCapture?
AnimationEventHandler<object>
onAnimationEnd?
AnimationEventHandler<object>
onAnimationEndCapture?
AnimationEventHandler<object>
onAnimationIteration?
AnimationEventHandler<object>
onAnimationIterationCapture?
AnimationEventHandler<object>
onToggle?
ToggleEventHandler<object>
onBeforeToggle?
ToggleEventHandler<object>
onTransitionCancel?
TransitionEventHandler<object>
onTransitionCancelCapture?
TransitionEventHandler<object>
onTransitionEnd?
TransitionEventHandler<object>
onTransitionEndCapture?
TransitionEventHandler<object>
onTransitionRun?
TransitionEventHandler<object>
onTransitionRunCapture?
TransitionEventHandler<object>
onTransitionStart?
TransitionEventHandler<object>
onTransitionStartCapture?
TransitionEventHandler<object>
aria-activedescendant?
string
Identifies the currently active element when DOM focus is on a composite widget, textbox, group, or application.
aria-atomic?
union
Indicates whether assistive technologies will present all, or only parts of, the changed region based on the change notifications defined by the aria-relevant attribute.
aria-autocomplete?
union
Indicates whether inputting text could trigger display of one or more predictions of the user's intended value for an input and specifies how predictions would be presented if they are made.
aria-braillelabel?
string
Defines a string value that labels the current element, which is intended to be converted into Braille.
aria-brailleroledescription?
string
Defines a human-readable, author-localized abbreviated description for the role of an element, which is intended to be converted into Braille.
aria-busy?
union
aria-checked?
union
Indicates the current "checked" state of checkboxes, radio buttons, and other widgets.
aria-colcount?
number
Defines the total number of columns in a table, grid, or treegrid.
aria-colindex?
number
Defines an element's column index or position with respect to the total number of columns within a table, grid, or treegrid.
aria-colindextext?
string
Defines a human readable text alternative of aria-colindex.
aria-colspan?
number
Defines the number of columns spanned by a cell or gridcell within a table, grid, or treegrid.
aria-controls?
string
Identifies the element (or elements) whose contents or presence are controlled by the current element.
aria-current?
union
Indicates the element that represents the current item within a container or set of related elements.
aria-describedby?
string
Identifies the element (or elements) that describes the object.
aria-description?
string
Defines a string value that describes or annotates the current element.
aria-details?
string
Identifies the element that provides a detailed, extended description for the object.
aria-disabled?
union
Indicates that the element is perceivable but disabled, so it is not editable or otherwise operable.
aria-dropeffect?deprecated
union
Indicates what functions can be performed when a dragged object is released on the drop target.
aria-errormessage?
string
Identifies the element that provides an error message for the object.
aria-expanded?
union
Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed.
aria-flowto?
string
Identifies the next element (or elements) in an alternate reading order of content which, at the user's discretion, allows assistive technology to override the general default of reading in document source order.
aria-grabbed?deprecated
union
Indicates an element's "grabbed" state in a drag-and-drop operation.
aria-haspopup?
union
Indicates the availability and type of interactive popup element, such as menu or dialog, that can be triggered by an element.
aria-hidden?
union
Indicates whether the element is exposed to an accessibility API.
aria-invalid?
union
Indicates the entered value does not conform to the format expected by the application.
aria-keyshortcuts?
string
Indicates keyboard shortcuts that an author has implemented to activate or give focus to an element.
aria-label?
string
Defines a string value that labels the current element.
aria-labelledby?
string
Identifies the element (or elements) that labels the current element.
aria-level?
number
Defines the hierarchical level of an element within a structure.
aria-live?
union
Indicates that an element will be updated, and describes the types of updates the user agents, assistive technologies, and user can expect from the live region.
aria-modal?
union
Indicates whether an element is modal when displayed.
aria-multiline?
union
Indicates whether a text box accepts multiple lines of input or only a single line.
aria-multiselectable?
union
Indicates that the user may select more than one item from the current selectable descendants.
aria-orientation?
union
Indicates whether the element's orientation is horizontal, vertical, or unknown/ambiguous.
aria-owns?
string
Identifies an element (or elements) in order to define a visual, functional, or contextual parent/child relationship between DOM elements where the DOM hierarchy cannot be used to represent the relationship.
aria-placeholder?
string
Defines a short hint (a word or short phrase) intended to aid the user with data entry when the control has no value. A hint could be a sample value or a brief description of the expected format.
aria-posinset?
number
Defines an element's number or position in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM.
aria-pressed?
union
Indicates the current "pressed" state of toggle buttons.
aria-readonly?
union
Indicates that the element is not editable, but is otherwise operable.
aria-relevant?
union
Indicates what notifications the user agent will trigger when the accessibility tree within a live region is modified.
aria-required?
union
Indicates that user input is required on the element before a form may be submitted.
aria-roledescription?
string
Defines a human-readable, author-localized description for the role of an element.
aria-rowcount?
number
Defines the total number of rows in a table, grid, or treegrid.
aria-rowindex?
number
Defines an element's row index or position with respect to the total number of rows within a table, grid, or treegrid.
aria-rowindextext?
string
Defines a human readable text alternative of aria-rowindex.
aria-rowspan?
number
Defines the number of rows spanned by a cell or gridcell within a table, grid, or treegrid.
aria-selected?
union
Indicates the current "selected" state of various widgets.
aria-setsize?
number
Defines the number of items in the current set of listitems or treeitems. Not required if all elements in the set are present in the DOM.
aria-sort?
union
Indicates if items in a table or grid are sorted in ascending or descending order.
aria-valuemax?
number
Defines the maximum allowed value for a range widget.
aria-valuemin?
number
Defines the minimum allowed value for a range widget.
aria-valuenow?
number
Defines the current value for a range widget.
aria-valuetext?
string
Defines the human readable text alternative of aria-valuenow for a range widget.
defaultChecked?
union
defaultValue?
union
suppressContentEditableWarning?
union
suppressHydrationWarning?
union
accessKey?
string
autoCapitalize?
union
autoFocus?
union
className?
string
contentEditable?
union
contextMenu?
string
dir?
string
draggable?
union
enterKeyHint?
union
hidden?
union
id?
string
lang?
string
nonce?
string
slot?
string
spellCheck?
union
style?
object
tabIndex?
number
title?
string
translate?
union
radioGroup?
string
role?
union
about?
string
content?
string
datatype?
string
inlist?
any
prefix?
string
property?
string
rel?
string
resource?
string
rev?
string
typeof?
string
vocab?
string
autoCorrect?
string
autoSave?
string
color?
string
itemProp?
string
itemScope?
union
itemType?
string
itemID?
string
itemRef?
string
results?
number
security?
string
unselectable?
union
popover?
union
popoverTargetAction?
union
popoverTarget?
string
inert?
union
inputMode?
union
Hints at the type of data that might be entered by the user while editing the element or its contents
is?
string
Specify that a standard HTML element should behave like a defined custom built-in element
exportparts?
string
part?
string

UseDropZoneReturn

State and helper functions returned by the hook.

Property
Specification
isOver
union
Whether a dragged item is currently hovering over the drop zone
isInvalid
union
Whether the currently dragged item violates accept or multiple rules
files
array
Array of files from the most recent valid drop
clearFiles
function
Clears the files state array
getRootProps
function
Prop getter for binding drag-and-drop handlers directly onto a JSX element