Skip to content

Commit e5079de

Browse files
committed
refactor: share ref consume helper
1 parent 9fb85c5 commit e5079de

2 files changed

Lines changed: 28 additions & 18 deletions

File tree

src/CSSMotion.tsx

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export interface CSSMotionState {
110110
prevProps?: CSSMotionProps;
111111
}
112112

113+
export function isRefConsume(children?: CSSMotionProps['children']) {
114+
return children?.length < 2;
115+
}
116+
113117
/**
114118
* `transitionSupport` is used for none transition test case.
115119
* Default we use browser transition event support check.
@@ -193,33 +197,33 @@ export function genCSSMotion(config: CSSMotionConfig) {
193197
}
194198

195199
// We should render children when motionStyle is sync with stepStatus
196-
const motionChildren = React.useMemo(() => {
200+
const returnNode = React.useMemo(() => {
197201
if (styleReady === 'NONE') {
198202
return null;
199203
}
200204

201-
let nextMotionChildren: React.ReactNode;
205+
let motionChildren: React.ReactNode;
202206
const mergedProps = { ...eventProps, visible };
203207

204208
if (!children) {
205209
// No children
206-
nextMotionChildren = null;
210+
motionChildren = null;
207211
} else if (status === STATUS_NONE) {
208212
// Stable children
209213
if (mergedVisible) {
210-
nextMotionChildren = children({ ...mergedProps }, nodeRef);
214+
motionChildren = children({ ...mergedProps }, nodeRef);
211215
} else if (!removeOnLeave && renderedRef.current && leavedClassName) {
212-
nextMotionChildren = children(
216+
motionChildren = children(
213217
{ ...mergedProps, className: leavedClassName },
214218
nodeRef,
215219
);
216220
} else if (forceRender || (!removeOnLeave && !leavedClassName)) {
217-
nextMotionChildren = children(
221+
motionChildren = children(
218222
{ ...mergedProps, style: { display: 'none' } },
219223
nodeRef,
220224
);
221225
} else {
222-
nextMotionChildren = null;
226+
motionChildren = null;
223227
}
224228
} else {
225229
// In motion
@@ -237,7 +241,7 @@ export function genCSSMotion(config: CSSMotionConfig) {
237241
`${status}-${statusSuffix}`,
238242
);
239243

240-
nextMotionChildren = children(
244+
motionChildren = children(
241245
{
242246
...mergedProps,
243247
className: clsx(getTransitionName(motionName, status), {
@@ -250,22 +254,20 @@ export function genCSSMotion(config: CSSMotionConfig) {
250254
);
251255
}
252256

253-
return nextMotionChildren;
257+
return motionChildren;
254258
}, [idRef.current]) as React.ReactElement;
255259

256-
const shouldAutoInjectRef = children?.length < 2;
257-
258-
if (shouldAutoInjectRef && supportNodeRef(motionChildren)) {
259-
const originNodeRef = getNodeRef(motionChildren);
260+
if (isRefConsume(children) && supportNodeRef(returnNode)) {
261+
const originNodeRef = getNodeRef(returnNode);
260262

261263
if (originNodeRef !== nodeRef) {
262-
return React.cloneElement(motionChildren, {
264+
return React.cloneElement(returnNode, {
263265
ref: composeRef(originNodeRef, nodeRef),
264266
});
265267
}
266268
}
267269

268-
return motionChildren;
270+
return returnNode;
269271
},
270272
);
271273

src/CSSMotionList.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint react/prop-types: 0 */
22
import * as React from 'react';
33
import type { CSSMotionProps } from './CSSMotion';
4-
import OriginCSSMotion from './CSSMotion';
4+
import OriginCSSMotion, { isRefConsume } from './CSSMotion';
55
import type { KeyObject } from './util/diff';
66
import {
77
diffKeys,
@@ -59,6 +59,10 @@ export interface CSSMotionListProps
5959
) => React.ReactElement;
6060
}
6161

62+
type ChildrenWithoutRef = (
63+
props: Parameters<CSSMotionListProps['children']>[0],
64+
) => ReturnType<CSSMotionListProps['children']>;
65+
6266
export interface CSSMotionListState {
6367
keyEntities: KeyObject[];
6468
}
@@ -174,8 +178,12 @@ export function genCSSMotionList(
174178
}
175179
}}
176180
>
177-
{children.length < 2
178-
? props => children({ ...props, index }, undefined as any)
181+
{isRefConsume(children)
182+
? props =>
183+
(children as ChildrenWithoutRef)({
184+
...props,
185+
index,
186+
})
179187
: (props, ref) => children({ ...props, index }, ref)}
180188
</CSSMotion>
181189
);

0 commit comments

Comments
 (0)