Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/hooks/useNextFrame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export default (): [

function cancelNextFrame() {
raf.cancel(nextFrameRef.current);
// A frame may already be waiting for an asynchronous prepare callback.
// Invalidate it as well, so resolving after cleanup cannot advance the queue.
nextFrameRef.current = null;
}

function nextFrame(
Expand Down
32 changes: 23 additions & 9 deletions src/hooks/useStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export default function useStatus(

const currentStatus = getStatus();

// This tracks the component instance, not whether its effects are connected.
const mountedRef = useRef(false);
const deadlineRef = useRef(null);

Expand Down Expand Up @@ -201,16 +202,21 @@ export default function useStatus(

// ============================ Status ============================
const visibleRef = useRef<boolean | null>(null);
const pendingVisibleChangeRef = useRef(false);

// Update with new status
useIsomorphicLayoutEffect(() => {
// When use Suspense, the `visible` will repeat trigger,
// But not real change of the `visible`, we need to skip it.
// https://github.com/ant-design/ant-design/issues/44379
// Activity and Suspense reconnect effects without creating a new instance.
// Keep settled content intact. Restart interrupted motion on the same node
// so its frame queue, end listeners and deadline can be connected again.
if (mountedRef.current && visibleRef.current === visible) {
if (getStatus() !== STATUS_NONE) {
startStep();
}
return;
}

pendingVisibleChangeRef.current = true;
setAsyncVisible(visible);

const isMounted = mountedRef.current;
Expand Down Expand Up @@ -271,7 +277,6 @@ export default function useStatus(

useEffect(
() => () => {
mountedRef.current = false;
clearTimeout(deadlineRef.current);
},
[],
Expand All @@ -285,14 +290,23 @@ export default function useStatus(
firstMountChangeRef.current = true;
}

if (asyncVisible !== undefined && currentStatus === STATUS_NONE) {
// Consume each visibility transition once. Activity can reconnect this effect
// without a new transition. Read the live status to avoid notifying an old
// render when a layout effect has already started the next motion.
if (
pendingVisibleChangeRef.current &&
asyncVisible !== undefined &&
asyncVisible === visibleRef.current &&
getStatus() === STATUS_NONE
) {
pendingVisibleChangeRef.current = false;
// Skip first render is invisible since it's nothing changed
if (firstMountChangeRef.current || asyncVisible) {
onVisibleChanged?.(asyncVisible);
}
firstMountChangeRef.current = true;
}
}, [asyncVisible, currentStatus]);
}, [asyncVisible, currentStatus, getStatus, onVisibleChanged]);

// ============================ Styles ============================
let mergedStyle = style[0];
Expand All @@ -318,8 +332,8 @@ export default function useStatus(
motionAppear
? 'NONE'
: // Enter or Leave check
step === STEP_START || step === STEP_ACTIVE
? styleStep === step
: true,
step === STEP_START || step === STEP_ACTIVE
? styleStep === step
: true,
];
}
11 changes: 6 additions & 5 deletions src/hooks/useStepQueue.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useState } from '@rc-component/util';
import * as React from 'react';
import type { MotionStatus, StepStatus } from '../interface';
import {
Expand Down Expand Up @@ -37,12 +36,14 @@ export default (
step: StepStatus,
) => Promise<void> | void | typeof SkipStep | typeof DoStep,
): [() => void, StepStatus] => {
const [step, setStep] = useState<StepStatus>(STEP_NONE);
const [step, setStep] = React.useState<StepStatus>(STEP_NONE);

const [nextFrame, cancelNextFrame] = useNextFrame();

function startQueue() {
setStep(STEP_PREPARE, true);
// Effect reconnection runs layout effects before passive effects. A passive
// "destroyed" flag would discard this restart even though the instance lives.
setStep(STEP_PREPARE);
}

const STEP_QUEUE = prepareOnly ? SIMPLE_STEP_QUEUE : FULL_STEP_QUEUE;
Expand All @@ -56,15 +57,15 @@ export default (

if (result === SkipStep) {
// Skip when no needed
setStep(nextStep, true);
setStep(nextStep);
} else if (nextStep) {
// Do as frame for step update
nextFrame(info => {
function doNext() {
// Skip since current queue is ood
if (info.isCanceled()) return;

setStep(nextStep, true);
setStep(nextStep);
}

if (result === true) {
Expand Down
Loading
Loading