Skip to content

Commit a69589e

Browse files
committed
Document scrollMargin support in InView
1 parent 32f8da6 commit a69589e

2 files changed

Lines changed: 7 additions & 1 deletion

File tree

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Provide these as the options argument in the `useInView` hook or as props on the
206206
| ---------------------- | ------------------------- | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
207207
| **root** | `Element` | `document` | The Intersection Observer interface's read-only root property identifies the Element or Document whose bounds are treated as the bounding box of the viewport for the element which is the observer's target. If the root is `null`, then the bounds of the actual document viewport are used. |
208208
| **rootMargin** | `string` | `'0px'` | Margin around the root. Can have values similar to the CSS margin property, e.g. `"10px 20px 30px 40px"` (top, right, bottom, left). Also supports percentages, to check if an element intersects with the center of the viewport for example `"-50% 0% -50% 0%"`. |
209-
| **scrollMargin** | `string` | `'0px'` | Margin around nested scroll containers that clip the target, including the root if it is a scroll container. |
209+
| **scrollMargin** | `string` | `'0px'` | Margin around nested scroll containers that clip the target. Can have values similar to the CSS margin property, e.g. `"10px 20px 30px 40px"` (top, right, bottom, left). Unlike `rootMargin`, this grows or shrinks every scroll container's clipping rectangle within the root, including the root itself if it is a scroll container. |
210210
| **threshold** | `number` or `number[]` | `0` | Number between `0` and `1` indicating the percentage that should be visible before triggering. Can also be an array of numbers, to create multiple trigger points. |
211211
| **onChange** | `(inView, entry) => void` | `undefined` | Call this function whenever the in view state changes. It will receive the `inView` boolean, alongside the current `IntersectionObserverEntry`. |
212212
| **trackVisibility** 🧪 | `boolean` | `false` | A boolean indicating whether this Intersection Observer will track visibility changes on the target. |
@@ -299,9 +299,14 @@ When using `rootMargin`, the margin gets added to the current `root` - If your
299299
application is running inside a `<iframe>`, or you have defined a custom `root`
300300
this will not be the current viewport.
301301
302+
If the target is clipped by a scrollable container inside the `root`, use
303+
`scrollMargin` to change when intersections are calculated for that nested
304+
scroll container.
305+
302306
You can read more about this on these links:
303307
304308
- [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#The_intersection_root_and_root_margin)
309+
- [IntersectionObserver scrollMargin](https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/scrollMargin)
305310
- [w3c/IntersectionObserver: rootMargin ignored within iframe](https://github.com/w3c/IntersectionObserver/issues/283#issuecomment-507397917)
306311
- [w3c/IntersectionObserver: Cannot track intersection with an iframe's viewport](https://github.com/w3c/IntersectionObserver/issues/372)
307312
- [w3c/Support iframe viewport tracking](https://github.com/w3c/IntersectionObserver/pull/465)

src/InView.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ export class InView extends React.Component<
194194
threshold,
195195
root,
196196
rootMargin,
197+
scrollMargin,
197198
onChange,
198199
skip,
199200
trackVisibility,

0 commit comments

Comments
 (0)