'use client' import { useState, useCallback, useRef, KeyboardEvent } from 'react' interface DropZoneProps { onFilesDropped: (files: File[]) => void accept?: string[] maxSize?: number maxFiles?: number children?: React.ReactNode className?: string disabled?: boolean ariaLabel?: string } export default function DropZone({ onFilesDropped, accept = ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'video/mp4', 'video/webm'], maxSize = 100 * 1024 * 1024, // 100MB maxFiles = 10, children, className = '', disabled = false, ariaLabel = 'Drop files here or press Enter to browse', }: DropZoneProps) { const [isDragging, setIsDragging] = useState(false) const dragCounter = useRef(0) const fileInputRef = useRef(null) const handleDragEnter = useCallback((e: React.DragEvent) => { e.preventDefault() e.stopPropagation() dragCounter.current += 1 if (!disabled) { setIsDragging(true) } }, [disabled]) const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault() e.stopPropagation() dragCounter.current -= 1 if (dragCounter.current === 0) { setIsDragging(false) } }, []) const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault() e.stopPropagation() }, []) const processFiles = useCallback((files: File[]) => { const validFiles = files.filter(file => { if (accept.length > 0 && !accept.some(type => file.type.match(type))) return false if (file.size > maxSize) return false return true }) if (validFiles.length > 0) { onFilesDropped(validFiles.slice(0, maxFiles)) } }, [accept, maxSize, maxFiles, onFilesDropped]) const handleKeyDown = useCallback((e: KeyboardEvent) => { if (disabled) return if (e.key === 'Enter' || e.key === ' ') { e.preventDefault() fileInputRef.current?.click() } }, [disabled]) const handleFileInputChange = useCallback((e: React.ChangeEvent) => { const files = Array.from(e.target.files ?? []) if (files.length > 0) processFiles(files) // Reset so the same file can be re-selected e.target.value = '' }, [processFiles]) const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault() e.stopPropagation() setIsDragging(false) dragCounter.current = 0 if (disabled) return processFiles(Array.from(e.dataTransfer.files)) }, [disabled, processFiles]) const acceptAttr = accept.join(',') return (
{children} {/* Drag overlay */} {isDragging && (

Drop files here

Images or videos

)}
) }