Skip to content

Commit

Permalink
Add React 19 support (#3543)
Browse files Browse the repository at this point in the history
This PR fixes an issue where `@headlessui/react` was not compatible with
React 19.

We made sure that accessing `ref`s is safe and works in React 18 and
React 19. We also made sure to include React 19 as a valid version in
the peer dependencies. For now, we also allowed the RC versions of React
and React DOM.
  • Loading branch information
RobinMalfait authored Oct 25, 2024
1 parent 5eb3b12 commit 8814b0e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet!
### Added

- Add React 19 support ([#3543](https://github.com/tailwindlabs/headlessui/pull/3543))

## [2.1.10] - 2024-10-10

Expand Down
4 changes: 2 additions & 2 deletions packages/@headlessui-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@
"clean": "rimraf ./dist"
},
"peerDependencies": {
"react": "^18",
"react-dom": "^18"
"react": "^18 || ^19 || ^19.0.0-rc",
"react-dom": "^18 || ^19 || ^19.0.0-rc"
},
"devDependencies": {
"@testing-library/react": "^15.0.7",
Expand Down
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 8814b0e

Please sign in to comment.