1- import { useState } from "react" ;
1+ import { useState , useEffect } from "react" ;
22import Heading from "@/components/Heading/index" ;
33import PageTransition from "../../components/PageTransition" ;
44
@@ -18,34 +18,56 @@ export default function Gallery() {
1818 const [ modalOpen , setModalOpen ] = useState ( false ) ;
1919 const [ selectedImageIndex , setSelectedImageIndex ] = useState ( null ) ;
2020
21+ // Function to open the modal and set the selected image index
2122 const openModal = ( index = 0 ) => {
2223 setSelectedImageIndex ( index ) ;
2324 setModalOpen ( true ) ;
2425 } ;
2526
27+ // Function to close the modal
2628 const closeModal = ( ) => {
2729 setModalOpen ( false ) ;
2830 } ;
2931
32+ // Function to navigate to the previous image
3033 const goToPrevious = ( ) => {
3134 setSelectedImageIndex ( ( prevIndex ) =>
32- prevIndex === 0 ? images . length - 1 : prevIndex - 1 ,
35+ prevIndex === 0 ? images . length - 1 : prevIndex - 1
3336 ) ;
3437 } ;
3538
39+ // Function to navigate to the next image
3640 const goToNext = ( ) => {
3741 setSelectedImageIndex ( ( prevIndex ) =>
38- prevIndex === images . length - 1 ? 0 : prevIndex + 1 ,
42+ prevIndex === images . length - 1 ? 0 : prevIndex + 1
3943 ) ;
4044 } ;
4145
46+ // Function to handle keydown events for modal navigation
4247 const handleKeyDown = ( event ) => {
4348 if ( event . key === "Escape" ) {
4449 closeModal ( ) ;
50+ } else if ( event . key === "ArrowLeft" ) {
51+ goToPrevious ( ) ;
52+ } else if ( event . key === "ArrowRight" ) {
53+ goToNext ( ) ;
4554 }
46- // You can add more key handlers here as needed
4755 } ;
4856
57+ // Add event listener for keydown events when modal is open
58+ useEffect ( ( ) => {
59+ if ( modalOpen ) {
60+ window . addEventListener ( "keydown" , handleKeyDown ) ;
61+ } else {
62+ window . removeEventListener ( "keydown" , handleKeyDown ) ;
63+ }
64+
65+ // Clean up event listener on unmount
66+ return ( ) => {
67+ window . removeEventListener ( "keydown" , handleKeyDown ) ;
68+ } ;
69+ } , [ modalOpen ] ) ;
70+
4971 return (
5072 < PageTransition >
5173 < Heading
@@ -54,32 +76,38 @@ export default function Gallery() {
5476 />
5577 < div className = "grid grid-cols-2 md:grid-cols-3 gap-4 m-[5%]" >
5678 { images . map ( ( image , index ) => (
57- < div
58- role = "button"
59- aria-label = "Open"
60- onClick = { ( ) => openModal ( index ) }
61- onKeyDown = { ( e ) => handleKeyDown ( e , index ) }
62- tabIndex = { 0 }
63- key = { image }
64- className = "cursor-pointer"
65- >
66- < img
67- className = "h-auto max-w-full rounded-lg aspect-square"
68- src = { image }
69- alt = { `Gallery img ${ index + 1 } ` }
70- />
71- </ div >
79+ < div
80+ role = "button"
81+ aria-label = "Open"
82+ onClick = { ( ) => openModal ( index ) }
83+ onKeyDown = { ( e ) => {
84+ if ( e . key === 'Enter' || e . key === ' ' ) {
85+ openModal ( index ) ;
86+ }
87+ } }
88+ tabIndex = { 0 } // Makes the div focusable
89+ key = { image }
90+ className = "cursor-pointer"
91+ >
92+ < img
93+ className = "h-auto max-w-full rounded-lg aspect-square"
94+ src = { image }
95+ alt = { `Gallery img ${ index + 1 } ` }
96+ />
97+ </ div >
98+
7299 ) ) }
73100
74101 { modalOpen ? (
75- < div className = "fixed inset-0 flex items-center justify-center z-50 bg-black bg-opacity-75" >
76- < div className = "relative" >
102+ < div className = "fixed inset-0 flex items-center justify-center z-50 bg-black bg-opacity-75 overflow-y-auto " >
103+ < div className = "relative max-h-screen max-w-screen-lg " >
77104 < button
78105 type = "button"
79106 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"
80107 onClick = { closeModal }
81108 aria-label = "Close"
82109 >
110+ x
83111 < i className = "fa-solid fa-xmark" />
84112 </ button >
85113 < button
@@ -88,6 +116,7 @@ export default function Gallery() {
88116 aria-label = "Previous Image"
89117 type = "button"
90118 >
119+ <<
91120 < i className = "fa-solid fa-backward" />
92121 </ button >
93122 < button
@@ -96,10 +125,11 @@ export default function Gallery() {
96125 aria-label = "Next Image"
97126 type = "button"
98127 >
128+ >>
99129 < i className = "fa-solid fa-forward" />
100130 </ button >
101131 < img
102- className = "max-w-full rounded-lg"
132+ className = "max-h-screen max- w-full rounded-lg"
103133 src = { images [ selectedImageIndex ] }
104134 alt = { `Selected gallery img ${ selectedImageIndex + 1 } ` }
105135 />
0 commit comments