Skip to content

Commit

Permalink
fix: cleanup Modal timeout (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
martimalek authored Nov 17, 2021
1 parent baad7b4 commit fcb1341
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState, ReactNode, useContext } from 'react';
import React, { useEffect, useState, ReactNode, useContext, useRef } from 'react';
import { createGlobalStyle } from 'styled-components';
import { WidthProps } from 'styled-system';
import { useIsEscKeyPressed } from '../../utils/hooks/useIsEscKeyPressed';
Expand Down Expand Up @@ -56,12 +56,13 @@ const ANIMATION_DURATION = Math.max(DIMMING_ANIMATION_DURATION, CARD_ANIMATION_D
const Modal: React.FC<ModalProps> = ({ children, onClose, dismissible, ...rest }: ModalProps) => {
const [visible, setVisible] = useState(true);
const isEscKeyPressed = useIsEscKeyPressed();
const closeTimeout = useRef(null);

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

if (onClose) {
setTimeout(() => onClose(), ANIMATION_DURATION);
closeTimeout.current = setTimeout(() => onClose(), ANIMATION_DURATION);
}
};

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

useEffect(
() => () => {
if (closeTimeout.current) clearTimeout(closeTimeout.current);
},
[]
);

const renderChildren = () => {
if (typeof children === 'function') {
return children(handleClose);
Expand Down

0 comments on commit fcb1341

Please sign in to comment.