Skip to content

useDropZone

Category
Export Size
1.13 kB
Last Changed
5 months ago

Create a zone where files can be dropped.

WARNING

Due to Safari browser limitations, file type validation is only possible during the drop event, not during drag events. As a result, the isOverDropZone value will always be true during drag operations in Safari, regardless of file type.

Demo

Drop files from your computer on to drop zones

General DropZone
isOverDropZone: false
Image DropZone
isOverDropZone: false

Usage

vue
<script setup lang="ts">
import { 
useDropZone
} from '@vueuse/core'
import {
ref
} from 'vue'
const
dropZoneRef
=
ref
<HTMLDivElement>()
function
onDrop
(
files
: File[] | null) {
// called when files are dropped on zone } const {
isOverDropZone
} =
useDropZone
(
dropZoneRef
, {
onDrop
,
// specify the types of data to be received.
dataTypes
: ['image/jpeg'],
// control multi-file drop
multiple
: true,
// whether to prevent default behavior for unhandled events
preventDefaultForUnhandled
: false,
}) </script> <template> <
div
ref
="
dropZoneRef
">
Drop files here </
div
>
</template>

Type Declarations

Show Type Declarations
ts
export interface UseDropZoneReturn {
  
files
:
ShallowRef
<File[] | null>
isOverDropZone
:
ShallowRef
<boolean>
} export interface UseDropZoneOptions { /** * Allowed data types, if not set, all data types are allowed. * Also can be a function to check the data types. */
dataTypes
?:
|
MaybeRef
<readonly string[]>
| ((
types
: readonly string[]) => boolean)
onDrop
?: (
files
: File[] | null,
event
: DragEvent) => void
onEnter
?: (
files
: File[] | null,
event
: DragEvent) => void
onLeave
?: (
files
: File[] | null,
event
: DragEvent) => void
onOver
?: (
files
: File[] | null,
event
: DragEvent) => void
/** * Allow multiple files to be dropped. Defaults to true. */
multiple
?: boolean
/** * Prevent default behavior for unhandled events. Defaults to false. */
preventDefaultForUnhandled
?: boolean
} export declare function
useDropZone
(
target
:
MaybeRefOrGetter
<HTMLElement | Document | null | undefined>,
options
?: UseDropZoneOptions | UseDropZoneOptions["onDrop"],
): UseDropZoneReturn

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
青椒肉丝
Ostap Brehin
Mazel
briankchan
Anthony Fu
danngossinga
babu-ch
Tycho
Diego Pascual
getname1997
Neo Fu
Luka Bagic
aki77
Matthias Stiller
Julien Martin

Changelog

v13.1.0 on
21fa6 - fix: Allow passing document as target (#4681)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
b01cf - fix: validate all file types individually when dropping multiple files (#4325)
v12.1.0 on
df363 - fix: update type to allow readonly array param (#4319)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.2.0 on
1d661 - fix: remove file kind restriction (#4305)
v11.1.0 on
3b94d - feat: add multiple prop to control multi-file drop (#4227)
v11.0.2 on
1c849 - fix: ensure files ref is updated even without onDrop callback (#4168)
v10.7.1 on
15a38 - fix(useDropzone): avoid circular reference (#3683)
v10.7.0 on
8f42a - fix: fix file types filter (#3540)
v10.6.0 on
3f3f1 - feat: add dataTypes option (#3471)
v10.2.1 on
6d4ef - feat: expose files as ref
17d8d - feat: expose more hooks
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter

Released under the MIT License.

FREE WEEKEND
48 Hours of Unlimited Official Vue.js Certification Training
Reserve Your Spot
23-24 August