|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useRef, useId } from "react"; |
| 4 | +import { ImagePlus, AlertCircle } from "lucide-react"; |
| 5 | +import { ImagePreview } from "@/components/common/ImagePreview"; |
| 6 | +import type { ImageFile } from "@/hooks/useImageUpload"; |
| 7 | +import { cn } from "@/lib/utils"; |
| 8 | + |
| 9 | +interface ImageDropzoneProps { |
| 10 | + images: ImageFile[]; |
| 11 | + isDragging: boolean; |
| 12 | + canAddMore: boolean; |
| 13 | + remainingSlots: number; |
| 14 | + maxFiles: number; |
| 15 | + label?: string; |
| 16 | + error?: string; |
| 17 | + acceptedFormats?: string; |
| 18 | + onDragEnter: (e: React.DragEvent) => void; |
| 19 | + onDragLeave: (e: React.DragEvent) => void; |
| 20 | + onDragOver: (e: React.DragEvent) => void; |
| 21 | + onDrop: (e: React.DragEvent) => void; |
| 22 | + onFileSelect: (e: React.ChangeEvent<HTMLInputElement>) => void; |
| 23 | + onRemove: (id: string) => void; |
| 24 | +} |
| 25 | + |
| 26 | +export const ImageDropzone = ({ |
| 27 | + images, |
| 28 | + isDragging, |
| 29 | + canAddMore, |
| 30 | + remainingSlots, |
| 31 | + maxFiles, |
| 32 | + label = "Images", |
| 33 | + error, |
| 34 | + acceptedFormats = "image/png,image/jpeg,image/heic,image/webp", |
| 35 | + onDragEnter, |
| 36 | + onDragLeave, |
| 37 | + onDragOver, |
| 38 | + onDrop, |
| 39 | + onFileSelect, |
| 40 | + onRemove, |
| 41 | +}: ImageDropzoneProps) => { |
| 42 | + const inputRef = useRef<HTMLInputElement>(null); |
| 43 | + const dropzoneId = useId(); |
| 44 | + const errorId = useId(); |
| 45 | + const instructionsId = useId(); |
| 46 | + |
| 47 | + const handleKeyDown = (e: React.KeyboardEvent) => { |
| 48 | + if (e.key === "Enter" || e.key === " ") { |
| 49 | + e.preventDefault(); |
| 50 | + inputRef.current?.click(); |
| 51 | + } |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <div className="space-y-2"> |
| 56 | + <div className="flex items-center justify-between"> |
| 57 | + <label id={`${dropzoneId}-label`} className="block text-sm font-medium"> |
| 58 | + {label} |
| 59 | + </label> |
| 60 | + <span className="text-muted-foreground text-xs"> |
| 61 | + {images.length}/{maxFiles} images |
| 62 | + </span> |
| 63 | + </div> |
| 64 | + |
| 65 | + <div |
| 66 | + role="button" |
| 67 | + tabIndex={canAddMore ? 0 : -1} |
| 68 | + aria-labelledby={`${dropzoneId}-label`} |
| 69 | + aria-describedby={`${instructionsId} ${error ? errorId : ""}`} |
| 70 | + aria-disabled={!canAddMore} |
| 71 | + onDragEnter={onDragEnter} |
| 72 | + onDragLeave={onDragLeave} |
| 73 | + onDragOver={onDragOver} |
| 74 | + onDrop={onDrop} |
| 75 | + onKeyDown={handleKeyDown} |
| 76 | + onClick={() => canAddMore && inputRef.current?.click()} |
| 77 | + className={cn( |
| 78 | + "cursor-pointer rounded-lg border-2 border-dashed p-6 text-center transition-all", |
| 79 | + "focus-visible:ring-ring focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:outline-none", |
| 80 | + isDragging && "scale-[1.02] border-blue-500 bg-blue-50", |
| 81 | + !isDragging && !error && "border-gray-300 hover:border-blue-400 hover:bg-blue-50/30", |
| 82 | + error && "border-red-300 bg-red-50/30", |
| 83 | + !canAddMore && "cursor-not-allowed opacity-50" |
| 84 | + )} |
| 85 | + > |
| 86 | + <input |
| 87 | + ref={inputRef} |
| 88 | + type="file" |
| 89 | + multiple |
| 90 | + accept={acceptedFormats} |
| 91 | + className="sr-only" |
| 92 | + onChange={onFileSelect} |
| 93 | + disabled={!canAddMore} |
| 94 | + aria-hidden="true" |
| 95 | + tabIndex={-1} |
| 96 | + /> |
| 97 | + |
| 98 | + {images.length === 0 ? ( |
| 99 | + <div className="flex flex-col items-center justify-center py-8"> |
| 100 | + <div |
| 101 | + className={cn( |
| 102 | + "mb-4 flex h-14 w-14 items-center justify-center rounded-lg transition-colors", |
| 103 | + isDragging ? "bg-blue-100" : "bg-gray-100" |
| 104 | + )} |
| 105 | + > |
| 106 | + <ImagePlus |
| 107 | + className={cn( |
| 108 | + "h-7 w-7 transition-colors", |
| 109 | + isDragging ? "text-blue-500" : "text-gray-400" |
| 110 | + )} |
| 111 | + aria-hidden="true" |
| 112 | + /> |
| 113 | + </div> |
| 114 | + <p className="mb-1 font-medium text-gray-600"> |
| 115 | + {isDragging ? "Drop images here" : "Drop images here or click to browse"} |
| 116 | + </p> |
| 117 | + <p id={instructionsId} className="text-xs text-gray-400"> |
| 118 | + Up to {maxFiles} images. PNG, JPG, HEIC supported. |
| 119 | + </p> |
| 120 | + </div> |
| 121 | + ) : ( |
| 122 | + <div className="space-y-4"> |
| 123 | + <div |
| 124 | + className="grid gap-3" |
| 125 | + style={{ |
| 126 | + gridTemplateColumns: `repeat(auto-fill, minmax(80px, 1fr))`, |
| 127 | + }} |
| 128 | + role="list" |
| 129 | + aria-label="Uploaded images" |
| 130 | + > |
| 131 | + {images.map((image, index) => ( |
| 132 | + <ImagePreview |
| 133 | + key={image.id} |
| 134 | + image={image} |
| 135 | + index={index} |
| 136 | + onRemove={() => onRemove(image.id)} |
| 137 | + /> |
| 138 | + ))} |
| 139 | + </div> |
| 140 | + |
| 141 | + {canAddMore && ( |
| 142 | + <p id={instructionsId} className="text-xs text-gray-500"> |
| 143 | + {remainingSlots === 1 |
| 144 | + ? "You can add 1 more image" |
| 145 | + : `You can add ${remainingSlots} more images`} |
| 146 | + </p> |
| 147 | + )} |
| 148 | + </div> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + |
| 152 | + {error && ( |
| 153 | + <p id={errorId} role="alert" className="flex items-center gap-1 text-sm text-red-600"> |
| 154 | + <AlertCircle className="h-4 w-4" aria-hidden="true" /> |
| 155 | + {error} |
| 156 | + </p> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + ); |
| 160 | +}; |
0 commit comments