11/* eslint-disable react/default-props-match-prop-types, react/no-multi-comp, react/prop-types */
2+ import { getDOM } from '@rc-component/util/lib/Dom/findDOMNode' ;
3+ import { getNodeRef , supportRef } from '@rc-component/util/lib/ref' ;
24import classNames from 'classnames' ;
3- import findDOMNode from 'rc-util/lib/Dom/findDOMNode' ;
4- import { fillRef , getNodeRef , supportRef } from 'rc-util/lib/ref' ;
55import * as React from 'react' ;
66import { useRef } from 'react' ;
77import { Context } from './context' ;
8- import DomWrapper from './DomWrapper' ;
98import useStatus from './hooks/useStatus' ;
109import { isActive } from './hooks/useStepQueue' ;
1110import type {
@@ -91,7 +90,7 @@ export interface CSSMotionProps {
9190 style ?: React . CSSProperties ;
9291 [ key : string ] : any ;
9392 } ,
94- ref : ( node : any ) => void ,
93+ ref : React . Ref < any > ,
9594 ) => React . ReactElement ;
9695}
9796
@@ -137,22 +136,9 @@ export function genCSSMotion(config: CSSMotionConfig) {
137136
138137 // Ref to the react node, it may be a HTMLElement
139138 const nodeRef = useRef < any > ( ) ;
140- // Ref to the dom wrapper in case ref can not pass to HTMLElement
141- const wrapperNodeRef = useRef ( ) ;
142139
143140 function getDomElement ( ) {
144- try {
145- // Here we're avoiding call for findDOMNode since it's deprecated
146- // in strict mode. We're calling it only when node ref is not
147- // an instance of DOM HTMLElement. Otherwise use
148- // findDOMNode as a final resort
149- return nodeRef . current instanceof HTMLElement
150- ? nodeRef . current
151- : findDOMNode < HTMLElement > ( wrapperNodeRef . current ) ;
152- } catch ( e ) {
153- // Only happen when `motionDeadline` trigger but element removed.
154- return null ;
155- }
141+ return getDOM ( nodeRef . current ) as HTMLElement ;
156142 }
157143
158144 const [ status , statusStep , statusStyle , mergedVisible ] = useStatus (
@@ -170,13 +156,7 @@ export function genCSSMotion(config: CSSMotionConfig) {
170156 }
171157
172158 // ====================== Refs ======================
173- const setNodeRef = React . useCallback (
174- ( node : any ) => {
175- nodeRef . current = node ;
176- fillRef ( ref , node ) ;
177- } ,
178- [ ref ] ,
179- ) ;
159+ React . useImperativeHandle ( ref , ( ) => getDomElement ( ) ) ;
180160
181161 // ===================== Render =====================
182162 let motionChildren : React . ReactNode ;
@@ -188,16 +168,16 @@ export function genCSSMotion(config: CSSMotionConfig) {
188168 } else if ( status === STATUS_NONE ) {
189169 // Stable children
190170 if ( mergedVisible ) {
191- motionChildren = children ( { ...mergedProps } , setNodeRef ) ;
171+ motionChildren = children ( { ...mergedProps } , nodeRef ) ;
192172 } else if ( ! removeOnLeave && renderedRef . current && leavedClassName ) {
193173 motionChildren = children (
194174 { ...mergedProps , className : leavedClassName } ,
195- setNodeRef ,
175+ nodeRef ,
196176 ) ;
197177 } else if ( forceRender || ( ! removeOnLeave && ! leavedClassName ) ) {
198178 motionChildren = children (
199179 { ...mergedProps , style : { display : 'none' } } ,
200- setNodeRef ,
180+ nodeRef ,
201181 ) ;
202182 } else {
203183 motionChildren = null ;
@@ -227,7 +207,7 @@ export function genCSSMotion(config: CSSMotionConfig) {
227207 } ) ,
228208 style : statusStyle ,
229209 } ,
230- setNodeRef ,
210+ nodeRef ,
231211 ) ;
232212 }
233213
@@ -239,13 +219,13 @@ export function genCSSMotion(config: CSSMotionConfig) {
239219 motionChildren = React . cloneElement (
240220 motionChildren as React . ReactElement ,
241221 {
242- ref : setNodeRef ,
222+ ref : nodeRef ,
243223 } ,
244224 ) ;
245225 }
246226 }
247227
248- return < DomWrapper ref = { wrapperNodeRef } > { motionChildren } </ DomWrapper > ;
228+ return motionChildren as React . ReactElement ;
249229 } ) ;
250230
251231 CSSMotion . displayName = 'CSSMotion' ;
0 commit comments