Skip to content

Commit

Permalink
access componentInstance.ref in a safe way
Browse files Browse the repository at this point in the history
This makes it work in both React 18 and React 19
  • Loading branch information
RobinMalfait committed Oct 25, 2024
1 parent 7f6218d commit 2bc1cd2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions packages/@headlessui-react/src/utils/render.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import React, {
Fragment,
cloneElement,
createElement,
Expand Down Expand Up @@ -257,7 +257,7 @@ function _render<TTag extends ElementType, TSlot>(
mergedProps,
dataAttributes,
refRelatedProps,
{ ref: mergeRefs((resolvedChildren as any).ref, refRelatedProps.ref) },
{ ref: mergeRefs(getElementRef(resolvedChildren), refRelatedProps.ref) },
classNameProps
)
)
Expand Down Expand Up @@ -460,3 +460,8 @@ function omit<T extends Record<any, any>>(object: T, keysToOmit: string[] = [])
}
return clone
}

function getElementRef(element: React.ReactElement) {
// @ts-expect-error
return React.version.split('.')[0] >= '19' ? element.props.ref : element.ref
}

0 comments on commit 2bc1cd2

Please sign in to comment.