Repository navigation
Support the IntersectionObserver scrollMargin option (#60153) - #60153
Open
rubennorte wants to merge 1 commit into
Open
rubennorte wants to merge 1 commit into
rubennorte wants to merge 1 commit into
Conversation
rubennorte
force-pushed
the
intersection-observer-scroll-margin
branch
3 times, most recently
from
November 19, 2026 18:41
f2d860b to
d64a9d0
Compare
rubennorte
marked this pull request as ready for review
November 19, 2026 18:42
|
@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
rubennorte
force-pushed
the
intersection-observer-scroll-margin
branch
from
November 23, 2026 15:52
d64a9d0 to
aa626bf
Compare
|
@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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Note
Stacked on #60152, which this PR includes. Only the top commit (
feat: support the IntersectionObserver scrollMargin option) is new here.rootMarginonly dilates the root intersection rectangle, so it can't reveal a target hidden inside an intermediate scroll container. In the spec, that'sscrollMargin'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,
scrollMarginwas 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: visibleancestors (to their content bounds). That's a no-op without margins, but not with them:visiblenode 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.
overflowInsetcan't identify scroll containers (it's also zero for avisiblenode whose children fit), andreact/renderer/corecan't see Yoga styles. SoLayoutableShadowNodegains a virtualisScrollContainer(), which defaults tofalseand whichYogaLayoutableShadowNodeoverrides to return whetheroverflow != 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.rootBoundsis the root intersection rectangle, which onlyrootMargindilates, so it's unaffected.Changes:
MarginValuemoves fromIntersectionObserver.htoRectangleEdges.h, becausereact/renderer/corecan't depend onobservers/intersection. It gainsEdgeMarginsandresolveEdgeMargins(), which replace the index-basedstd::vector<MarginValue>with named edges and retirecalculateRootMarginInsets.LayoutInspectingPolicygainsstd::optional<EdgeMargins> clipMargin. The margins are passed unresolved, because percentages must resolve against each individual scrollport.enableOverflowClippingonly clips at scroll containers.LayoutableShadowNode::isScrollContainer(), overridden byYogaLayoutableShadowNode.IntersectionObserverappliesscrollMarginto a scrollable explicit root's clip rect.parseNormalizedRootMarginbecomesparseNormalizedMargin, returningstd::optional<EdgeMargins>, and is shared by both options.scrollMarginis added toIntersectionObserverInit, the throws are removed, andnormalizeRootMarginbecomesnormalizeMargin(value, name)so errors name the right property.rootMarginandscrollMargin. It reports items of aScrollViewwithin 500px of the viewport, showing that onlyscrollMargincan see past theScrollView.One deviation from the spec, inherited from the existing
rootMargincode: percentages on the vertical edges resolve against height, while the spec resolves all four against width.Changelog:
[GENERAL] [ADDED] - Support the
scrollMarginoption in IntersectionObserverTest Plan:
Rebased onto current
master(648e9072bbcd). Resolved the merge conflict by removing tests for the now-supportedscrollMarginoption.The two
LayoutableShadowNodeTestclipping cases now explicitly coveroverflow: visible,hidden, andscroll, 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 existingscrollMarginparsing/getter, intermediate and nested scrollport, negative-margin, explicit-root, percentage, androotMargincomposition coverage.clang-formatapplied 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