Skip to content

Commit 94fec85

Browse files
committed
fix(modal): remove event listener on clean up
1 parent 56d28bb commit 94fec85

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/dialog/Modal.jsx

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ const ESCAPE_KEYCODE = 27;
66
export function Modal({ title, body, onClose, isOpen, footer, staticBackdrop, scrollable, centered, size, keyboard }) {
77
const modalRef = useRef(null);
88

9-
useEffect(() => {
10-
if (keyboard) {
11-
modalRef.current.addEventListener('keydown', (event) => {
12-
if (event.which === ESCAPE_KEYCODE) {
13-
onClose();
14-
}
15-
});
9+
function closeIfEscape(event) {
10+
if (keyboard && event.which === ESCAPE_KEYCODE) {
11+
onClose();
1612
}
13+
}
14+
15+
useEffect(() => {
16+
modalRef.current.addEventListener('keydown', closeIfEscape);
17+
18+
return () => {
19+
modalRef.current.removeEventListener('keydown', closeIfEscape);
20+
};
1721
}, [keyboard]);
1822

1923
useEffect(() => {

0 commit comments

Comments
 (0)