'use client' import { createPortal } from 'react-dom' import { X, ZoomIn, ZoomOut, Download } from 'lucide-react' import { motion, AnimatePresence } from 'framer-motion' import { useState, useRef, useEffect } from 'react' import { cn } from '@/lib/utils' interface ImageViewerProps { src: string alt: string isOpen: boolean onClose: () => void } export function ImageViewer({ src, alt, isOpen, onClose }: ImageViewerProps) { const [mounted, setMounted] = useState(false) const [scale, setScale] = useState(1) const [position, setPosition] = useState({ x: 0, y: 0 }) const [isDragging, setIsDragging] = useState(false) const containerRef = useRef(null) useEffect(() => { setMounted(true) }, []) // Reset zoom on close useEffect(() => { if (!isOpen) { setScale(1) setPosition({ x: 0, y: 0 }) } }, [isOpen]) const handleZoomIn = () => setScale(prev => Math.min(prev + 0.5, 4)) const handleZoomOut = () => setScale(prev => Math.max(prev - 0.5, 1)) const handleDownload = async () => { try { const response = await fetch(src) const blob = await response.blob() const url = window.URL.createObjectURL(blob) const a = document.createElement('a') a.href = url a.download = alt || 'image.jpg' document.body.appendChild(a) a.click() window.URL.revokeObjectURL(url) document.body.removeChild(a) } catch (err) { console.error('Download failed', err) window.open(src, '_blank') } } if (!mounted) return null return ( {isOpen && createPortal( {/* Header Controls - Absolute top */}
{/* Image Container - Center of Screen */}
1} dragConstraints={containerRef} dragElastic={0.1} onDragStart={() => setIsDragging(true)} onDragEnd={() => setIsDragging(false)} animate={{ scale, x: position.x, y: position.y }} transition={{ type: "spring", stiffness: 300, damping: 30 }} className="relative cursor-grab active:cursor-grabbing flex items-center justify-center" onClick={(e) => e.stopPropagation()} > {alt}
, document.body )}
) }