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)', () => {