Skip to content

Commit

Permalink
fix: imperative transition sometimes would get stuck never exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jan 25, 2023
1 parent e89ba60 commit 50994f3
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/ImperativeTransition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface TransitionFunctionOptions {
in: boolean;
element: HTMLElement;
initial: boolean;
isStale: () => boolean;
}

export type TransitionHandler = (
Expand All @@ -30,14 +31,20 @@ export function useTransition({

useEffect(() => {
if (!ref.current) {
return;
return undefined;
}

let stale = false;

handleTransition({
in: inProp,
element: ref.current!,
initial: isInitialRef.current,
isStale: () => stale,
});
return () => {
stale = true;
};
}, [inProp, handleTransition]);

useEffect(() => {
Expand Down Expand Up @@ -76,20 +83,29 @@ export default function ImperativeTransition({
}: ImperativeTransitionProps) {
const [exited, setExited] = useState(!inProp);

// TODO: I think this needs to be in an effect
if (inProp && exited) {
setExited(false);
}

const ref = useTransition({
in: !!inProp,
onTransition: (options) => {
const onFinish = () => {
if (options.isStale()) return;

if (options.in) {
setExited(false);
onEntered?.(options.element, options.initial);
} else {
setExited(true);
onExited?.(options.element);
}
};

Promise.resolve(transition(options)).then(onFinish);
Promise.resolve(transition(options)).then(onFinish, (error) => {
if (!options.in) setExited(true);
throw error;
});
},
});

Expand Down

0 comments on commit 50994f3

Please sign in to comment.