Skip to content

Properly cleanup Modal timeout #193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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