2022-09-29 10:26:34 +00:00
|
|
|
import React, { ReactElement, useEffect, useState } from 'react';
|
|
|
|
|
|
|
|
export const DialogTransitionContext = React.createContext({ isUnmounting: false });
|
|
|
|
|
2022-11-17 22:17:43 +00:00
|
|
|
type TimeoutType = ReturnType<typeof setTimeout>;
|
|
|
|
|
2022-09-29 10:26:34 +00:00
|
|
|
const DialogTransition = ({ children }: { children: ReactElement | null; }) => {
|
|
|
|
const [ childrenToRender, setChildrenToRender ] = useState(children);
|
|
|
|
const [ isUnmounting, setIsUnmounting ] = useState(false);
|
2022-11-17 22:17:43 +00:00
|
|
|
const [ timeoutID, setTimeoutID ] = useState<TimeoutType | undefined>(undefined);
|
2022-09-29 10:26:34 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (children === null) {
|
|
|
|
setIsUnmounting(true);
|
2022-11-17 22:17:43 +00:00
|
|
|
if (typeof timeoutID === 'undefined') {
|
|
|
|
setTimeoutID(setTimeout(() => {
|
|
|
|
setChildrenToRender(children);
|
|
|
|
setIsUnmounting(false);
|
|
|
|
setTimeoutID(undefined);
|
|
|
|
}, 150));
|
|
|
|
}
|
2022-09-29 10:26:34 +00:00
|
|
|
} else {
|
2022-11-17 22:17:43 +00:00
|
|
|
if (typeof timeoutID !== 'undefined') {
|
|
|
|
clearTimeout(timeoutID);
|
|
|
|
setTimeoutID(undefined);
|
|
|
|
setIsUnmounting(false);
|
|
|
|
}
|
2022-09-29 10:26:34 +00:00
|
|
|
setChildrenToRender(children);
|
|
|
|
}
|
|
|
|
}, [ children ]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<DialogTransitionContext.Provider value = {{ isUnmounting }}>
|
|
|
|
{childrenToRender}
|
|
|
|
</DialogTransitionContext.Provider>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default DialogTransition;
|