Skip to content

Commit 82253f4

Browse files
committed
Support keyboard navigation
Signed-off-by: Yukai Huang <[email protected]>
1 parent c22ce8d commit 82253f4

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

public/js/lib/renderer/lightbox/index.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ let images = []
55
let currentImage = null
66
let currentIndexIndex = 0
77

8+
let hideContainer
9+
810
function 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

177203
init()

0 commit comments

Comments
 (0)