diff --git a/resources/scripts/components/elements/Modal.tsx b/resources/scripts/components/elements/Modal.tsx index b62918050..896a32567 100644 --- a/resources/scripts/components/elements/Modal.tsx +++ b/resources/scripts/components/elements/Modal.tsx @@ -47,20 +47,21 @@ const Modal: React.FC = ({ visible, appear, dismissable, showSpinner return (dismissable || true) && !(showSpinnerOverlay || false); }, [ dismissable, showSpinnerOverlay ]); - const handleEscapeEvent = (e: KeyboardEvent) => { - if (isDismissable && closeOnEscape && e.key === 'Escape') { - setRender(false); - } - }; + useEffect(() => { + if (!isDismissable || !closeOnEscape) return; + + const handler = (e: KeyboardEvent) => { + if (e.key === 'Escape') setRender(false); + }; + + window.addEventListener('keydown', handler); + return () => { + window.removeEventListener('keydown', handler); + }; + }, [ isDismissable, closeOnEscape, render ]); useEffect(() => setRender(visible), [ visible ]); - useEffect(() => { - window.addEventListener('keydown', handleEscapeEvent); - - return () => window.removeEventListener('keydown', handleEscapeEvent); - }, [ render ]); - return ( = ({ visible, appear, dismissable, showSpinner onExited={() => onDismissed()} > { + onClick={e => e.stopPropagation()} + onMouseDown={e => { if (isDismissable && closeOnBackground) { e.stopPropagation(); if (e.target === e.currentTarget) {