11/** @jsx jsx */
22/** @jsxRuntime classic */
33import { jsx , css , keyframes } from '@emotion/react' ;
4- import { ReactPortal , MouseEvent } from 'react' ;
4+ import { ReactPortal , MouseEvent , useRef , useEffect } from 'react' ;
55import ReactDOM from 'react-dom' ;
66
77import { useRecoilState , useRecoilValue } from 'recoil' ;
@@ -128,6 +128,7 @@ function BlockModal(): JSX.Element {
128128 setFocus,
129129 } ,
130130 ] = useManager ( focusId ) ;
131+ const modalEL = useRef < HTMLDivElement > ( ) ;
131132
132133 const createBlockHandler = async ( type : string ) => {
133134 startTransaction ( ) ;
@@ -147,9 +148,21 @@ function BlockModal(): JSX.Element {
147148 setModal ( { ...modal , isOpen : false } ) ;
148149 } ;
149150
151+ const handleClickOutside = ( { target } : any ) => {
152+ if ( modal . isOpen && ! modalEL . current . contains ( target ) )
153+ setModal ( { ...modal , isOpen : false } ) ;
154+ } ;
155+
156+ useEffect ( ( ) => {
157+ window . addEventListener ( 'click' , handleClickOutside ) ;
158+ return ( ) => {
159+ window . removeEventListener ( 'click' , handleClickOutside ) ;
160+ } ;
161+ } , [ ] ) ;
162+
150163 return (
151164 < ModalPortal >
152- < div css = { modalWrapperCss ( modal . left , modal . top ) } >
165+ < div css = { modalWrapperCss ( modal . left , modal . top ) } ref = { modalEL } >
153166 { Object . keys ( typeName ) . map ( ( type ) => (
154167 < div
155168 key = { type }
0 commit comments