Skip to content

Commit bbe3151

Browse files
committed
Add rootMargin
1 parent c7a1434 commit bbe3151

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

docs/api/InfiniteScroll.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ interface Props<T, P extends HTMLElement> {
2121
next: () => unknown;
2222
threshold?: number;
2323
root?: Element | Document | undefined | null;
24+
rootMargin?: string;
2425
reverse?: boolean;
2526
}
2627

@@ -32,6 +33,7 @@ declare function InfiniteScroll<T, P extends HTMLElement>({
3233
next,
3334
threshold,
3435
root,
36+
rootMargin,
3537
reverse,
3638
}: Props<T, P>): JSX.Element;
3739
```
@@ -76,6 +78,16 @@ The range of `threshold` is 0 ~ 1, and the default value is `0`.
7678

7779
This property will be used as the viewport for checking visibility of the last or the first item. Must be the ancestor of the item. Defaults to the browser viewport.
7880

81+
### `rootMargin`
82+
83+
A string which specifies a set of offsets to add to the root's bounding box.
84+
85+
:::tip
86+
87+
Since `InfiniteScroll` is implemented with IntersectionObserver, you can find more information about `threshold`, `root` and `rootMargin` on [MDN](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API)
88+
89+
:::
90+
7991
### `reverse`
8092

8193
Set this property to `true` means that `InfiniteScroll` will call `next` when the first item shows up in the viewport. This property is usually used to implement the chatbox behavior.

src/lib/InfiniteScroll.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ interface Props<T, P extends HTMLElement> {
1313
next: () => unknown;
1414
threshold?: number;
1515
root?: Element | Document | null;
16+
rootMargin?: string;
1617
reverse?: boolean;
1718
}
1819

@@ -24,6 +25,7 @@ function InfiniteScroll<T, P extends HTMLElement>({
2425
next,
2526
threshold = 0,
2627
root = null,
28+
rootMargin = '0px',
2729
reverse,
2830
}: Props<T, P>) {
2931
const observer = useRef<IntersectionObserver>();
@@ -46,11 +48,11 @@ function InfiniteScroll<T, P extends HTMLElement>({
4648
next();
4749
}
4850
},
49-
{ threshold, root }
51+
{ threshold, root, rootMargin }
5052
);
5153
observer.current.observe(element);
5254
},
53-
[hasMore, isLoading, next, threshold, root]
55+
[hasMore, isLoading, next, threshold, root, rootMargin]
5456
);
5557

5658
const renderedItems = itemData.map((datum, idx) => {

0 commit comments

Comments
 (0)