Skip to content

Commit

Permalink
fix: Motion should not unmount node
Browse files Browse the repository at this point in the history
  • Loading branch information
zombieJ committed Sep 27, 2020
1 parent af8c89e commit 42f54bc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
18 changes: 16 additions & 2 deletions examples/CSSMotion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ async function forceDelay(): Promise<void> {
});
}

const Div = React.forwardRef<HTMLDivElement, any>((props, ref) => {
React.useEffect(() => {
console.log('DIV >>> Mounted!');

return () => {
console.log('DIV >>> UnMounted!');
};
}, []);

return <div {...props} ref={ref} />;
});

class Demo extends React.Component<{}, DemoState> {
state: DemoState = {
show: true,
Expand All @@ -27,7 +39,9 @@ class Demo extends React.Component<{}, DemoState> {
};

onTrigger = () => {
this.setState(({ show }) => ({ show: !show }));
setTimeout(() => {
this.setState(({ show }) => ({ show: !show }));
}, 100);
};

onTriggerDelay = () => {
Expand Down Expand Up @@ -128,7 +142,7 @@ class Demo extends React.Component<{}, DemoState> {
onLeaveEnd={this.skipColorTransition}
>
{({ style, className }, ref) => (
<div
<Div
ref={ref}
className={classNames('demo-block', className)}
style={style}
Expand Down
4 changes: 2 additions & 2 deletions src/CSSMotion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export function genCSSMotion(
}
}

const [status, statusStep, statusStyle] = useStatus(
const [status, statusStep, statusStyle, mergedVisible] = useStatus(
supportMotion,
visible,
getDomElement,
Expand All @@ -159,7 +159,7 @@ export function genCSSMotion(
motionChildren = null;
} else if (status === STATUS_NONE || !isSupportTransition(props)) {
// Stable children
if (visible) {
if (mergedVisible) {
motionChildren = children({ ...eventProps }, setNodeRef);
} else if (!removeOnLeave) {
motionChildren = children(
Expand Down
8 changes: 6 additions & 2 deletions src/hooks/useStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export default function useStatus(
onEnterEnd,
onLeaveEnd,
}: CSSMotionProps,
): [MotionStatus, StepStatus, React.CSSProperties] {
): [MotionStatus, StepStatus, React.CSSProperties, boolean] {
// Used for outer render usage to avoid `visible: false & status: none` to render nothing
const [asyncVisible, setAsyncVisible] = useState(visible);
const [status, setStatus] = useState<MotionStatus>(STATUS_NONE);
const [style, setStyle] = useState<React.CSSProperties | undefined>(null);

Expand Down Expand Up @@ -192,6 +194,8 @@ export default function useStatus(
setStatus(nextStatus);
startStep();
}

setAsyncVisible(visible);
}, [visible]);

// ============================ Effect ============================
Expand Down Expand Up @@ -225,5 +229,5 @@ export default function useStatus(
};
}

return [status, step, mergedStyle];
return [status, step, mergedStyle, asyncVisible];
}

0 comments on commit 42f54bc

Please sign in to comment.