Repository navigation
Commit f2d860b
committed
feat: support the IntersectionObserver scrollMargin option
Summary:
`rootMargin` only dilates the root intersection rectangle, so it cannot
reveal a target hidden inside an intermediate scroll container. Per spec
that is `scrollMargin`'s job: it dilates the clip rect of every scrollport
on the path from the root to the target, and of the root itself when the
root is scrollable.
Until now `scrollMargin` was explicitly rejected — the constructor threw on
`'scrollMargin' in options` and the getter always threw.
D117876568 (#58177) did the groundwork by adding `clipSpecifiedAncestor` and
excluding the observation root from the ancestor clip walk, matching the
spec's "while container is not root" loop.
Per spec, only nodes with a content clip clip the target, and only scroll
containers have their clip rects dilated. RN's clip walk used to clip at
every node, including the target itself and `overflow: visible` ancestors
(to their content bounds). That is a no-op without margins, but not with
them: dilating those clips shrinks the target for negative (or mixed-sign)
margins, and not dilating them lets a `visible` node re-clip content that a
dilated scroll container below it revealed (e.g. the content container
between two nested ScrollViews). So the walk now only clips at nodes with a
content clip, only dilates scroll containers, and never dilates the
measured node's own clip, since the spec starts the walk at its containing
block. `overflowInset` cannot identify these nodes (it is also zero for a
`visible` node whose children fit), and `react/renderer/core` cannot see
Yoga styles, so `LayoutableShadowNode` gains virtual `hasContentClip()` and
`isScrollContainer()` (both default `false`). `YogaLayoutableShadowNode`
returns `overflow != visible` for the former and `overflow` is `hidden` or
`scroll` for the latter. They're the same set today, but CSS
`overflow: clip` is a content clip without being a scroll container, and
`isScrollContainer()` uses an exhaustive switch so new `overflow` values
must be classified explicitly. Only IntersectionObserver uses this clipping
mode.
Per spec, "both the scrollMargin and the rootMargin are applied to a
scrollable intersection root's rectangle". The rect targets are clipped
against is therefore further dilated by `scrollMargin` when the explicit
root is a scroll container, with percentages resolved against the
undilated root. `rootBounds` is the root intersection rectangle, which only
`rootMargin` dilates, so it is unaffected.
Changes:
- `MarginValue` moves from `IntersectionObserver.h` to `RectangleEdges.h`
(`react/renderer/core` cannot depend on `observers/intersection`), gaining
`EdgeMargins` and `resolveEdgeMargins()`. This replaces the index-based
`std::vector<MarginValue>` with named edges and retires
`calculateRootMarginInsets`.
- `LayoutInspectingPolicy` gains `std::optional<EdgeMargins> clipMargin`,
applied to scroll-container ancestors only. The margins travel unresolved
because percentages must resolve against each individual scrollport.
- `enableOverflowClipping` only clips at nodes with a content clip.
- `LayoutableShadowNode::hasContentClip()` and `isScrollContainer()`,
overridden by `YogaLayoutableShadowNode`.
- `IntersectionObserver` applies `scrollMargin` to a scrollable explicit
root's clip rect.
- `parseNormalizedRootMargin` becomes `parseNormalizedMargin`, returning
`std::optional<EdgeMargins>` and shared by both options.
- JS: `scrollMargin` added to `IntersectionObserverInit`, the throws are
removed, and `normalizeRootMargin` becomes `normalizeMargin(value, name)`
so errors name the right property.
- RNTester: a prefetching example (toggle between `rootMargin` and
`scrollMargin`, reporting items of a ScrollView within 500px of the
viewport) showing that only `scrollMargin` can see past the ScrollView.
One deviation, inherited from the existing `rootMargin` code: percentages
on the vertical edges resolve against height (the spec resolves all four
against width).
Changelog:
[GENERAL] [ADDED] - Support the `scrollMargin` option in IntersectionObserver
Test Plan:
New Fantom tests cover parsing and the getter, plus behaviour: revealing a
target past an intermediate ScrollView, partial and insufficient margins,
percentages resolved per scrollport, negative values, negative values not
affecting `overflow: visible` ancestors or the target itself,
`overflow: hidden` ancestors, scrollable and non-scrollable explicit roots
(including combination with `rootMargin` and percentage resolution against
the undilated root), nested ScrollViews, horizontal edges, the top edge
after scrolling, and composition with `rootMargin` on the implicit root.
```
yarn fantom packages/react-native/src/private/webapis/intersectionobserver/
```
All tests pass. With the C++ changes from the review fixes reverted, the
negative-margin and scrollable-root tests fail, as expected.
```
yarn flow-check
yarn lint
```
Clean.
Manually verified the RNTester example on an iOS simulator.
The `scripts/cxx-api/api-snapshots/*.api` files were taken from CI's
`validate_cxx_api_snapshots` run (Doxygen 1.16.1); the only changes are the
new public symbols above and the codegen'd `scrollMargin` option.1 parent 5490f49 commit f2d860b
30 files changed
Lines changed: 1334 additions & 135 deletions
File tree
- packages
- react-native
- ReactCommon/react
- nativemodule/intersectionobserver
- renderer
- components/view
- core
- graphics
- observers/intersection
- flow
- src/private/webapis/intersectionobserver
- __tests__
- internals
- specs
- rn-tester/js/examples/IntersectionObserver
- scripts/cxx-api/api-snapshots
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
63 | 63 | | |
64 | 64 | | |
65 | 65 | | |
| 66 | + | |
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| |||
72 | 73 | | |
73 | 74 | | |
74 | 75 | | |
| 76 | + | |
75 | 77 | | |
76 | 78 | | |
77 | 79 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
181 | 198 | | |
182 | 199 | | |
183 | 200 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
82 | 86 | | |
83 | 87 | | |
84 | 88 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
181 | 181 | | |
182 | 182 | | |
183 | 183 | | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
184 | 187 | | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
189 | 199 | | |
190 | 200 | | |
191 | 201 | | |
| |||
224 | 234 | | |
225 | 235 | | |
226 | 236 | | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
227 | 245 | | |
228 | 246 | | |
229 | 247 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| 46 | + | |
| 47 | + | |
45 | 48 | | |
46 | 49 | | |
47 | 50 | | |
48 | 51 | | |
49 | 52 | | |
50 | 53 | | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
51 | 59 | | |
52 | 60 | | |
53 | 61 | | |
| |||
140 | 148 | | |
141 | 149 | | |
142 | 150 | | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
143 | 166 | | |
144 | 167 | | |
145 | 168 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
85 | 119 | | |
86 | 120 | | |
87 | 121 | | |
| |||
0 commit comments