Skip to content

Commit 63daa86

Browse files
authored
Fix crash when using instanceof HTMLElement in some environments (#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
1 parent f2c80c4 commit 63daa86

File tree

2 files changed

+9
-11
lines changed

2 files changed

+9
-11
lines changed

packages/@headlessui-react/CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Fixed
1111

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

1415
## [2.1.8] - 2024-09-12
1516

packages/@headlessui-react/src/internal/floating.tsx

+8-11
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,14 @@ export function useFloatingPanel(
133133
let stablePlacement = useMemo(
134134
() => placement,
135135
[
136-
JSON.stringify(
137-
placement,
138-
typeof HTMLElement !== 'undefined'
139-
? (_, v) => {
140-
if (v instanceof HTMLElement) {
141-
return v.outerHTML
142-
}
143-
return v
144-
}
145-
: undefined
146-
),
136+
JSON.stringify(placement, (_, v) => {
137+
// When we are trying to stringify a DOM element, we want to return the
138+
// `outerHTML` of the element. In all other cases, we want to return the
139+
// value as-is.
140+
// It's not safe enough to check whether `v` is an instanceof
141+
// `HTMLElement` because some tools (like AG Grid) polyfill it to be `{}`.
142+
return v?.outerHTML ?? v
143+
}),
147144
]
148145
)
149146
useIsoMorphicEffect(() => {

0 commit comments

Comments
 (0)