Skip to content

Commit

Permalink
fix: support React 19 (#1430)
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan authored Oct 22, 2024
1 parent c1b4bcc commit 697f701
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions src/core/primitives/popover/popover.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import {
Middleware,
RootBoundary,
Expand Down Expand Up @@ -359,8 +360,7 @@ export const Popover = memo(
(node: HTMLElement | null) => {
refs.setReference(node)

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const childRef = (childProp as any)?.ref
const childRef = getElementRef(childProp as any)

if (typeof childRef === 'function') {
childRef(node)
Expand Down Expand Up @@ -455,3 +455,29 @@ export const Popover = memo(
}),
)
Popover.displayName = 'Memo(ForwardRef(Popover))'

// Before React 19 accessing `element.props.ref` will throw a warning and suggest using `element.ref`
// After React 19 accessing `element.ref` does the opposite.
// https://github.com/facebook/react/pull/28348
//
// Access the ref using the method that doesn't yield a warning.
function getElementRef(element: React.ReactElement) {
// React <=18 in DEV
let getter = Object.getOwnPropertyDescriptor(element.props, 'ref')?.get
let mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning

if (mayWarn) {
return (element as any).ref
}

// React 19 in DEV
getter = Object.getOwnPropertyDescriptor(element, 'ref')?.get
mayWarn = getter && 'isReactWarning' in getter && getter.isReactWarning

if (mayWarn) {
return element.props.ref
}

// Not DEV
return element.props.ref || (element as any).ref
}

0 comments on commit 697f701

Please sign in to comment.