@@ -5,6 +5,8 @@ let images = []
55let currentImage = null
66let currentIndexIndex = 0
77
8+ let hideContainer
9+
810function findOrCreateLightboxContainer ( ) {
911 const lightboxContainerSelector = '.lightbox-container'
1012
@@ -24,8 +26,7 @@ function findOrCreateLightboxContainer () {
2426
2527 addImageZoomListener ( lightBoxContainer )
2628
27- const hideContainer = ( e ) => {
28- e . stopPropagation ( )
29+ hideContainer = ( ) => {
2930 lightBoxContainer . classList . remove ( 'show' )
3031 document . body . classList . remove ( 'no-scroll' )
3132 currentImage = null
@@ -39,8 +40,14 @@ function findOrCreateLightboxContainer () {
3940 e . stopPropagation ( )
4041 switchImage ( 1 )
4142 } )
42- lightBoxContainer . querySelector ( '.lightbox-control-close' ) . addEventListener ( 'click' , hideContainer )
43- lightBoxContainer . addEventListener ( 'click' , hideContainer )
43+ lightBoxContainer . querySelector ( '.lightbox-control-close' ) . addEventListener ( 'click' , ( e ) => {
44+ e . stopPropagation ( )
45+ hideContainer ( )
46+ } )
47+ lightBoxContainer . addEventListener ( 'click' , ( e ) => {
48+ e . stopPropagation ( )
49+ hideContainer ( )
50+ } )
4451
4552 document . body . appendChild ( lightBoxContainer )
4653 }
@@ -172,6 +179,25 @@ const init = () => {
172179 e . stopPropagation ( )
173180 }
174181 } )
182+
183+ window . addEventListener ( 'keydown' , function ( e ) {
184+ if ( ! currentImage ) {
185+ return
186+ }
187+
188+ if ( e . key === 'ArrowRight' ) {
189+ switchImage ( 1 )
190+ e . stopPropagation ( )
191+ } else if ( e . key === 'ArrowLeft' ) {
192+ switchImage ( - 1 )
193+ e . stopPropagation ( )
194+ } else if ( e . key === 'Escape' ) {
195+ if ( hideContainer ) {
196+ hideContainer ( )
197+ e . stopPropagation ( )
198+ }
199+ }
200+ } )
175201}
176202
177203init ( )
0 commit comments