File size: 2,614 Bytes
df033d6 | 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 | import { Camera, ImageUp, RefreshCw, X } from 'lucide-react'
import { useRef, useState } from 'react'
import { Button } from './ui'
/**
* Empty: a drop zone with "Take or upload a photo" (on phones the button opens the camera or photos).
* With a photo: `children` renders it (plain image or attention view) with Change / Remove.
*/
export default function PhotoPicker({ hasPhoto, onFile, onClear, children, disabled = false, label = 'Take or upload a photo' }) {
const inputRef = useRef(null)
const [dragging, setDragging] = useState(false)
function pick(files) {
const file = files?.[0]
if (file) onFile(file)
if (inputRef.current) inputRef.current.value = '' // choosing the same file again still fires
}
const input = (
<input
ref={inputRef}
type="file"
accept="image/*"
className="sr-only"
tabIndex={-1}
aria-hidden="true"
onChange={(e) => pick(e.target.files)}
/>
)
if (hasPhoto) {
return (
<div className="space-y-3">
<div className="overflow-hidden rounded-control border border-hairline bg-raised">{children}</div>
<div className="flex gap-2">
<Button className="flex-1" onClick={() => inputRef.current?.click()} disabled={disabled}>
<RefreshCw className="size-4" strokeWidth={1.75} aria-hidden="true" />
Change
</Button>
<Button className="flex-1" onClick={onClear} disabled={disabled}>
<X className="size-4" strokeWidth={1.75} aria-hidden="true" />
Remove
</Button>
</div>
{input}
</div>
)
}
return (
<div
onDragOver={(e) => {
e.preventDefault()
setDragging(true)
}}
onDragLeave={() => setDragging(false)}
onDrop={(e) => {
e.preventDefault()
setDragging(false)
pick(e.dataTransfer.files)
}}
className={`grid justify-items-center gap-3 rounded-control border-2 border-dashed px-5 py-8 text-center transition-colors duration-150 ${
dragging ? 'border-accent bg-accent-soft' : 'border-control bg-card'
}`}
>
<ImageUp className="size-7 text-ink-3" strokeWidth={1.5} aria-hidden="true" />
<p className="hidden text-ink-2 sm:block">Drag a photo here, or</p>
<Button variant="primary" onClick={() => inputRef.current?.click()} disabled={disabled}>
<Camera className="size-[18px]" strokeWidth={1.75} aria-hidden="true" />
{label}
</Button>
<p className="text-xs text-ink-3">JPG, PNG or WebP, up to 10 MB</p>
{input}
</div>
)
}
|