Skip to content

Commit

Permalink
Ensure Element is available before polyfilling (#3493)
Browse files Browse the repository at this point in the history
In some environments `Element` won't be available, which is needed for
the `Element.prototype.getAnimations` polyfill. If `Element` is not
available at all, it means that we are not in a browser so we don't need
the polyfill.

Fixes: #3490
  • Loading branch information
RobinMalfait authored Sep 27, 2024
1 parent 5ca68a9 commit f2c80c4
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 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!
### Fixed

- Ensure `Element` is available before polyfilling to prevent crashes in non-browser environments ([#3493](https://github.com/tailwindlabs/headlessui/pull/3493))

## [2.1.8] - 2024-09-12

Expand Down
3 changes: 2 additions & 1 deletion packages/@headlessui-react/src/hooks/use-transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import { useIsoMorphicEffect } from './use-iso-morphic-effect'
if (
typeof process !== 'undefined' &&
typeof globalThis !== 'undefined' &&
typeof Element !== 'undefined' &&
// Strange string concatenation is on purpose to prevent `esbuild` from
// replacing `process.env.NODE_ENV` with `production` in the build output,
// eliminating this whole branch.
process?.env?.['NODE' + '_' + 'ENV'] === 'test'
) {
if (typeof Element.prototype.getAnimations === 'undefined') {
if (typeof Element?.prototype?.getAnimations === 'undefined') {
Element.prototype.getAnimations = function getAnimationsPolyfill() {
console.warn(
[
Expand Down

0 comments on commit f2c80c4

Please sign in to comment.