|
| 1 | +import { useState } from "react"; |
| 2 | +import Heading from "@/components/Heading/index"; |
| 3 | + |
| 4 | +const images = [ |
| 5 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image.jpg", |
| 6 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-1.jpg", |
| 7 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-2.jpg", |
| 8 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-3.jpg", |
| 9 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-4.jpg", |
| 10 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-5.jpg", |
| 11 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-6.jpg", |
| 12 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-7.jpg", |
| 13 | + "https://flowbite.s3.amazonaws.com/docs/gallery/masonry/image-8.jpg", |
| 14 | +]; |
| 15 | + |
| 16 | +export default function Gallery() { |
| 17 | + const [modalOpen, setModalOpen] = useState(false); |
| 18 | + const [selectedImageIndex, setSelectedImageIndex] = useState(null); |
| 19 | + |
| 20 | + const openModal = (index = 0) => { |
| 21 | + setSelectedImageIndex(index); |
| 22 | + setModalOpen(true); |
| 23 | + }; |
| 24 | + |
| 25 | + const closeModal = () => { |
| 26 | + setModalOpen(false); |
| 27 | + }; |
| 28 | + |
| 29 | + const goToPrevious = () => { |
| 30 | + setSelectedImageIndex((prevIndex) => |
| 31 | + prevIndex === 0 ? images.length - 1 : prevIndex - 1, |
| 32 | + ); |
| 33 | + }; |
| 34 | + |
| 35 | + const goToNext = () => { |
| 36 | + setSelectedImageIndex((prevIndex) => |
| 37 | + prevIndex === images.length - 1 ? 0 : prevIndex + 1, |
| 38 | + ); |
| 39 | + }; |
| 40 | + |
| 41 | + const handleKeyDown = (event) => { |
| 42 | + if (event.key === "Escape") { |
| 43 | + closeModal(); |
| 44 | + } |
| 45 | + // You can add more key handlers here as needed |
| 46 | + }; |
| 47 | + |
| 48 | + return ( |
| 49 | + <div> |
| 50 | + <Heading |
| 51 | + text="Gallery" |
| 52 | + className="text-center absolute top-0 left-0 right-0 mb-24" |
| 53 | + /> |
| 54 | + <div className="grid grid-cols-2 md:grid-cols-3 gap-4 m-[5%]"> |
| 55 | + {images.map((image, index) => ( |
| 56 | + <div |
| 57 | + role="button" |
| 58 | + aria-label="Open" |
| 59 | + onClick={() => openModal(index)} |
| 60 | + onKeyDown={(e) => handleKeyDown(e, index)} |
| 61 | + tabIndex={0} |
| 62 | + key={image} |
| 63 | + className="cursor-pointer" |
| 64 | + > |
| 65 | + <img |
| 66 | + className="h-auto max-w-full rounded-lg aspect-square" |
| 67 | + src={image} |
| 68 | + alt={`Gallery img ${index + 1}`} |
| 69 | + /> |
| 70 | + </div> |
| 71 | + ))} |
| 72 | + |
| 73 | + {modalOpen ? ( |
| 74 | + <div className="fixed inset-0 flex items-center justify-center z-50 bg-black bg-opacity-75"> |
| 75 | + <div className="relative"> |
| 76 | + <button |
| 77 | + type="button" |
| 78 | + className="absolute top-0 right-0 m-4 text-white bg-gray-500 hover:bg-gray-700 font-bold py-2 px-4 border border-gray-700 rounded" |
| 79 | + onClick={closeModal} |
| 80 | + aria-label="Close" |
| 81 | + > |
| 82 | + <i className="fa-solid fa-xmark" /> |
| 83 | + </button> |
| 84 | + <button |
| 85 | + className="absolute left-0 top-1/2 transform -translate-y-1/2 text-black font-bold py-2 px-4 border border-black rounded-3xl ml-5" |
| 86 | + onClick={goToPrevious} |
| 87 | + aria-label="Previous Image" |
| 88 | + type="button" |
| 89 | + > |
| 90 | + <i className="fa-solid fa-backward" /> |
| 91 | + </button> |
| 92 | + <button |
| 93 | + className="absolute right-0 top-1/2 transform -translate-y-1/2 text-black font-bold py-2 px-4 border border-black rounded-3xl mr-5" |
| 94 | + onClick={goToNext} |
| 95 | + aria-label="Next Image" |
| 96 | + type="button" |
| 97 | + > |
| 98 | + <i className="fa-solid fa-forward" /> |
| 99 | + </button> |
| 100 | + <img |
| 101 | + className="max-w-full rounded-lg" |
| 102 | + src={images[selectedImageIndex]} |
| 103 | + alt={`Selected gallery img ${selectedImageIndex + 1}`} |
| 104 | + /> |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + ) : ( |
| 108 | + <div> </div> |
| 109 | + )} |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + ); |
| 113 | +} |
0 commit comments