1
1
/** @jsx jsx */
2
2
/** @jsxRuntime classic */
3
3
import { jsx , css , keyframes } from '@emotion/react' ;
4
- import { ReactPortal , MouseEvent } from 'react' ;
4
+ import { ReactPortal , MouseEvent , useRef , useEffect } from 'react' ;
5
5
import ReactDOM from 'react-dom' ;
6
6
7
7
import { useRecoilState , useRecoilValue } from 'recoil' ;
@@ -128,6 +128,7 @@ function BlockModal(): JSX.Element {
128
128
setFocus,
129
129
} ,
130
130
] = useManager ( focusId ) ;
131
+ const modalEL = useRef < HTMLDivElement > ( ) ;
131
132
132
133
const createBlockHandler = async ( type : string ) => {
133
134
startTransaction ( ) ;
@@ -147,9 +148,21 @@ function BlockModal(): JSX.Element {
147
148
setModal ( { ...modal , isOpen : false } ) ;
148
149
} ;
149
150
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
+
150
163
return (
151
164
< ModalPortal >
152
- < div css = { modalWrapperCss ( modal . left , modal . top ) } >
165
+ < div css = { modalWrapperCss ( modal . left , modal . top ) } ref = { modalEL } >
153
166
{ Object . keys ( typeName ) . map ( ( type ) => (
154
167
< div
155
168
key = { type }
0 commit comments