1
This commit is contained in:
Generated
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
import type { MotionEvent } from '../interface';
|
||||
declare const _default: (onInternalMotionEnd: (event: MotionEvent) => void) => [(element: HTMLElement) => void, (element: HTMLElement) => void];
|
||||
export default _default;
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
import * as React from 'react';
|
||||
import { useRef } from 'react';
|
||||
import { animationEndName, transitionEndName } from "../util/motion";
|
||||
export default (onInternalMotionEnd => {
|
||||
const cacheElementRef = useRef();
|
||||
|
||||
// Remove events
|
||||
function removeMotionEvents(element) {
|
||||
if (element) {
|
||||
element.removeEventListener(transitionEndName, onInternalMotionEnd);
|
||||
element.removeEventListener(animationEndName, onInternalMotionEnd);
|
||||
}
|
||||
}
|
||||
|
||||
// Patch events
|
||||
function patchMotionEvents(element) {
|
||||
if (cacheElementRef.current && cacheElementRef.current !== element) {
|
||||
removeMotionEvents(cacheElementRef.current);
|
||||
}
|
||||
if (element && element !== cacheElementRef.current) {
|
||||
element.addEventListener(transitionEndName, onInternalMotionEnd);
|
||||
element.addEventListener(animationEndName, onInternalMotionEnd);
|
||||
|
||||
// Save as cache in case dom removed trigger by `motionDeadline`
|
||||
cacheElementRef.current = element;
|
||||
}
|
||||
}
|
||||
|
||||
// Clean up when removed
|
||||
React.useEffect(() => () => {
|
||||
removeMotionEvents(cacheElementRef.current);
|
||||
cacheElementRef.current = null;
|
||||
}, []);
|
||||
return [patchMotionEvents, removeMotionEvents];
|
||||
});
|
||||
Generated
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
import { useEffect } from 'react';
|
||||
declare const useIsomorphicLayoutEffect: typeof useEffect;
|
||||
export default useIsomorphicLayoutEffect;
|
||||
Generated
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
import canUseDom from "@rc-component/util/es/Dom/canUseDom";
|
||||
import { useEffect, useLayoutEffect } from 'react';
|
||||
|
||||
// It's safe to use `useLayoutEffect` but the warning is annoying
|
||||
const useIsomorphicLayoutEffect = canUseDom() ? useLayoutEffect : useEffect;
|
||||
export default useIsomorphicLayoutEffect;
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
declare const _default: () => [(callback: (info: {
|
||||
isCanceled: () => boolean;
|
||||
}) => void) => void, () => void];
|
||||
export default _default;
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
import raf from "@rc-component/util/es/raf";
|
||||
import * as React from 'react';
|
||||
export default (() => {
|
||||
const nextFrameRef = React.useRef(null);
|
||||
function cancelNextFrame() {
|
||||
raf.cancel(nextFrameRef.current);
|
||||
}
|
||||
function nextFrame(callback, delay = 2) {
|
||||
cancelNextFrame();
|
||||
const nextFrameId = raf(() => {
|
||||
if (delay <= 1) {
|
||||
callback({
|
||||
isCanceled: () => nextFrameId !== nextFrameRef.current
|
||||
});
|
||||
} else {
|
||||
nextFrame(callback, delay - 1);
|
||||
}
|
||||
});
|
||||
nextFrameRef.current = nextFrameId;
|
||||
}
|
||||
React.useEffect(() => () => {
|
||||
cancelNextFrame();
|
||||
}, []);
|
||||
return [nextFrame, cancelNextFrame];
|
||||
});
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import * as React from 'react';
|
||||
import type { CSSMotionProps } from '../CSSMotion';
|
||||
import type { MotionStatus, StepStatus } from '../interface';
|
||||
export default function useStatus(supportMotion: boolean, visible: boolean, getElement: () => HTMLElement, { motionEnter, motionAppear, motionLeave, motionDeadline, motionLeaveImmediately, onAppearPrepare, onEnterPrepare, onLeavePrepare, onAppearStart, onEnterStart, onLeaveStart, onAppearActive, onEnterActive, onLeaveActive, onAppearEnd, onEnterEnd, onLeaveEnd, onVisibleChanged, }: CSSMotionProps): [
|
||||
status: () => MotionStatus,
|
||||
stepStatus: StepStatus,
|
||||
style: React.CSSProperties,
|
||||
visible: boolean,
|
||||
styleReady: 'NONE' | boolean
|
||||
];
|
||||
+239
@@ -0,0 +1,239 @@
|
||||
import { useEvent } from '@rc-component/util';
|
||||
import useSyncState from "@rc-component/util/es/hooks/useSyncState";
|
||||
import * as React from 'react';
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { STATUS_APPEAR, STATUS_ENTER, STATUS_LEAVE, STATUS_NONE, STEP_ACTIVE, STEP_PREPARE, STEP_PREPARED, STEP_START } from "../interface";
|
||||
import useDomMotionEvents from "./useDomMotionEvents";
|
||||
import useIsomorphicLayoutEffect from "./useIsomorphicLayoutEffect";
|
||||
import useStepQueue, { DoStep, isActive, SkipStep } from "./useStepQueue";
|
||||
export default function useStatus(supportMotion, visible, getElement, {
|
||||
motionEnter = true,
|
||||
motionAppear = true,
|
||||
motionLeave = true,
|
||||
motionDeadline,
|
||||
motionLeaveImmediately,
|
||||
onAppearPrepare,
|
||||
onEnterPrepare,
|
||||
onLeavePrepare,
|
||||
onAppearStart,
|
||||
onEnterStart,
|
||||
onLeaveStart,
|
||||
onAppearActive,
|
||||
onEnterActive,
|
||||
onLeaveActive,
|
||||
onAppearEnd,
|
||||
onEnterEnd,
|
||||
onLeaveEnd,
|
||||
onVisibleChanged
|
||||
}) {
|
||||
// Used for outer render usage to avoid `visible: false & status: none` to render nothing
|
||||
const [asyncVisible, setAsyncVisible] = React.useState();
|
||||
const [getStatus, setStatus] = useSyncState(STATUS_NONE);
|
||||
const [style, setStyle] = React.useState([null, null]);
|
||||
const currentStatus = getStatus();
|
||||
const mountedRef = useRef(false);
|
||||
const deadlineRef = useRef(null);
|
||||
|
||||
// =========================== Dom Node ===========================
|
||||
function getDomElement() {
|
||||
return getElement();
|
||||
}
|
||||
|
||||
// ========================== Motion End ==========================
|
||||
const activeRef = useRef(false);
|
||||
|
||||
/**
|
||||
* Clean up status & style
|
||||
*/
|
||||
function updateMotionEndStatus() {
|
||||
setStatus(STATUS_NONE);
|
||||
setStyle([null, null]);
|
||||
}
|
||||
const onInternalMotionEnd = useEvent(event => {
|
||||
const status = getStatus();
|
||||
// Do nothing since not in any transition status.
|
||||
// This may happen when `motionDeadline` trigger.
|
||||
if (status === STATUS_NONE) {
|
||||
return;
|
||||
}
|
||||
const element = getDomElement();
|
||||
if (event && !event.deadline && event.target !== element) {
|
||||
// event exists
|
||||
// not initiated by deadline
|
||||
// transitionEnd not fired by inner elements
|
||||
return;
|
||||
}
|
||||
const currentActive = activeRef.current;
|
||||
let canEnd;
|
||||
if (status === STATUS_APPEAR && currentActive) {
|
||||
canEnd = onAppearEnd?.(element, event);
|
||||
} else if (status === STATUS_ENTER && currentActive) {
|
||||
canEnd = onEnterEnd?.(element, event);
|
||||
} else if (status === STATUS_LEAVE && currentActive) {
|
||||
canEnd = onLeaveEnd?.(element, event);
|
||||
}
|
||||
|
||||
// Only update status when `canEnd` and not destroyed
|
||||
if (currentActive && canEnd !== false) {
|
||||
updateMotionEndStatus();
|
||||
}
|
||||
});
|
||||
const [patchMotionEvents] = useDomMotionEvents(onInternalMotionEnd);
|
||||
|
||||
// ============================= Step =============================
|
||||
const getEventHandlers = targetStatus => {
|
||||
switch (targetStatus) {
|
||||
case STATUS_APPEAR:
|
||||
return {
|
||||
[STEP_PREPARE]: onAppearPrepare,
|
||||
[STEP_START]: onAppearStart,
|
||||
[STEP_ACTIVE]: onAppearActive
|
||||
};
|
||||
case STATUS_ENTER:
|
||||
return {
|
||||
[STEP_PREPARE]: onEnterPrepare,
|
||||
[STEP_START]: onEnterStart,
|
||||
[STEP_ACTIVE]: onEnterActive
|
||||
};
|
||||
case STATUS_LEAVE:
|
||||
return {
|
||||
[STEP_PREPARE]: onLeavePrepare,
|
||||
[STEP_START]: onLeaveStart,
|
||||
[STEP_ACTIVE]: onLeaveActive
|
||||
};
|
||||
default:
|
||||
return {};
|
||||
}
|
||||
};
|
||||
const eventHandlers = React.useMemo(() => getEventHandlers(currentStatus), [currentStatus]);
|
||||
const [startStep, step] = useStepQueue(currentStatus, !supportMotion, newStep => {
|
||||
// Only prepare step can be skip
|
||||
if (newStep === STEP_PREPARE) {
|
||||
const onPrepare = eventHandlers[STEP_PREPARE];
|
||||
if (!onPrepare) {
|
||||
return SkipStep;
|
||||
}
|
||||
return onPrepare(getDomElement());
|
||||
}
|
||||
|
||||
// Rest step is sync update
|
||||
if (newStep in eventHandlers) {
|
||||
setStyle([eventHandlers[newStep]?.(getDomElement(), null) || null, newStep]);
|
||||
}
|
||||
if (newStep === STEP_ACTIVE && currentStatus !== STATUS_NONE) {
|
||||
// Patch events when motion needed
|
||||
patchMotionEvents(getDomElement());
|
||||
if (motionDeadline > 0) {
|
||||
clearTimeout(deadlineRef.current);
|
||||
deadlineRef.current = setTimeout(() => {
|
||||
onInternalMotionEnd({
|
||||
deadline: true
|
||||
});
|
||||
}, motionDeadline);
|
||||
}
|
||||
}
|
||||
if (newStep === STEP_PREPARED) {
|
||||
updateMotionEndStatus();
|
||||
}
|
||||
return DoStep;
|
||||
});
|
||||
const active = isActive(step);
|
||||
activeRef.current = active;
|
||||
|
||||
// ============================ Status ============================
|
||||
const visibleRef = useRef(null);
|
||||
|
||||
// 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
|
||||
if (mountedRef.current && visibleRef.current === visible) {
|
||||
return;
|
||||
}
|
||||
setAsyncVisible(visible);
|
||||
const isMounted = mountedRef.current;
|
||||
mountedRef.current = true;
|
||||
|
||||
// if (!supportMotion) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
let nextStatus;
|
||||
|
||||
// Appear
|
||||
if (!isMounted && visible && motionAppear) {
|
||||
nextStatus = STATUS_APPEAR;
|
||||
}
|
||||
|
||||
// Enter
|
||||
if (isMounted && visible && motionEnter) {
|
||||
nextStatus = STATUS_ENTER;
|
||||
}
|
||||
|
||||
// Leave
|
||||
if (isMounted && !visible && motionLeave || !isMounted && motionLeaveImmediately && !visible && motionLeave) {
|
||||
nextStatus = STATUS_LEAVE;
|
||||
}
|
||||
const nextEventHandlers = getEventHandlers(nextStatus);
|
||||
|
||||
// Update to next status
|
||||
if (nextStatus && (supportMotion || nextEventHandlers[STEP_PREPARE])) {
|
||||
setStatus(nextStatus);
|
||||
startStep();
|
||||
} else {
|
||||
// Set back in case no motion but prev status has prepare step
|
||||
setStatus(STATUS_NONE);
|
||||
}
|
||||
visibleRef.current = visible;
|
||||
}, [visible]);
|
||||
|
||||
// ============================ Effect ============================
|
||||
// Reset when motion changed
|
||||
useEffect(() => {
|
||||
if (
|
||||
// Cancel appear
|
||||
currentStatus === STATUS_APPEAR && !motionAppear ||
|
||||
// Cancel enter
|
||||
currentStatus === STATUS_ENTER && !motionEnter ||
|
||||
// Cancel leave
|
||||
currentStatus === STATUS_LEAVE && !motionLeave) {
|
||||
setStatus(STATUS_NONE);
|
||||
}
|
||||
}, [motionAppear, motionEnter, motionLeave]);
|
||||
useEffect(() => () => {
|
||||
mountedRef.current = false;
|
||||
clearTimeout(deadlineRef.current);
|
||||
}, []);
|
||||
|
||||
// Trigger `onVisibleChanged`
|
||||
const firstMountChangeRef = React.useRef(false);
|
||||
useEffect(() => {
|
||||
// [visible & motion not end] => [!visible & motion end] still need trigger onVisibleChanged
|
||||
if (asyncVisible) {
|
||||
firstMountChangeRef.current = true;
|
||||
}
|
||||
if (asyncVisible !== undefined && currentStatus === STATUS_NONE) {
|
||||
// Skip first render is invisible since it's nothing changed
|
||||
if (firstMountChangeRef.current || asyncVisible) {
|
||||
onVisibleChanged?.(asyncVisible);
|
||||
}
|
||||
firstMountChangeRef.current = true;
|
||||
}
|
||||
}, [asyncVisible, currentStatus]);
|
||||
|
||||
// ============================ Styles ============================
|
||||
let mergedStyle = style[0];
|
||||
if (eventHandlers[STEP_PREPARE] && step === STEP_START) {
|
||||
mergedStyle = {
|
||||
transition: 'none',
|
||||
...mergedStyle
|
||||
};
|
||||
}
|
||||
const styleStep = style[1];
|
||||
return [getStatus, step, mergedStyle, asyncVisible ?? visible,
|
||||
// Appear Check
|
||||
!mountedRef.current && currentStatus === STATUS_NONE && supportMotion && motionAppear ? 'NONE' :
|
||||
// Enter or Leave check
|
||||
step === STEP_START || step === STEP_ACTIVE ? styleStep === step : true];
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import type { MotionStatus, StepStatus } from '../interface';
|
||||
/** Skip current step */
|
||||
export declare const SkipStep: false;
|
||||
/** Current step should be update in */
|
||||
export declare const DoStep: true;
|
||||
export declare function isActive(step: StepStatus): boolean;
|
||||
declare const _default: (status: MotionStatus, prepareOnly: boolean, callback: (step: StepStatus) => Promise<void> | void | typeof SkipStep | typeof DoStep) => [() => void, StepStatus];
|
||||
export default _default;
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
import useState from "@rc-component/util/es/hooks/useState";
|
||||
import * as React from 'react';
|
||||
import { STEP_ACTIVATED, STEP_ACTIVE, STEP_NONE, STEP_PREPARE, STEP_PREPARED, STEP_START } from "../interface";
|
||||
import useIsomorphicLayoutEffect from "./useIsomorphicLayoutEffect";
|
||||
import useNextFrame from "./useNextFrame";
|
||||
const FULL_STEP_QUEUE = [STEP_PREPARE, STEP_START, STEP_ACTIVE, STEP_ACTIVATED];
|
||||
const SIMPLE_STEP_QUEUE = [STEP_PREPARE, STEP_PREPARED];
|
||||
|
||||
/** Skip current step */
|
||||
export const SkipStep = false;
|
||||
/** Current step should be update in */
|
||||
export const DoStep = true;
|
||||
export function isActive(step) {
|
||||
return step === STEP_ACTIVE || step === STEP_ACTIVATED;
|
||||
}
|
||||
export default ((status, prepareOnly, callback) => {
|
||||
const [step, setStep] = useState(STEP_NONE);
|
||||
const [nextFrame, cancelNextFrame] = useNextFrame();
|
||||
function startQueue() {
|
||||
setStep(STEP_PREPARE, true);
|
||||
}
|
||||
const STEP_QUEUE = prepareOnly ? SIMPLE_STEP_QUEUE : FULL_STEP_QUEUE;
|
||||
useIsomorphicLayoutEffect(() => {
|
||||
if (step !== STEP_NONE && step !== STEP_ACTIVATED) {
|
||||
const index = STEP_QUEUE.indexOf(step);
|
||||
const nextStep = STEP_QUEUE[index + 1];
|
||||
const result = callback(step);
|
||||
if (result === SkipStep) {
|
||||
// Skip when no needed
|
||||
setStep(nextStep, true);
|
||||
} 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);
|
||||
}
|
||||
if (result === true) {
|
||||
doNext();
|
||||
} else {
|
||||
// Only promise should be async
|
||||
Promise.resolve(result).then(doNext);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [status, step]);
|
||||
React.useEffect(() => () => {
|
||||
cancelNextFrame();
|
||||
}, []);
|
||||
return [startQueue, step];
|
||||
});
|
||||
Reference in New Issue
Block a user