Skip to content

Commit 8771d39

Browse files
authored
Don't report IntersectionObserver 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. That's 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 an intersection ratio of 0 crosses a threshold of 0, observers with the default `threshold` (or an `rnRootThreshold` containing 0) reported these targets with `isIntersecting: true`, as long as their unclipped bounding box overlapped the root. `rootMargin` makes this easy to hit because it widens that overlap. For example, 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`](https://github.com/web-platform-tests/wpt/blob/master/intersection-observer/clip-path.html) Web Platform Test: a target fully clipped by an ancestor, while its bounding box is still 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. Found while working on `scrollMargin` support, which is stacked on top of this. ## Changelog: [GENERAL] [FIXED] - IntersectionObserver no longer reports targets fully clipped by an ancestor as intersecting ## Test Plan: Added Fantom tests for a target fully clipped by an intermediate `ScrollView`, with the default threshold and with `rootMargin`. Both fail without this change. ``` yarn fantom packages/react-native/src/private/webapis/intersectionobserver/ ``` 110 passed, 1 skipped. ``` yarn flow-check yarn lint ``` Clean. Differential Revision: D133249484 Pull Request resolved: #60152
1 parent 4634366 commit 8771d39

2 files changed

Lines changed: 77 additions & 5 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ static Rect getBoundingRect(const ShadowNodeFamily::AncestorList& ancestors) {
150150
return layoutMetrics == EmptyLayoutMetrics ? Rect{} : layoutMetrics.frame;
151151
}
152152

153-
static Rect getClippedTargetBoundingRect(
153+
// Returns std::nullopt if the target is entirely clipped by an ancestor.
154+
static std::optional<Rect> getClippedTargetBoundingRect(
154155
const ShadowNodeFamily::AncestorList& targetAncestors) {
155156
auto layoutMetrics = LayoutableShadowNode::computeRelativeLayoutMetrics(
156157
targetAncestors,
@@ -159,7 +160,10 @@ static Rect getClippedTargetBoundingRect(
159160
/* .applyParentClipping = */ .enableOverflowClipping = true,
160161
/* .clipSpecifiedAncestor = */ .clipSpecifiedAncestor = false});
161162

162-
return layoutMetrics == EmptyLayoutMetrics ? Rect{} : layoutMetrics.frame;
163+
if (layoutMetrics == EmptyLayoutMetrics) {
164+
return std::nullopt;
165+
}
166+
return layoutMetrics.frame;
163167
}
164168

165169
// Distinguishes between edge-adjacent vs. no intersection
@@ -212,13 +216,18 @@ static std::optional<Rect> computeIntersection(
212216
// final clip against the root intersection rectangle.
213217
auto clippedTargetFromRoot =
214218
getClippedTargetBoundingRect(targetToRootAncestors);
219+
if (!clippedTargetFromRoot.has_value()) {
220+
// Don't fall through with an empty rect: at the origin, it would count as
221+
// edge-adjacent to any root rect containing the origin.
222+
return std::nullopt;
223+
}
215224

216225
// Use root origin (without rootMargins) to translate coordinates of
217226
// clippedTarget from relative to root, to top-level coordinate system
218227
auto clippedTargetBoundingRect = hasExplicitRoot ? Rect{
219-
.origin= rootBoundingRect.origin + clippedTargetFromRoot.origin,
220-
.size=clippedTargetFromRoot.size}
221-
: clippedTargetFromRoot;
228+
.origin= rootBoundingRect.origin + clippedTargetFromRoot->origin,
229+
.size=clippedTargetFromRoot->size}
230+
: *clippedTargetFromRoot;
222231

223232
return intersectOrNull(rootMarginBoundingRect, clippedTargetBoundingRect);
224233
}

‎packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3976,6 +3976,69 @@ describe('IntersectionObserver', () => {
39763976
expect(entries[0].isIntersecting).toBe(false);
39773977
expect(entries[0].intersectionRatio).toBe(0);
39783978
});
3979+
3980+
describe('targets fully clipped by an intermediate ScrollView', () => {
3981+
// The target (150..200) is inside the 1000x1000 viewport but entirely
3982+
// clipped by the 100x100 ScrollView. With a threshold of 0, even an
3983+
// edge-adjacent intersection would count, so this checks that a fully
3984+
// clipped target is not mistaken for one.
3985+
function renderFullyClippedTarget(): HostInstance {
3986+
const nodeRef = React.createRef<HostInstance>();
3987+
3988+
const root = Fantom.createRoot({
3989+
viewportWidth: 1000,
3990+
viewportHeight: 1000,
3991+
});
3992+
Fantom.runTask(() => {
3993+
root.render(
3994+
<ScrollView style={{width: 100, height: 100, flexGrow: 0}}>
3995+
<View
3996+
style={{width: 50, height: 50, marginTop: 150}}
3997+
ref={nodeRef}
3998+
/>
3999+
</ScrollView>,
4000+
);
4001+
});
4002+
4003+
return nullthrows(nodeRef.current);
4004+
}
4005+
4006+
it('should not report them as intersecting with the default threshold', () => {
4007+
const node = renderFullyClippedTarget();
4008+
4009+
const intersectionObserverCallback = jest.fn();
4010+
4011+
Fantom.runTask(() => {
4012+
observer = new IntersectionObserver(intersectionObserverCallback);
4013+
observer.observe(node);
4014+
});
4015+
4016+
expect(intersectionObserverCallback).toHaveBeenCalledTimes(1);
4017+
const [entries] = intersectionObserverCallback.mock.lastCall;
4018+
expect(entries.length).toBe(1);
4019+
expect(entries[0].isIntersecting).toBe(false);
4020+
expect(entries[0].intersectionRatio).toBe(0);
4021+
});
4022+
4023+
it('should not report them as intersecting with rootMargin', () => {
4024+
const node = renderFullyClippedTarget();
4025+
4026+
const intersectionObserverCallback = jest.fn();
4027+
4028+
Fantom.runTask(() => {
4029+
observer = new IntersectionObserver(intersectionObserverCallback, {
4030+
rootMargin: '500px 0px',
4031+
});
4032+
observer.observe(node);
4033+
});
4034+
4035+
expect(intersectionObserverCallback).toHaveBeenCalledTimes(1);
4036+
const [entries] = intersectionObserverCallback.mock.lastCall;
4037+
expect(entries.length).toBe(1);
4038+
expect(entries[0].isIntersecting).toBe(false);
4039+
expect(entries[0].intersectionRatio).toBe(0);
4040+
});
4041+
});
39794042
});
39804043

39814044
describe('unobserve(target)', () => {

0 commit comments

Comments
 (0)