Skip to content

Commit

Permalink
Fix crash when using instanceof HTMLElement in some environments (#…
Browse files Browse the repository at this point in the history
…3494)

This PR fixes an issue where in some environments where `HTMLElement` is
not
available (on the server) and AG Grid is used, we crashed.

This happens because the `HTMLElement` is polyfilled to an empty object.
This means that the `typeof HTMLElement !== 'undefined'` check passed,
but the `v instanceof HTMLElement` translated to `v instanceof {}` which
is invalid and resulted in a crash...

This PR solves it by checking for exactly what we need, in this case
whether the `outerHTML` property is available.

Alternatively, we could use `return v?.outerHTML ?? v`, but not sure if
that's always safe to do.

Fixes: #3471
  • Loading branch information
RobinMalfait authored Sep 27, 2024
1 parent f2c80c4 commit 63daa86
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/@headlessui-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Ensure `Element` is available before polyfilling to prevent crashes in non-browser environments ([#3493](https://github.com/tailwindlabs/headlessui/pull/3493))
- Fix crash when using `instanceof HTMLElement` in some environments ([#3494](https://github.com/tailwindlabs/headlessui/pull/3494))

## [2.1.8] - 2024-09-12

Expand Down
19 changes: 8 additions & 11 deletions packages/@headlessui-react/src/internal/floating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,17 +133,14 @@ export function useFloatingPanel(
let stablePlacement = useMemo(
() => placement,
[
JSON.stringify(
placement,
typeof HTMLElement !== 'undefined'
? (_, v) => {
if (v instanceof HTMLElement) {
return v.outerHTML
}
return v
}
: undefined
),
JSON.stringify(placement, (_, v) => {
// When we are trying to stringify a DOM element, we want to return the
// `outerHTML` of the element. In all other cases, we want to return the
// value as-is.
// It's not safe enough to check whether `v` is an instanceof
// `HTMLElement` because some tools (like AG Grid) polyfill it to be `{}`.
return v?.outerHTML ?? v
}),
]
)
useIsoMorphicEffect(() => {
Expand Down

0 comments on commit 63daa86

Please sign in to comment.