Skip to content

Commit

Permalink
fix #559
Browse files Browse the repository at this point in the history
  • Loading branch information
fkhadra committed Jan 27, 2021
1 parent 02562b4 commit 8f8fe5d
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/utils/cssTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,6 +72,7 @@ export function cssTransition({
const enterClassName = appendPosition ? `${enter}--${position}` : enter;
const exitClassName = appendPosition ? `${exit}--${position}` : exit;
const baseClassName = useRef<string>();
const animationStep = useRef(AnimationStep.Enter);

useLayoutEffect(() => {
onEnter();
Expand All @@ -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}</>;
};
Expand Down

0 comments on commit 8f8fe5d

Please sign in to comment.