Skip to content

Commit c0ece2d

Browse files
committed
[#143] feat: Modal 밖의 영역을 클릭할 시 close
- 모달 생성 시, 밖의 영역을 클릭하는 onClick이벤트 등록
1 parent 0eae244 commit c0ece2d

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

frontend/src/components/molecules/BlockModal/BlockModal.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** @jsx jsx */
22
/** @jsxRuntime classic */
33
import { jsx, css, keyframes } from '@emotion/react';
4-
import { ReactPortal, MouseEvent } from 'react';
4+
import { ReactPortal, MouseEvent, useRef, useEffect } from 'react';
55
import ReactDOM from 'react-dom';
66

77
import { 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

Comments
 (0)