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 79a5eb97950c..921fe8a388e8 100644 --- a/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp +++ b/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include namespace facebook::react { @@ -280,15 +281,20 @@ IntersectionObserver::updateIntersectionObservation( rootMarginBoundingRect, targetBoundingRect, {}, time); } - auto targetToRootAncestors = hasExplicitRoot - ? targetShadowNodeFamily_->getAncestors(*getShadowNode(rootAncestors)) - : targetAncestors; + ShadowNodeFamily::AncestorList targetToRootAncestors; + if (hasExplicitRoot && targetAncestors.size() > rootAncestors.size() && + &targetAncestors[rootAncestors.size()].first.get() == + getShadowNode(rootAncestors).get()) { + targetToRootAncestors.assign( + targetAncestors.begin() + std::ssize(rootAncestors), + targetAncestors.end()); + } auto intersection = computeIntersection( rootBoundingRect, rootClipRect, targetBoundingRect, - targetToRootAncestors, + hasExplicitRoot ? targetToRootAncestors : targetAncestors, hasExplicitRoot, scrollMargins_); 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 c8e697c23b6e..5ad0daa26e4f 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 @@ -5195,6 +5195,51 @@ describe('IntersectionObserver', () => { root.destroy(); }); + it('should resolve explicit roots through the target ancestor path', () => { + const observationRootRef = createRef(); + const targetRef = createRef(); + const unrelatedRootRef = createRef(); + const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 100}); + Fantom.runTask(() => { + root.render( + <> + + + + + + + + + , + ); + }); + const target = nullthrows(targetRef.current); + const observationRoot = nullthrows(observationRootRef.current); + const cases: ReadonlyArray<[HostInstance, number]> = [ + [observationRoot, 1], + [nullthrows(unrelatedRootRef.current), 0], + [target, 0], + ]; + for (const [candidateRoot, expectedRatio] of cases) { + const callback = jest.fn(); + Fantom.runTask(() => { + observer = new IntersectionObserver(callback, {root: candidateRoot}); + observer.observe(target); + }); + expect(callback).toHaveBeenCalledTimes(1); + const entry = callback.mock.lastCall[0][0]; + expect(entry.target).toBe(target); + expect(entry.intersectionRatio).toBe(expectedRatio); + expect(entry.isIntersecting).toBe(expectedRatio === 1); + Fantom.runTask(() => observer.disconnect()); + } + root.destroy(); + }); + describe('disconnect', () => { it('should do nothing if no targets are observed (not fail)', () => { const callback = jest.fn();