Infinite Scroll
Load the next page when a sentinel scrolls into view, with a loading row, an end of list message and a Load more button as the accessible fallback.
Installation
npx shadcn@latest add @minidev/infinite-scroll
MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/infinite-scroll.json
Usage
import { InfiniteScroll } from "@/components/ui/infinite-scroll"
export function Example() {
return (
<InfiniteScroll />
)
}
API reference
| Prop | Type | Default |
|---|---|---|
| loadMoreFetch a page (0-based); return its items and whether more remain. Throw to show Retry. | (page: number) => Promise<{ items: T[]; hasMore: boolean }> | - |
| renderItemRenders one item. Each is wrapped in an li. | (item: T, index: number) => React.ReactNode | - |
| rootMarginHow far before the end to start loading, as a CSS margin. | string | "240px" |
| labelAccessible name for the focusable scroll box and the list inside it. | string | "Results" |
| className | string | - |
When to use
It uses IntersectionObserver, never fires twice for the same page, shows a loading row and an end-of-list message, keeps a real "Load more" button as the accessible fallback, and offers Retry when a page fails. Use it for feeds of unknown length. For thousands of rows already in memory use VirtualList, and for numbered pages use Pagination.
Keyboard interactions
| Key | Action |
|---|---|
| Tab | Focuses the scroll box, then moves through focusable content in the items and on to the Load more or Retry button. |
| ArrowUporArrowDown | With the scroll box focused, scrolls the list. |
| EnterorSpace | On Load more, fetches the next page. On Retry, repeats the failed request. |
Accessibility
- The scroll box is a focusable
role="region"with a focus ring, so it can be scrolled from the keyboard even when the items hold nothing focusable. - The region and the
ulinside it are both named bylabel("Results" by default). Theulhasaria-busywhile a page loads. - The status line is
aria-live="polite", so "Loading more", "Could not load more." and "You have reached the end" are announced. - The scroll sentinel is
aria-hidden; keyboard and screen reader users load pages with the real Load more button instead.
Frequently asked questions
How do I use InfiniteScroll with a cursor-based API?
loadMore(page) receives a 0-based page number and must resolve { items, hasMore }. For cursors, keep the last cursor in a ref inside your loader and return hasMore: Boolean(res.nextCursor). The latest loadMore is read from a ref, so an inline function does not reset anything.
What happens when a page fails to load?
Throw or reject in loadMore. The list shows "Could not load more." with a Retry button, and stops watching the scroll until Retry is pressed, so a failing API is not called on every scroll.
How do I start over when the search or filters change?
There is no reset prop; the items and page number are internal state. Remount it with a key: <InfiniteScroll key={query} loadMore={...} /> loads from page 0 again.
Why does it scroll inside a box instead of the page?
The component is its own scroll container (h-80 overflow-y-auto), and the IntersectionObserver uses that box as its root. Change the height with className, such as className="h-[600px]". Scrolling the window instead means changing the observer's root in your copy.
Dependencies
Examples
Custom loader and rows
loadMore receives the page index and returns the items plus whether more exist; renderItem draws each row.
Short list that ends
Return hasMore false from the last page and the end of list message replaces the loader; rootMargin starts fetching earlier.