From 8f8fe5dc9fc7eccae59169cf4694ddcb03dbaf46 Mon Sep 17 00:00:00 2001 From: Fadi Date: Wed, 27 Jan 2021 11:05:48 +0100 Subject: [PATCH] fix #559 --- src/utils/cssTransition.tsx | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/utils/cssTransition.tsx b/src/utils/cssTransition.tsx index 3a6b060c..9b5ee5b7 100644 --- a/src/utils/cssTransition.tsx +++ b/src/utils/cssTransition.tsx @@ -36,6 +36,11 @@ export interface CSSTransitionProps { collapseDuration?: number; } +const enum AnimationStep { + Enter, + Exit +} + /** * Css animation that just work. * You could use animate.css for instance @@ -67,6 +72,7 @@ export function cssTransition({ const enterClassName = appendPosition ? `${enter}--${position}` : enter; const exitClassName = appendPosition ? `${exit}--${position}` : exit; const baseClassName = useRef(); + const animationStep = useRef(AnimationStep.Enter); useLayoutEffect(() => { onEnter(); @@ -76,32 +82,35 @@ export function cssTransition({ if (!isIn) preventExitTransition ? onExited() : onExit(); }, [isIn]); - const onEnter = () => { + function onEnter() { const node = nodeRef.current!; baseClassName.current = node.className; node.className += ` ${enterClassName}`; node.addEventListener('animationend', onEntered); - }; + } - const onEntered = () => { + function onEntered() { const node = nodeRef.current!; node.removeEventListener('animationend', onEntered); - node.className = baseClassName.current!; - }; + if (animationStep.current === AnimationStep.Enter) { + node.className = baseClassName.current!; + } + } - const onExit = () => { + function onExit() { + animationStep.current = AnimationStep.Exit; const node = nodeRef.current!; node.className += ` ${exitClassName}`; node.addEventListener('animationend', onExited); - }; + } - const onExited = () => { + function onExited() { const node = nodeRef.current!; node.removeEventListener('animationend', onExited); collapse ? collapseToast(node, done, collapseDuration) : done(); - }; + } return <>{children}; };