Close lightbox on web with escape key (#543)

* Close lightbox on web with escape key

* Lint
zio/stable
Ollie Hsieh 2023-04-27 07:30:47 -07:00 committed by GitHub
parent afd87a6390
commit 996dba7595
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 1 deletions

View File

@ -1,4 +1,4 @@
import React from 'react'
import React, {useCallback, useEffect} from 'react'
import {
Image,
TouchableOpacity,
@ -73,6 +73,20 @@ function LightboxInner({
}
}
const onEscape = useCallback(
(e: KeyboardEvent) => {
if (e.key === 'Escape') {
onClose()
}
},
[onClose],
)
useEffect(() => {
window.addEventListener('keydown', onEscape)
return () => window.removeEventListener('keydown', onEscape)
}, [onEscape])
return (
<View style={styles.mask}>
<TouchableWithoutFeedback onPress={onClose}>