@@ -2,16 +2,19 @@ import {
22 AppBar ,
33 Box ,
44 CardActionArea ,
5- Container , Dialog ,
5+ Container ,
6+ Dialog ,
67 Grid ,
78 IconButton ,
8- Paper , Slide ,
9+ Paper ,
10+ Slide ,
911 Stack ,
1012 Toolbar ,
11- Typography , useMediaQuery
13+ Typography ,
14+ useMediaQuery
1215} from "@mui/material" ;
1316import { Close , KeyboardArrowLeft , KeyboardArrowRight } from "@mui/icons-material" ;
14- import React , { useState } from "react" ;
17+ import React , { useCallback , useEffect , useState } from "react" ;
1518import images from "../../../assets/images/event/Images" ;
1619
1720const Transition = React . forwardRef ( function Transition (
@@ -28,28 +31,62 @@ export function GalleryDialog(props) {
2831 const [ selectedImage , setSelectedImage ] = useState ( 0 )
2932 const isMobile = useMediaQuery ( ( theme ) => theme . breakpoints . down ( 'md' ) ) ;
3033
34+
35+ const handleKeyboardInput = useCallback ( ( event ) => {
36+ if ( event ?. key === "ArrowRight" ) {
37+ nextImage ( )
38+ }
39+ if ( event ?. key === "ArrowLeft" ) {
40+ previousImage ( )
41+ }
42+ } , [ ] )
43+
44+ const nextImage = ( ) => {
45+ setNewImage ( selectedImage => {
46+ return ( ( selectedImage + 1 ) % images [ galleryTab ] . items . length )
47+ } )
48+ } ;
49+ const previousImage = ( ) => {
50+ setNewImage ( selectedImage => {
51+ return ( ( ( ( selectedImage - 1 ) % images [ galleryTab ] . items . length ) + images [ galleryTab ] . items . length ) % images [ galleryTab ] . items . length )
52+ } )
53+ } ;
54+
55+
56+ useEffect ( ( ) => {
57+ document . addEventListener ( "keydown" , handleKeyboardInput , false ) ;
58+
59+ return ( ) => {
60+ document . removeEventListener ( "keydown" , handleKeyboardInput , false ) ;
61+ } ;
62+ } , [ handleKeyboardInput ( ) ] ) ;
63+
3164 function setNewImage ( index ) {
32- scrollImageIntoView ( index )
3365 setSelectedImage ( index )
3466 }
67+
68+ useEffect ( ( ) => {
69+ scrollImageIntoView ( selectedImage )
70+ } , [ selectedImage ] ) ;
71+
3572 function scrollImageIntoView ( index ) {
3673 const element = document . getElementById ( "img-" + index ) ;
3774 if ( ! element ) {
38- console . error ( "could not find element" , "img-" + index , element )
75+ console . warn ( "could not find element" , "img-" + index , element )
3976 return
4077 }
4178 element . scrollIntoView ( { behavior : 'smooth' , block : 'center' , inline : 'center' } )
42-
4379 }
4480
45- return (
81+ return (
4682 < Dialog
4783 fullScreen
4884 open = { props . isOpen }
4985 onClose = { props . onClose }
5086 TransitionComponent = { Transition }
5187 >
52- < Box sx = { { flexDirection : "column" , height : "100vh !important" , width : "100vw" } } >
88+ < Box sx = { { flexDirection : "column" , height : "100vh !important" , width : "100vw" } }
89+ onKeyDown = { ( e ) => console . log ( "KEYDOWN" , e ) } >
5390 < AppBar sx = { { position : 'absolute' } } >
5491 < Toolbar >
5592 < Typography sx = { { ml : 2 , flex : 1 } } variant = "h6" component = "div" >
@@ -155,15 +192,15 @@ export function GalleryDialog(props) {
155192 sx = { {
156193 background : "rgba(19,16,27,0.4)" ,
157194 } }
158- onClick = { ( ) => setNewImage ( ( ( ( selectedImage - 1 ) % images [ galleryTab ] . items . length ) + images [ galleryTab ] . items . length ) % images [ galleryTab ] . items . length ) }
195+ onClick = { previousImage }
159196 >
160197 < KeyboardArrowLeft fontSize = { "large" } sx = { { color : "#fff" } } />
161198 </ IconButton >
162199 < IconButton
163200 sx = { {
164201 background : "rgba(19,16,27,0.4)" ,
165202 } }
166- onClick = { ( ) => setNewImage ( ( selectedImage + 1 ) % images [ galleryTab ] . items . length ) }
203+ onClick = { nextImage }
167204 >
168205 < KeyboardArrowRight fontSize = { "large" } sx = { { color : "#fff" } } />
169206 </ IconButton >
0 commit comments