Skip to content

Commit bc37bd6

Browse files
committed
fix(ui-motion): use elementRef instead of ref in BaseTransition renderChildren
Replace the deprecated ref callback with elementRef and remove the now-redundant findDOMNode wrapper, since elementRef receives the DOM element directly from InstUI children. renderChildren now distinguishes between HTML elements and components: HTML elements receive a ref callback (which gives the DOM element directly), while components receive elementRef (the InstUI convention). This removes findDOMNode without breaking non-InstUI children like plain <div> or <span>. The test's ExampleComponent was updated from a class component to a functional component that supports elementRef.
1 parent 5e7a355 commit bc37bd6

2 files changed

Lines changed: 25 additions & 25 deletions

File tree

packages/ui-motion/src/Transition/BaseTransition/index.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
import { Component, ReactElement } from 'react'
2626

27-
import { getClassList, findDOMNode } from '@instructure/ui-dom-utils'
27+
import { getClassList } from '@instructure/ui-dom-utils'
2828
import {
2929
ensureSingleChild,
3030
safeCloneElement
@@ -333,19 +333,23 @@ class BaseTransition extends Component<
333333
}
334334

335335
renderChildren() {
336-
return this.props.children
337-
? safeCloneElement(
338-
ensureSingleChild(this.props.children) as ReactElement,
339-
{
340-
'aria-hidden': !this.props.in ? true : undefined,
341-
ref: (el: React.ReactInstance | null) => {
342-
const ref = (findDOMNode(el) as Element) || null
343-
344-
this.handleRef(ref)
336+
if (!this.props.children) return null
337+
338+
const child = ensureSingleChild(this.props.children) as ReactElement
339+
const isInstUIComponent = !!(child.type as any)?.componentId
340+
341+
return safeCloneElement(child, {
342+
'aria-hidden': !this.props.in ? true : undefined,
343+
...(isInstUIComponent
344+
? { elementRef: this.handleRef }
345+
: {
346+
ref: (el: unknown) => {
347+
if (el instanceof Element) {
348+
this.handleRef(el)
349+
}
345350
}
346-
}
347-
)
348-
: null
351+
})
352+
})
349353
}
350354

351355
render() {

packages/ui-motion/src/Transition/__tests__/Transition.test.tsx

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* SOFTWARE.
2323
*/
2424

25-
import { Component, createRef, RefObject } from 'react'
25+
import { forwardRef } from 'react'
2626
import {
2727
render,
2828
waitFor,
@@ -47,17 +47,13 @@ const getClass = (
4747
return classNames[phase]
4848
}
4949

50-
class ExampleComponent extends Component<any, any> {
51-
private ref: RefObject<any>
52-
53-
constructor(props: any) {
54-
super(props)
55-
this.ref = createRef()
56-
}
57-
render() {
58-
return <div ref={this.ref}>{COMPONENT_TEXT}</div>
59-
}
60-
}
50+
const ExampleComponent = forwardRef<HTMLDivElement>((props, ref) => {
51+
return (
52+
<div ref={ref} {...props}>
53+
{COMPONENT_TEXT}
54+
</div>
55+
)
56+
})
6157

6258
describe('<Transition />', () => {
6359
let consoleWarningMock: ReturnType<typeof vi.spyOn>

0 commit comments

Comments
 (0)