12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- import { useCallback, useState } from 'react'
- export const useDraggableUploader = <T extends HTMLElement>(setImageFn: (file: File) => void) => {
- const [isDragActive, setIsDragActive] = useState(false)
- const handleDragEnter = useCallback((e: React.DragEvent<T>) => {
- e.preventDefault()
- e.stopPropagation()
- setIsDragActive(true)
- }, [])
- const handleDragOver = useCallback((e: React.DragEvent<T>) => {
- e.preventDefault()
- e.stopPropagation()
- }, [])
- const handleDragLeave = useCallback((e: React.DragEvent<T>) => {
- e.preventDefault()
- e.stopPropagation()
- setIsDragActive(false)
- }, [])
- const handleDrop = useCallback((e: React.DragEvent<T>) => {
- e.preventDefault()
- e.stopPropagation()
- setIsDragActive(false)
- const file = e.dataTransfer.files[0]
- if (!file)
- return
- setImageFn(file)
- }, [setImageFn])
- return {
- handleDragEnter,
- handleDragOver,
- handleDragLeave,
- handleDrop,
- isDragActive,
- }
- }
|