Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DOM] Handle blur on Fragments below Document - #37062

Merged
eps1lon merged 2 commits into
mainfrom
sebbie/fragment-ref-blur-document
Jul 21, 2026
Merged

[DOM] Handle blur on Fragments below Document#37062
eps1lon merged 2 commits into
mainfrom
sebbie/fragment-ref-blur-document

Conversation

@eps1lon

@eps1lon eps1lon commented Jul 19, 2026

Copy link
Copy Markdown
Collaborator

When we blur, we check first if the active element is even within the Fragment to avoid having to traverse the whole subtree.

However, assuming ownerDocument returns a Document is incorrect for the cases where the Node is a Document. Then the ownerDocument is null. We need to handle that case to avoid Cannot read properties of null (reading 'activeElement').

@meta-cla meta-cla Bot added the CLA Signed label Jul 19, 2026
@github-actions github-actions Bot added the React Core Team Opened by a member of the React Core Team label Jul 19, 2026
@eps1lon
eps1lon force-pushed the sebbie/fragment-ref-blur-document branch 2 times, most recently from fb42f26 to 8dbbc6b Compare July 19, 2026 10:57
@eps1lon eps1lon changed the title [Fiber] Handle blur on Fragments below Document [DOM] Handle blur on Fragments below Document Jul 19, 2026
@eps1lon
eps1lon force-pushed the sebbie/fragment-ref-blur-document branch 2 times, most recently from 13280bf to c31a171 Compare July 19, 2026 12:08
@eps1lon eps1lon changed the title [DOM] Handle blur on Fragments below Document [DOM] Handle blur on Fragments below Document Jul 19, 2026
@eps1lon
eps1lon requested a review from jackpope July 19, 2026 12:15
@eps1lon
eps1lon marked this pull request as ready for review July 19, 2026 12:15
@eps1lon
eps1lon force-pushed the sebbie/fragment-ref-blur-document branch from c1c16cb to 3e44489 Compare July 19, 2026 12:21

@daltino daltino left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR fixes handling blur events on Fragments below the Document by properly accounting for the document context via getOwnerDocumentFromRootContainer. The changes are minimal, coherent, and backed by a new test case to ensure the behavior is correct. Everything aligns with the contribution guidelines and seems ready to merge.

@eps1lon
eps1lon disabled the stack merge July 19, 2026 19:40
@eps1lon
eps1lon force-pushed the sebbie/fragment-ref-blur-document branch from 3e44489 to ed407f7 Compare July 19, 2026 19:42
Base automatically changed from sebbie/fragment-ref-scroll-container to main July 20, 2026 20:41
eps1lon and others added 2 commits July 20, 2026 22:44
… a Document

When a FragmentInstance's nearest host parent fiber is the HostRoot,
`getInstanceFromHostFiber` returns the root's `Container`. `blur()`
reads `parentHostInstance.ownerDocument.activeElement` from it, but a
`Document` container's `ownerDocument` is null, so the read throws.

The test mounts the fragment above the `<html>` element of a root
created on the document, where the nearest host parent is always the
HostRoot. This keeps the coverage intact even once HostSingleton fibers
are treated as host parents, which will stop fragments mounted under
`<body>` from falling back to the root.

The test pins down the current throwing behavior. The fix in the next
commit flips it to asserting that the focused child inside the fragment
actually loses focus.

Co-Authored-By: Claude Fable 5 <[email protected]>
When a FragmentInstance's nearest host parent fiber is the HostRoot,
`blur()` read `parentInstanceOrContainer.ownerDocument.activeElement`
from the root's `Container`. A `Document` container's `ownerDocument` is
null, so the read threw for fragments whose nearest host parent resolves
to a root mounted on the document.

The owner document is now resolved with the existing
`getOwnerDocumentFromRootContainer` helper, which returns the container
itself when it is a `Document`. The `contains(activeElement)` check and
the rest of the flow already work on a `Document`.

The test from the previous commit flips from asserting the throw to
asserting that the focused child inside the fragment loses focus.

Co-Authored-By: Claude Fable 5 <[email protected]>
@eps1lon
eps1lon force-pushed the sebbie/fragment-ref-blur-document branch from ed407f7 to 54b6d91 Compare July 20, 2026 20:44
@react-sizebot

Copy link
Copy Markdown

Comparing: fa5212c...54b6d91

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.js = 7.19 kB 7.19 kB +0.10% 1.91 kB 1.91 kB
oss-stable/react-dom/cjs/react-dom-client.production.js = 615.12 kB 615.14 kB = 108.78 kB 108.77 kB
oss-experimental/react-dom/cjs/react-dom.production.js = 7.19 kB 7.19 kB +0.10% 1.91 kB 1.91 kB
oss-experimental/react-dom/cjs/react-dom-client.production.js = 686.30 kB 686.33 kB = 120.26 kB 120.26 kB
facebook-www/ReactDOM-prod.classic.js = 706.23 kB 706.26 kB = 123.69 kB 123.69 kB
facebook-www/ReactDOM-prod.modern.js = 696.55 kB 696.58 kB = 122.09 kB 122.08 kB

Significant size changes

Includes any change greater than 0.2%:

(No significant changes)

Generated by 🚫 dangerJS against 54b6d91

@eps1lon
eps1lon requested a review from jackpope July 20, 2026 21:54
@eps1lon
eps1lon merged commit 862e275 into main Jul 21, 2026
238 checks passed
github-actions Bot pushed a commit that referenced this pull request Jul 21, 2026
Co-authored-by: Claude Fable 5 <[email protected]>

DiffTrain build for [862e275](862e275)
github-actions Bot pushed a commit that referenced this pull request Jul 21, 2026
Co-authored-by: Claude Fable 5 <[email protected]>

DiffTrain build for [862e275](862e275)
github-actions Bot pushed a commit to HaroldHuanrongLIU/react that referenced this pull request Jul 21, 2026
github-actions Bot pushed a commit to HaroldHuanrongLIU/react that referenced this pull request Jul 21, 2026
github-actions Bot pushed a commit to code/lib-react that referenced this pull request Jul 25, 2026
github-actions Bot pushed a commit to code/lib-react that referenced this pull request Jul 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed React Core Team Opened by a member of the React Core Team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants