Skip to content

useDropZone

Category
Export Size
629 B
Last Changed
15 minutes ago

创建一个可接收文件拖放的区域。

Demo

拖放文件到拖放区域

拖放我PNG
拖放我SVG
通用拖放区域
是否在拖放区域内: false
图片拖放区域
是否在图片拖放区域内: false

用法

vue
<script setup lang="ts">
import { useDropZone } from '@vueuse/core'

const dropZoneRef = ref<HTMLDivElement>()

function onDrop(files: File[] | null) {
  // 当文件被拖放到区域时调用
}

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">
    拖放文件到这里
  </div>
</template>

类型声明

typescript
export interface UseDropZoneReturn {
  files: Ref<File[] | null>
  isOverDropZone: Ref<boolean>
}
export interface UseDropZoneOptions {
  /**
   * 允许的数据类型,如果未设置,则允许所有数据类型。
   * 也可以是检查数据类型的函数。
   */
  dataTypes?: MaybeRef<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 | null | undefined>,
  options?: UseDropZoneOptions | UseDropZoneOptions["onDrop"],
): UseDropZoneReturn

Source

SourceDemoDocs

贡献者

Anthony Fu
丶远方
CofCat
Anthony Fu
danngossinga
babu-ch
Tycho
Diego Pascual
getname1997
Neo Fu
Luka Bagic
aki77
Matthias Stiller
Julien Martin

变更日志

v11.2.0 on 10/30/2024
1d661 - fix: remove file kind restriction (#4305)
v11.1.0 on 9/16/2024
3b94d - feat: add multiple prop to control multi-file drop (#4227)
v11.0.2 on 8/24/2024
1c849 - fix: ensure files ref is updated even without onDrop callback (#4168)
v10.7.1 on 12/27/2023
15a38 - fix(useDropzone): avoid circular reference (#3683)
v10.7.0 on 12/5/2023
8f42a - fix: fix file types filter (#3540)
v10.6.0 on 11/9/2023
3f3f1 - feat: add dataTypes option (#3471)
v10.2.1 on 6/28/2023
6d4ef - feat: expose files as ref
17d8d - feat: expose more hooks
v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter

Released under the MIT License.