Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <react/renderer/css/CSSPercentage.h>
#include <react/renderer/css/CSSValueParser.h>
#include <react/renderer/graphics/RectangleEdges.h>
#include <iterator>
#include <utility>

namespace facebook::react {
Expand Down Expand Up @@ -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_);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5164,6 +5164,51 @@ describe('IntersectionObserver', () => {
});
});

it('should resolve explicit roots through the target ancestor path', () => {
const observationRootRef = createRef<HostInstance>();
const targetRef = createRef<HostInstance>();
const unrelatedRootRef = createRef<HostInstance>();
const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 100});
Fantom.runTask(() => {
root.render(
<>
<View ref={observationRootRef} style={{width: 100, height: 100}}>
<View>
<View>
<View ref={targetRef} style={{width: 20, height: 20}} />
</View>
</View>
</View>
<View
ref={unrelatedRootRef}
style={{position: 'absolute', width: 100, height: 100}}
/>
</>,
);
});
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();
Expand Down
Loading