Skip to content

Commit c1d0fe6

Browse files
committed
refactor: simplify CSSMotion ref injection
1 parent ddf6bf6 commit c1d0fe6

1 file changed

Lines changed: 18 additions & 25 deletions

File tree

src/CSSMotion.tsx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable react/default-props-match-prop-types, react/no-multi-comp, react/prop-types */
22
import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode';
33
import {
4+
composeRef,
45
getNodeRef,
5-
supportRef,
6-
useComposeRef,
6+
supportNodeRef,
77
} from '@rc-component/util/lib/ref';
88
import { clsx } from 'clsx';
99
import * as React from 'react';
@@ -198,28 +198,28 @@ export function genCSSMotion(config: CSSMotionConfig) {
198198
return null;
199199
}
200200

201-
let motionChildren: React.ReactNode;
201+
let nextMotionChildren: React.ReactNode;
202202
const mergedProps = { ...eventProps, visible };
203203

204204
if (!children) {
205205
// No children
206-
motionChildren = null;
206+
nextMotionChildren = null;
207207
} else if (status === STATUS_NONE) {
208208
// Stable children
209209
if (mergedVisible) {
210-
motionChildren = children({ ...mergedProps }, nodeRef);
210+
nextMotionChildren = children({ ...mergedProps }, nodeRef);
211211
} else if (!removeOnLeave && renderedRef.current && leavedClassName) {
212-
motionChildren = children(
212+
nextMotionChildren = children(
213213
{ ...mergedProps, className: leavedClassName },
214214
nodeRef,
215215
);
216216
} else if (forceRender || (!removeOnLeave && !leavedClassName)) {
217-
motionChildren = children(
217+
nextMotionChildren = children(
218218
{ ...mergedProps, style: { display: 'none' } },
219219
nodeRef,
220220
);
221221
} else {
222-
motionChildren = null;
222+
nextMotionChildren = null;
223223
}
224224
} else {
225225
// In motion
@@ -237,7 +237,7 @@ export function genCSSMotion(config: CSSMotionConfig) {
237237
`${status}-${statusSuffix}`,
238238
);
239239

240-
motionChildren = children(
240+
nextMotionChildren = children(
241241
{
242242
...mergedProps,
243243
className: clsx(getTransitionName(motionName, status), {
@@ -250,24 +250,17 @@ export function genCSSMotion(config: CSSMotionConfig) {
250250
);
251251
}
252252

253-
return motionChildren;
253+
return nextMotionChildren;
254254
}, [idRef.current]) as React.ReactElement;
255255

256-
const canHoldRef =
257-
React.isValidElement(motionChildren) && supportRef(motionChildren);
258-
const originNodeRef = canHoldRef ? getNodeRef(motionChildren) : null;
259-
const shouldInjectRef = canHoldRef && originNodeRef !== nodeRef;
260-
const mergedNodeRef = useComposeRef(
261-
shouldInjectRef ? originNodeRef : null,
262-
nodeRef,
263-
);
264-
265-
// Preserve original behavior when child already uses motion's ref directly.
266-
// Only compose refs when child owns another ref or misses the motion ref.
267-
if (shouldInjectRef) {
268-
return React.cloneElement(motionChildren, {
269-
ref: mergedNodeRef,
270-
});
256+
if (supportNodeRef(motionChildren)) {
257+
const originNodeRef = getNodeRef(motionChildren);
258+
259+
if (originNodeRef !== nodeRef) {
260+
return React.cloneElement(motionChildren, {
261+
ref: composeRef(originNodeRef, nodeRef),
262+
});
263+
}
271264
}
272265

273266
return motionChildren;

0 commit comments

Comments
 (0)