From 996dba759513c81ab57ee6167bb840340a468498 Mon Sep 17 00:00:00 2001 From: Ollie Hsieh Date: Thu, 27 Apr 2023 07:30:47 -0700 Subject: [PATCH] Close lightbox on web with escape key (#543) * Close lightbox on web with escape key * Lint --- src/view/com/lightbox/Lightbox.web.tsx | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/view/com/lightbox/Lightbox.web.tsx b/src/view/com/lightbox/Lightbox.web.tsx index f10548351e..c17356d943 100644 --- a/src/view/com/lightbox/Lightbox.web.tsx +++ b/src/view/com/lightbox/Lightbox.web.tsx @@ -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 (