Spaces:
Running
Running
File size: 5,031 Bytes
ccb7e46 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import { useCallback } from 'react'
import { useDropzone } from 'react-dropzone'
import { motion, AnimatePresence } from 'framer-motion'
import { Upload, X, Image as ImageIcon, User } from 'lucide-react'
function ImageUpload({ label, image, onImageSelect, onClear }) {
const onDrop = useCallback((acceptedFiles) => {
const file = acceptedFiles[0]
if (file) {
const preview = URL.createObjectURL(file)
onImageSelect({ file, preview })
}
}, [onImageSelect])
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop,
accept: {
'image/*': ['.jpeg', '.jpg', '.png', '.webp']
},
maxFiles: 1,
maxSize: 10 * 1024 * 1024 // 10MB
})
return (
<div className="relative">
<label className="block text-sm font-medium text-gray-400 mb-3 flex items-center gap-2">
<User className="w-4 h-4" />
{label}
</label>
<AnimatePresence mode="wait">
{!image ? (
<motion.div
key="dropzone"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
{...getRootProps()}
className={`
upload-zone relative cursor-pointer
border-2 border-dashed rounded-2xl p-8
transition-all duration-300 min-h-[300px]
flex flex-col items-center justify-center
${isDragActive
? 'active border-primary-500 bg-primary-500/10'
: 'border-gray-700 hover:border-gray-600'
}
`}
>
<input {...getInputProps()} />
{/* Animated upload icon */}
<motion.div
animate={isDragActive ? { scale: 1.1, y: -5 } : { scale: 1, y: 0 }}
transition={{ type: 'spring', stiffness: 300 }}
className="relative mb-6"
>
<div className="w-20 h-20 rounded-2xl bg-gradient-to-br from-primary-500/20 to-purple-500/20 flex items-center justify-center">
<Upload className={`w-10 h-10 ${isDragActive ? 'text-primary-400' : 'text-gray-500'}`} />
</div>
{isDragActive && (
<motion.div
initial={{ scale: 0.8, opacity: 0 }}
animate={{ scale: 1.5, opacity: 0 }}
transition={{ repeat: Infinity, duration: 1 }}
className="absolute inset-0 rounded-2xl border-2 border-primary-500"
/>
)}
</motion.div>
<p className="text-gray-300 font-medium mb-2">
{isDragActive ? 'Drop image here' : 'Drag & drop an image'}
</p>
<p className="text-sm text-gray-500">
or <span className="text-primary-400 hover:text-primary-300">browse files</span>
</p>
<p className="text-xs text-gray-600 mt-4">
Supports: JPG, PNG, WebP (max 10MB)
</p>
</motion.div>
) : (
<motion.div
key="preview"
initial={{ opacity: 0, scale: 0.95 }}
animate={{ opacity: 1, scale: 1 }}
exit={{ opacity: 0, scale: 0.95 }}
className="relative rounded-2xl overflow-hidden group min-h-[300px] bg-dark-200"
>
{/* Image preview */}
<img
src={image.preview}
alt="Preview"
className="w-full h-[300px] object-cover"
/>
{/* Overlay on hover */}
<div className="absolute inset-0 bg-gradient-to-t from-black/80 via-black/20 to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300" />
{/* Clear button */}
<motion.button
initial={{ opacity: 0, scale: 0.8 }}
animate={{ opacity: 1, scale: 1 }}
whileHover={{ scale: 1.1 }}
whileTap={{ scale: 0.9 }}
onClick={(e) => {
e.stopPropagation()
onClear()
}}
className="absolute top-3 right-3 w-10 h-10 rounded-full bg-black/50 backdrop-blur-sm flex items-center justify-center text-white hover:bg-red-500/80 transition-colors"
>
<X className="w-5 h-5" />
</motion.button>
{/* File info */}
<div className="absolute bottom-0 left-0 right-0 p-4 opacity-0 group-hover:opacity-100 transition-opacity">
<div className="flex items-center gap-2 text-sm text-white">
<ImageIcon className="w-4 h-4" />
<span className="truncate">{image.file.name}</span>
</div>
</div>
{/* Success indicator */}
<div className="absolute top-3 left-3">
<div className="w-3 h-3 rounded-full bg-green-500 shadow-lg shadow-green-500/50" />
</div>
</motion.div>
)}
</AnimatePresence>
</div>
)
}
export default ImageUpload
|