Skip to content

Commit fcb1341

Browse files
authored
fix: cleanup Modal timeout (#193)
1 parent baad7b4 commit fcb1341

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/components/Modal/Modal.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState, ReactNode, useContext } from 'react';
1+
import React, { useEffect, useState, ReactNode, useContext, useRef } from 'react';
22
import { createGlobalStyle } from 'styled-components';
33
import { WidthProps } from 'styled-system';
44
import { useIsEscKeyPressed } from '../../utils/hooks/useIsEscKeyPressed';
@@ -56,12 +56,13 @@ const ANIMATION_DURATION = Math.max(DIMMING_ANIMATION_DURATION, CARD_ANIMATION_D
5656
const Modal: React.FC<ModalProps> = ({ children, onClose, dismissible, ...rest }: ModalProps) => {
5757
const [visible, setVisible] = useState(true);
5858
const isEscKeyPressed = useIsEscKeyPressed();
59+
const closeTimeout = useRef(null);
5960

6061
const handleClose: DismissFunc = () => {
6162
setVisible(false);
6263

6364
if (onClose) {
64-
setTimeout(() => onClose(), ANIMATION_DURATION);
65+
closeTimeout.current = setTimeout(() => onClose(), ANIMATION_DURATION);
6566
}
6667
};
6768

@@ -77,6 +78,13 @@ const Modal: React.FC<ModalProps> = ({ children, onClose, dismissible, ...rest }
7778
}
7879
}, [dismissible, isEscKeyPressed]);
7980

81+
useEffect(
82+
() => () => {
83+
if (closeTimeout.current) clearTimeout(closeTimeout.current);
84+
},
85+
[]
86+
);
87+
8088
const renderChildren = () => {
8189
if (typeof children === 'function') {
8290
return children(handleClose);

0 commit comments

Comments
 (0)