forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLightbox.tsx
More file actions
89 lines (78 loc) · 2.74 KB
/
ImageLightbox.tsx
File metadata and controls
89 lines (78 loc) · 2.74 KB
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
'use client';
import { useState, useCallback } from 'react';
import { ArrowLeft, ArrowRight } from "@/components/ui/icon";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogTitle,
} from '@/components/ui/dialog';
interface LightboxImage {
src: string;
alt: string;
}
interface ImageLightboxProps {
images: LightboxImage[];
initialIndex: number;
open: boolean;
onOpenChange: (open: boolean) => void;
}
export function ImageLightbox({ images, initialIndex, open, onOpenChange }: ImageLightboxProps) {
const [currentIndex, setCurrentIndex] = useState(initialIndex);
const goToPrev = useCallback(() => {
setCurrentIndex((prev) => (prev > 0 ? prev - 1 : images.length - 1));
}, [images.length]);
const goToNext = useCallback(() => {
setCurrentIndex((prev) => (prev < images.length - 1 ? prev + 1 : 0));
}, [images.length]);
// Reset index when dialog opens with a new initialIndex
const handleOpenChange = useCallback((newOpen: boolean) => {
if (newOpen) {
setCurrentIndex(initialIndex);
}
onOpenChange(newOpen);
}, [initialIndex, onOpenChange]);
if (images.length === 0) return null;
const current = images[currentIndex];
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent
className="max-w-[95vw] max-h-[95vh] p-0 border-none bg-black/90 shadow-none sm:max-w-[95vw]"
showCloseButton
>
<DialogTitle className="sr-only">Image preview</DialogTitle>
<div className="relative flex items-center justify-center min-h-[50vh]">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={current.src}
alt={current.alt}
className="max-w-[90vw] max-h-[90vh] object-contain"
/>
{images.length > 1 && (
<>
<Button
variant="ghost"
size="icon"
onClick={goToPrev}
className="absolute left-2 top-1/2 -translate-y-1/2 rounded-full bg-black/50 p-2 text-white hover:bg-black/70 transition"
>
<ArrowLeft size={24} />
</Button>
<Button
variant="ghost"
size="icon"
onClick={goToNext}
className="absolute right-2 top-1/2 -translate-y-1/2 rounded-full bg-black/50 p-2 text-white hover:bg-black/70 transition"
>
<ArrowRight size={24} />
</Button>
<div className="absolute bottom-3 left-1/2 -translate-x-1/2 text-white/70 text-sm">
{currentIndex + 1} / {images.length}
</div>
</>
)}
</div>
</DialogContent>
</Dialog>
);
}