Skip to content

Support the IntersectionObserver scrollMargin option (#60153) - #60153

Open
rubennorte wants to merge 1 commit into
react:mainfrom
rubennorte:intersection-observer-scroll-margin
Open

rubennorte wants to merge 1 commit into
react:mainfrom
rubennorte:intersection-observer-scroll-margin

Conversation

@rubennorte

@rubennorte rubennorte commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary:

Note

Stacked on #60152, which this PR includes. Only the top commit (feat: support the IntersectionObserver scrollMargin option) is new here.

rootMargin only dilates the root intersection rectangle, so it can't reveal a target hidden inside an intermediate scroll container. In the spec, that's scrollMargin's job: it dilates the clip rect of every scrollport on the path from the root to the target, and the root's own rect when the root is scrollable. A typical use is prefetching data or images for list items before they scroll into view.

Until now, scrollMargin was rejected: the constructor threw if 'scrollMargin' in options, and the getter always threw.

Only scroll containers clip, and only their clip rects are dilated. RN's clip walk used to clip at every node, including the target itself and overflow: visible ancestors (to their content bounds). That's a no-op without margins, but not with them:

  • Dilating those clips would shrink the target for negative (or mixed-sign) margins.
  • Not dilating them would let a visible node re-clip content that a dilated scroll container below it revealed, e.g. the content container between two nested ScrollViews.

So the walk now only clips at scroll containers, and never dilates the measured node's own clip, since the spec starts the walk at its containing block. overflowInset can't identify scroll containers (it's also zero for a visible node whose children fit), and react/renderer/core can't see Yoga styles. So LayoutableShadowNode gains a virtual isScrollContainer(), which defaults to false and which YogaLayoutableShadowNode overrides to return whether overflow != visible. Only IntersectionObserver uses this clipping mode.

Scrollable explicit roots. Per spec, "both the scrollMargin and the rootMargin are applied to a scrollable intersection root's rectangle". When the explicit root is a scroll container, the rect targets are clipped against is also dilated by scrollMargin, with percentages resolved against the undilated root. rootBounds is the root intersection rectangle, which only rootMargin dilates, so it's unaffected.

Changes:

  • MarginValue moves from IntersectionObserver.h to RectangleEdges.h, because react/renderer/core can't depend on observers/intersection. It gains EdgeMargins and resolveEdgeMargins(), which replace the index-based std::vector<MarginValue> with named edges and retire calculateRootMarginInsets.
  • LayoutInspectingPolicy gains std::optional<EdgeMargins> clipMargin. The margins are passed unresolved, because percentages must resolve against each individual scrollport.
  • enableOverflowClipping only clips at scroll containers.
  • LayoutableShadowNode::isScrollContainer(), overridden by YogaLayoutableShadowNode.
  • IntersectionObserver applies scrollMargin to a scrollable explicit root's clip rect.
  • parseNormalizedRootMargin becomes parseNormalizedMargin, returning std::optional<EdgeMargins>, and is shared by both options.
  • JS: scrollMargin is added to IntersectionObserverInit, the throws are removed, and normalizeRootMargin becomes normalizeMargin(value, name) so errors name the right property.
  • RNTester: a prefetching example with a toggle between rootMargin and scrollMargin. It reports items of a ScrollView within 500px of the viewport, showing that only scrollMargin can see past the ScrollView.

One deviation from the spec, inherited from the existing rootMargin code: percentages on the vertical edges resolve against height, while the spec resolves all four against width.

Changelog:

[GENERAL] [ADDED] - Support the scrollMargin option in IntersectionObserver

Test Plan:
Rebased onto current master (648e9072bbcd). Resolved the merge conflict by removing tests for the now-supported scrollMargin option.

The two LayoutableShadowNodeTest clipping cases now explicitly cover overflow: visible, hidden, and scroll, both with and without the parent's scale transform. Visible overflow preserves the full target rectangle; hidden and scroll overflow preserve the original clipped expectations.

Validation on this version:

  • buck2 test fbsource//xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/core:tests --prefer-remote -j 12: 98 passed, no failures.
  • yarn fantom packages/react-native/src/private/webapis/intersectionobserver/ --runInBand: 2 suites passed, 139 tests passed. This includes the existing scrollMargin parsing/getter, intermediate and nested scrollport, negative-margin, explicit-root, percentage, and rootMargin composition coverage.
  • Meta-managed clang-format applied to the updated C++ test file.
  • arc lint --never-apply-patches --rev 'ancestor(remote/master,.)' --only-changed --output json: clean.

The RNTester example was not changed by this repair; simulator verification was not repeated. The C++ API snapshots are included in the diff and were merged during the rebase; snapshot generation/validation was not rerun locally.

Uploaded as draft version 481315923; CI is running. Neither publication nor landing was requested.

Differential Revision: D133273865

Pulled By: rubennorte

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 19, 2026
@rubennorte
rubennorte force-pushed the intersection-observer-scroll-margin branch 3 times, most recently from f2d860b to d64a9d0 Compare November 19, 2026 18:41
@rubennorte
rubennorte marked this pull request as ready for review November 19, 2026 18:42
@meta-codesync

meta-codesync Bot commented Nov 19, 2026

Copy link
Copy Markdown

@rubennorte has imported this pull request. If you are a Meta employee, you can view this in D133273865.

Summary:
> [!NOTE]
> Stacked on react#60152, which this PR includes. Only the top commit (`feat: support the IntersectionObserver scrollMargin option`) is new here.

`rootMargin` only dilates the root intersection rectangle, so it can't reveal a target hidden inside an intermediate scroll container. In the spec, that's `scrollMargin`'s job: it dilates the clip rect of every scrollport on the path from the root to the target, and the root's own rect when the root is scrollable. A typical use is prefetching data or images for list items before they scroll into view.

Until now, `scrollMargin` was rejected: the constructor threw if `'scrollMargin' in options`, and the getter always threw.

**Only scroll containers clip, and only their clip rects are dilated.** RN's clip walk used to clip at every node, including the target itself and `overflow: visible` ancestors (to their content bounds). That's a no-op without margins, but not with them:
- Dilating those clips would shrink the target for negative (or mixed-sign) margins.
- Not dilating them would let a `visible` node re-clip content that a dilated scroll container below it revealed, e.g. the content container between two nested ScrollViews.

So the walk now only clips at scroll containers, and never dilates the measured node's own clip, since the spec starts the walk at its containing block. `overflowInset` can't identify scroll containers (it's also zero for a `visible` node whose children fit), and `react/renderer/core` can't see Yoga styles. So `LayoutableShadowNode` gains a virtual `isScrollContainer()`, which defaults to `false` and which `YogaLayoutableShadowNode` overrides to return whether `overflow != visible`. Only IntersectionObserver uses this clipping mode.

**Scrollable explicit roots.** Per spec, *"both the scrollMargin and the rootMargin are applied to a scrollable intersection root's rectangle"*. When the explicit root is a scroll container, the rect targets are clipped against is also dilated by `scrollMargin`, with percentages resolved against the undilated root. `rootBounds` is the root intersection rectangle, which only `rootMargin` dilates, so it's unaffected.

Changes:
- `MarginValue` moves from `IntersectionObserver.h` to `RectangleEdges.h`, because `react/renderer/core` can't depend on `observers/intersection`. It gains `EdgeMargins` and `resolveEdgeMargins()`, which replace the index-based `std::vector<MarginValue>` with named edges and retire `calculateRootMarginInsets`.
- `LayoutInspectingPolicy` gains `std::optional<EdgeMargins> clipMargin`. The margins are passed unresolved, because percentages must resolve against each individual scrollport.
- `enableOverflowClipping` only clips at scroll containers.
- `LayoutableShadowNode::isScrollContainer()`, overridden by `YogaLayoutableShadowNode`.
- `IntersectionObserver` applies `scrollMargin` to a scrollable explicit root's clip rect.
- `parseNormalizedRootMargin` becomes `parseNormalizedMargin`, returning `std::optional<EdgeMargins>`, and is shared by both options.
- JS: `scrollMargin` is added to `IntersectionObserverInit`, the throws are removed, and `normalizeRootMargin` becomes `normalizeMargin(value, name)` so errors name the right property.
- RNTester: a prefetching example with a toggle between `rootMargin` and `scrollMargin`. It reports items of a `ScrollView` within 500px of the viewport, showing that only `scrollMargin` can see past the `ScrollView`.

One deviation from the spec, inherited from the existing `rootMargin` code: percentages on the vertical edges resolve against height, while the spec resolves all four against width.

## Changelog:

[GENERAL] [ADDED] - Support the `scrollMargin` option in IntersectionObserver

Pull Request resolved: react#60153

Test Plan:
Rebased onto current `master` (`648e9072bbcd`). Resolved the merge conflict by removing tests for the now-supported `scrollMargin` option.

The two `LayoutableShadowNodeTest` clipping cases now explicitly cover `overflow: visible`, `hidden`, and `scroll`, both with and without the parent's scale transform. Visible overflow preserves the full target rectangle; hidden and scroll overflow preserve the original clipped expectations.

Validation on this version:
- `buck2 test fbsource//xplat/js/react-native-github/packages/react-native/ReactCommon/react/renderer/core:tests --prefer-remote -j 12`: 98 passed, no failures.
- `yarn fantom packages/react-native/src/private/webapis/intersectionobserver/ --runInBand`: 2 suites passed, 139 tests passed. This includes the existing `scrollMargin` parsing/getter, intermediate and nested scrollport, negative-margin, explicit-root, percentage, and `rootMargin` composition coverage.
- Meta-managed `clang-format` applied to the updated C++ test file.
- `arc lint --never-apply-patches --rev 'ancestor(remote/master,.)' --only-changed --output json`: clean.

The RNTester example was not changed by this repair; simulator verification was not repeated. The C++ API snapshots are included in the diff and were merged during the rebase; snapshot generation/validation was not rerun locally.

Uploaded as draft version `481315923`; CI is running. Neither publication nor landing was requested.

Differential Revision: D133273865

Pulled By: rubennorte
@meta-codesync meta-codesync Bot changed the title Support the IntersectionObserver scrollMargin option Support the IntersectionObserver scrollMargin option (#60153) Nov 23, 2026
@rubennorte
rubennorte force-pushed the intersection-observer-scroll-margin branch from d64a9d0 to aa626bf Compare November 23, 2026 15:52
@meta-codesync

meta-codesync Bot commented Nov 23, 2026

Copy link
Copy Markdown

@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D133273865.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant