Skip to content

Bug - Popper - Cannot read properties of undefined (reading 'documentElement') #12255

@logonoff

Description

@logonoff

Describe the problem
A clear and concise description of the problem. Which components are affected?

We appear to be experiencing this bug floating-ui/floating-ui#2437

claude explanation:

## Description

PatternFly's bundled Popper copy (`@patternfly/react-core/src/helpers/Popper/thirdparty/popper-core`) crashes with `TypeError: can't access property "documentElement" of undefined` when a component using Popper unmounts while its dropdown/popover is open.

The root cause is in `getDocumentElement.ts` — it lacks the `|| window.document` null-safety fallback that upstream `@popperjs/core` has:

PatternFly's copy (crashes): `return (isElement(element) ? element.ownerDocument : element.document).documentElement;`

Upstream `@popperjs/core` (safe): `return ((isElement(element) ? element.ownerDocument : element.document) || window.document).documentElement;`

When a component unmounts while Popper is active, `usePopper`'s layout effect can call `createPopper()` with a reference element that is detached from the DOM. Popper's `listScrollParents()` traverses the broken parent chain, eventually calling `getDocumentElement()` with a value whose `.ownerDocument` / `.document` is nullish, and the missing fallback causes the crash.

A secondary issue is that `usePopper.ts` only null-checks `referenceElement` / `popperElement` but does not check `.isConnected`, so `createPopper()` proceeds with detached DOM nodes.

## Affected components

Any component that uses `@patternfly/react-core`'s internal `Popper` helper, including:
- `Select`
- `Dropdown`
- `Tooltip`
- `Popover`
- `MenuContainer`
- Any custom usage of the `Popper` component

## Steps to reproduce

1. Render a PatternFly `Select` (or any Popper-based component) inside a parent that can be conditionally unmounted
2. Open the dropdown and unmount the parent in the same React event handler (React 18 batches both state updates into a single commit)
3. Observe the crash in the browser console

## Expected behavior

No crash. Popper should handle detached elements gracefully.

## Suggested fix

1. Add the `|| window.document` fallback to `getDocumentElement.ts` to match upstream `@popperjs/core`
2. Add an `element.isConnected` guard in `usePopper.ts` before calling `createPopper()`

How do you reproduce the problem?
Provide steps to reproduce. A codesandbox demonstrating the problem is appreciated.

I don't have any code but in OCP I triggered this by going to Pod details, then quickly both switching in and out of the Logs tab as well as opening the log control dropdowns

Expected behavior
A clear and concise description of the expected behavior.

No crash

Is this issue blocking you?
List the workaround if there is one.

Yes. No workaround aside from patching PatternFly

Screenshots
If applicable, add screenshots to help explain the issue.

Image

What is your environment?

firefox.x86_64 148.0-1.fc43

What is your product and what release date are you targeting?

OCP 4.22 / 4.21 / 4.20. We'd also like a backport for PF 6.2

Any other information?

xref https://issues.redhat.com/browse/OCPBUGS-77204 (RH-internal)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    Status

    Needs triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions