swadhinbiswas Initial release v0.1.0 · 1/16/2026
ed7a4e4
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import { useEffect, useState } from 'react'
import { X, Download, ZoomIn, ZoomOut, ChevronLeft, ChevronRight, ExternalLink, Film } from 'lucide-react'
import CachedImage from '../common/CachedImage'
import VideoPlayer from './VideoPlayer'
// Check if URL is a video file
const isVideo = (url: string): boolean => {
const ext = url.split('.').pop()?.toLowerCase()
return ['webm', 'mp4', 'mov', 'gif'].includes(ext || '')
}
interface ImageLightboxProps {
images: Array<{
url: string
thumbnail: string
filename?: string
width?: number
height?: number
}>
initialIndex: number
onClose: () => void
}
export default function ImageLightbox({ images, initialIndex, onClose }: ImageLightboxProps) {
const [currentIndex, setCurrentIndex] = useState(initialIndex)
const [zoom, setZoom] = useState(1)
const [pan, setPan] = useState({ x: 0, y: 0 })
const [isDragging, setIsDragging] = useState(false)
const [dragStart, setDragStart] = useState({ x: 0, y: 0 })
const currentImage = images[currentIndex]
// Keyboard navigation
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
switch (e.key) {
case 'Escape':
onClose()
break
case 'ArrowLeft':
goToPrevious()
break
case 'ArrowRight':
goToNext()
break
case '+':
case '=':
setZoom(z => Math.min(z + 0.5, 4))
break
case '-':
setZoom(z => Math.max(z - 0.5, 0.5))
break
}
}
window.addEventListener('keydown', handleKeyDown)
return () => window.removeEventListener('keydown', handleKeyDown)
}, [currentIndex])
const goToNext = () => {
if (currentIndex < images.length - 1) {
setCurrentIndex(currentIndex + 1)
resetView()
}
}
const goToPrevious = () => {
if (currentIndex > 0) {
setCurrentIndex(currentIndex - 1)
resetView()
}
}
const resetView = () => {
setZoom(1)
setPan({ x: 0, y: 0 })
}
const handleDownload = async () => {
try {
const response = await fetch(currentImage.url)
const blob = await response.blob()
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = currentImage.filename || 'image.jpg'
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
window.URL.revokeObjectURL(url)
} catch (error) {
console.error('Download failed:', error)
}
}
const handleMouseDown = (e: React.MouseEvent) => {
if (zoom > 1) {
setIsDragging(true)
setDragStart({ x: e.clientX - pan.x, y: e.clientY - pan.y })
}
}
const handleMouseMove = (e: React.MouseEvent) => {
if (isDragging) {
setPan({
x: e.clientX - dragStart.x,
y: e.clientY - dragStart.y,
})
}
}
const handleMouseUp = () => {
setIsDragging(false)
}
return (
<div
className="fixed inset-0 z-50 bg-black/95 flex flex-col"
onClick={onClose}
>
{/* Header */}
<div className="flex items-center justify-between p-4 bg-black/50" onClick={(e) => e.stopPropagation()}>
<div className="flex items-center gap-4">
<span className="text-white font-medium">
{currentImage.filename || 'Image'}
</span>
{currentImage.width && currentImage.height && (
<span className="text-gray-400 text-sm">
{currentImage.width} × {currentImage.height}
</span>
)}
<span className="text-gray-400 text-sm">
{currentIndex + 1} / {images.length}
</span>
</div>
<div className="flex items-center gap-2">
<button
onClick={() => setZoom(z => Math.max(z - 0.5, 0.5))}
className="p-2 hover:bg-white/10 rounded transition-colors"
title="Zoom Out"
>
<ZoomOut className="w-5 h-5 text-white" />
</button>
<button
onClick={() => setZoom(z => Math.min(z + 0.5, 4))}
className="p-2 hover:bg-white/10 rounded transition-colors"
title="Zoom In"
>
<ZoomIn className="w-5 h-5 text-white" />
</button>
<button
onClick={handleDownload}
className="p-2 hover:bg-white/10 rounded transition-colors"
title="Download"
>
<Download className="w-5 h-5 text-white" />
</button>
<a
href={currentImage.url}
target="_blank"
rel="noopener noreferrer"
className="p-2 hover:bg-white/10 rounded transition-colors"
title="Open in New Tab"
onClick={(e) => e.stopPropagation()}
>
<ExternalLink className="w-5 h-5 text-white" />
</a>
<button
onClick={onClose}
className="p-2 hover:bg-white/10 rounded transition-colors"
title="Close (Esc)"
>
<X className="w-5 h-5 text-white" />
</button>
</div>
</div>
{/* Media Container */}
<div
className="flex-1 flex items-center justify-center relative overflow-hidden"
onClick={(e) => e.stopPropagation()}
onMouseDown={!isVideo(currentImage.url) ? handleMouseDown : undefined}
onMouseMove={!isVideo(currentImage.url) ? handleMouseMove : undefined}
onMouseUp={!isVideo(currentImage.url) ? handleMouseUp : undefined}
onMouseLeave={!isVideo(currentImage.url) ? handleMouseUp : undefined}
style={{ cursor: !isVideo(currentImage.url) && zoom > 1 ? (isDragging ? 'grabbing' : 'grab') : 'default' }}
>
{isVideo(currentImage.url) ? (
<VideoPlayer
key={currentIndex}
src={currentImage.url}
poster={currentImage.thumbnail}
filename={currentImage.filename}
autoPlay
className="max-w-[90%] max-h-[80vh]"
/>
) : (
<CachedImage
key={currentIndex}
src={currentImage.url}
alt={currentImage.filename || 'Image'}
style={{
transform: `scale(${zoom}) translate(${pan.x / zoom}px, ${pan.y / zoom}px)`,
maxWidth: zoom === 1 ? '90%' : 'none',
maxHeight: zoom === 1 ? '90%' : 'none',
objectFit: 'contain',
}}
className="select-none transition-opacity duration-200"
/>
)}
{/* Navigation Arrows */}
{images.length > 1 && (
<>
{currentIndex > 0 && (
<button
onClick={(e) => { e.stopPropagation(); goToPrevious() }}
className="absolute left-4 top-1/2 -translate-y-1/2 p-3 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Previous (←)"
>
<ChevronLeft className="w-8 h-8 text-white" />
</button>
)}
{currentIndex < images.length - 1 && (
<button
onClick={(e) => { e.stopPropagation(); goToNext() }}
className="absolute right-4 top-1/2 -translate-y-1/2 p-3 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Next (→)"
>
<ChevronRight className="w-8 h-8 text-white" />
</button>
)}
</>
)}
</div>
{/* Thumbnails Strip */}
{images.length > 1 && (
<div className="p-4 bg-black/50 flex gap-2 overflow-x-auto" onClick={(e) => e.stopPropagation()}>
{images.map((img, idx) => (
<button
key={idx}
onClick={() => { setCurrentIndex(idx); resetView() }}
className={`flex-shrink-0 w-20 h-20 rounded overflow-hidden border-2 transition-all ${idx === currentIndex
? 'border-primary-500 scale-110'
: 'border-transparent opacity-50 hover:opacity-100'
}`}
>
<img
src={img.thumbnail}
alt={`Thumbnail ${idx + 1}`}
className="w-full h-full object-cover"
/>
</button>
))}
</div>
)}
{/* Help */}
<div className="absolute bottom-20 left-1/2 -translate-x-1/2 text-gray-400 text-xs bg-black/70 px-4 py-2 rounded-full">
← → Navigate • +/- Zoom • Esc Close • Click & Drag to Pan
</div>
</div>
)
}