From 5490f490efd94e8664a5d3be39f4ae15f3f586f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 19 Nov 2026 15:00:24 +0000 Subject: [PATCH] fix: don't report targets fully clipped by an ancestor as intersecting Summary: When an ancestor between the target and the root (a ScrollView or an `overflow: hidden` view) clips the target away entirely, `getClippedTargetBoundingRect` returned `Rect{}`: a zero-size rect at the origin. `intersectOrNull` then treated that as an edge-adjacent intersection whenever the root rectangle contained the origin, which is always the case for the implicit root (the origin is its corner) and almost always for an explicit root (the empty rect is translated to the root's origin). Because a threshold of 0 is crossed by an intersection ratio of 0, observers with the default `threshold` (or an `rnRootThreshold` containing 0) reported such targets with `isIntersecting: true`, as long as their unclipped bounding box overlapped the root. `rootMargin` makes this easy to hit, since it widens that overlap: e.g. with `rootMargin: '500px 0px'`, every item of a ScrollView within 500px of the viewport is reported as intersecting even though the ScrollView clips it. A fully clipped target now produces no intersection, so it's reported as not intersecting with an empty `intersectionRect`. This matches the `intersection-observer/clip-path.html` Web Platform Test, where a target fully clipped by an ancestor while its bounding box is inside the viewport expects `isIntersecting: false`. Observers with thresholds above 0 are unaffected: they already reported these targets as not intersecting, with the same entry values. Changelog: [GENERAL] [FIXED] - IntersectionObserver no longer reports targets fully clipped by an ancestor as intersecting Test Plan: New Fantom tests cover a target fully clipped by an intermediate ScrollView with the default threshold, and with `rootMargin`. Both fail without this change. --- .../intersection/IntersectionObserver.cpp | 19 ++++-- .../__tests__/IntersectionObserver-itest.js | 63 +++++++++++++++++++ 2 files changed, 77 insertions(+), 5 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp b/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp index c62b66b05c97..325f0b6692ee 100644 --- a/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp +++ b/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp @@ -150,7 +150,8 @@ static Rect getBoundingRect(const ShadowNodeFamily::AncestorList& ancestors) { return layoutMetrics == EmptyLayoutMetrics ? Rect{} : layoutMetrics.frame; } -static Rect getClippedTargetBoundingRect( +// Returns std::nullopt if the target is entirely clipped by an ancestor. +static std::optional getClippedTargetBoundingRect( const ShadowNodeFamily::AncestorList& targetAncestors) { auto layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics( targetAncestors, @@ -159,7 +160,10 @@ static Rect getClippedTargetBoundingRect( /* .applyParentClipping = */ .enableOverflowClipping = true, /* .clipSpecifiedAncestor = */ .clipSpecifiedAncestor = false}); - return layoutMetrics == EmptyLayoutMetrics ? Rect{} : layoutMetrics.frame; + if (layoutMetrics == EmptyLayoutMetrics) { + return std::nullopt; + } + return layoutMetrics.frame; } // Distinguishes between edge-adjacent vs. no intersection @@ -212,13 +216,18 @@ static std::optional computeIntersection( // final clip against the root intersection rectangle. auto clippedTargetFromRoot = getClippedTargetBoundingRect(targetToRootAncestors); + if (!clippedTargetFromRoot.has_value()) { + // Don't fall through with an empty rect: at the origin, it would count as + // edge-adjacent to any root rect containing the origin. + return std::nullopt; + } // Use root origin (without rootMargins) to translate coordinates of // clippedTarget from relative to root, to top-level coordinate system auto clippedTargetBoundingRect = hasExplicitRoot ? Rect{ - .origin= rootBoundingRect.origin + clippedTargetFromRoot.origin, - .size=clippedTargetFromRoot.size} - : clippedTargetFromRoot; + .origin= rootBoundingRect.origin + clippedTargetFromRoot->origin, + .size=clippedTargetFromRoot->size} + : *clippedTargetFromRoot; return intersectOrNull(rootMarginBoundingRect, clippedTargetBoundingRect); } diff --git a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js index 31304cb2c340..543a6a26a35e 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js @@ -3976,6 +3976,69 @@ describe('IntersectionObserver', () => { expect(entries[0].isIntersecting).toBe(false); expect(entries[0].intersectionRatio).toBe(0); }); + + describe('targets fully clipped by an intermediate ScrollView', () => { + // The target (150..200) is inside the 1000x1000 viewport but entirely + // clipped by the 100x100 ScrollView. With a threshold of 0, even an + // edge-adjacent intersection would count, so this checks that a fully + // clipped target is not mistaken for one. + function renderFullyClippedTarget(): HostInstance { + const nodeRef = React.createRef(); + + const root = Fantom.createRoot({ + viewportWidth: 1000, + viewportHeight: 1000, + }); + Fantom.runTask(() => { + root.render( + + + , + ); + }); + + return nullthrows(nodeRef.current); + } + + it('should not report them as intersecting with the default threshold', () => { + const node = renderFullyClippedTarget(); + + const intersectionObserverCallback = jest.fn(); + + Fantom.runTask(() => { + observer = new IntersectionObserver(intersectionObserverCallback); + observer.observe(node); + }); + + expect(intersectionObserverCallback).toHaveBeenCalledTimes(1); + const [entries] = intersectionObserverCallback.mock.lastCall; + expect(entries.length).toBe(1); + expect(entries[0].isIntersecting).toBe(false); + expect(entries[0].intersectionRatio).toBe(0); + }); + + it('should not report them as intersecting with rootMargin', () => { + const node = renderFullyClippedTarget(); + + const intersectionObserverCallback = jest.fn(); + + Fantom.runTask(() => { + observer = new IntersectionObserver(intersectionObserverCallback, { + rootMargin: '500px 0px', + }); + observer.observe(node); + }); + + expect(intersectionObserverCallback).toHaveBeenCalledTimes(1); + const [entries] = intersectionObserverCallback.mock.lastCall; + expect(entries.length).toBe(1); + expect(entries[0].isIntersecting).toBe(false); + expect(entries[0].intersectionRatio).toBe(0); + }); + }); }); describe('unobserve(target)', () => {