Spaces:
Running
Running
Download frontend/components/ui/ImageViewer.tsx from axeronis/microfunnel-api: direct link, hf CLI and curl.
- Browser
- Download file 4.7 kB
-
https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/ui/ImageViewer.tsx
- Command line
-
hf download hf://spaces/axeronis/microfunnel-api/frontend/components/ui/ImageViewer.tsx
-
curl -L -o ImageViewer.tsx https://huggingface.co/spaces/axeronis/microfunnel-api/resolve/main/frontend/components/ui/ImageViewer.tsx
4.7 kB
| '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<HTMLDivElement>(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 ( | |
| <AnimatePresence> | |
| {isOpen && createPortal( | |
| <motion.div | |
| initial={{ opacity: 0 }} | |
| animate={{ opacity: 1 }} | |
| exit={{ opacity: 0 }} | |
| transition={{ duration: 0.2 }} | |
| className="fixed inset-0 z-[9999] bg-black/95 backdrop-blur-md flex flex-col items-center justify-center touch-none overflow-hidden" | |
| onClick={onClose} | |
| > | |
| {/* Header Controls - Absolute top */} | |
| <div className="absolute top-0 left-0 right-0 flex items-center justify-between p-4 z-50"> | |
| <button | |
| onClick={(e) => { e.stopPropagation(); onClose(); }} | |
| className="p-3 rounded-full bg-white/10 hover:bg-white/20 text-white transition-colors backdrop-blur-md" | |
| > | |
| <X className="w-6 h-6" /> | |
| </button> | |
| <div className="flex items-center gap-4"> | |
| <button | |
| onClick={(e) => { e.stopPropagation(); handleDownload(); }} | |
| className="p-3 rounded-full bg-white/10 hover:bg-white/20 text-white transition-colors backdrop-blur-md" | |
| > | |
| <Download className="w-6 h-6" /> | |
| </button> | |
| </div> | |
| </div> | |
| {/* Image Container - Center of Screen */} | |
| <div | |
| className="w-full h-full flex items-center justify-center relative touch-none" | |
| ref={containerRef} | |
| > | |
| <motion.div | |
| drag={scale > 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()} | |
| > | |
| <img | |
| src={src} | |
| alt={alt} | |
| className="max-w-[95vw] max-h-[95dvh] object-contain shadow-2xl rounded-sm" | |
| draggable={false} | |
| /> | |
| </motion.div> | |
| </div> | |
| </motion.div>, | |
| document.body | |
| )} | |
| </AnimatePresence> | |
| ) | |
| } | |