InfiniteScroll
Composable infinite-scroll slots for paginated lists.
Quick Preview
Composition
Use this public composition when building InfiniteScroll:
InfiniteScroll
├── InfiniteScrollEmpty
├── InfiniteScrollContent
├── InfiniteScrollLoading
├── InfiniteScrollError
├── InfiniteScrollEnd
└── InfiniteScrollSentinelUsage With TanStack Query
import {
InfiniteScroll,
InfiniteScrollContent,
InfiniteScrollEnd,
InfiniteScrollError,
InfiniteScrollLoading,
InfiniteScrollSentinel,
} from "@giddaa-housing/ui/infinite-scroll";
import { useInfiniteQuery } from "@tanstack/react-query";
const query = useInfiniteQuery({
queryKey: ["activity"],
queryFn: ({ pageParam }) => fetchActivityPage(pageParam),
initialPageParam: 0,
getNextPageParam: (lastPage) => lastPage.nextCursor,
});
const items = query.data?.pages.flatMap((page) => page.items) ?? [];
<InfiniteScroll
isLoading={query.isLoading}
isEmpty={items.length === 0}
error={query.error}
hasNextPage={query.hasNextPage}
isFetchingNextPage={query.isFetchingNextPage}
fetchNextPage={query.fetchNextPage}
>
<InfiniteScrollContent>{items.map((item) => <ActivityItem key={item.id} item={item} />)}</InfiniteScrollContent>
<InfiniteScrollLoading />
<InfiniteScrollError />
<InfiniteScrollEnd>All activity loaded.</InfiniteScrollEnd>
<InfiniteScrollSentinel />
</InfiniteScroll>Examples
TanStack Query List
Use useInfiniteQuery for page state and let InfiniteScrollSentinel call fetchNextPage when it enters the viewport.
Props
| Prop/API | Type | Default | Description |
|---|---|---|---|
fetchNextPage | () => unknown | - | Called when the sentinel enters view. Pass the function returned by useInfiniteQuery. |
hasNextPage | `boolean | undefined` | - |
isLoading | boolean | - | Initial page loading state. Usually query.isLoading. |
isEmpty | boolean | - | Whether the flattened list has no items. |
isFetchingNextPage | boolean | - | Whether another page is currently loading. Usually query.isFetchingNextPage. |
error | `Error | null` | - |
rootMargin on InfiniteScrollSentinel | string | "200px" | Distance from the viewport at which the next fetch triggers. |
Accessibility
- Announce loading and error states near the list.
- Keep the list keyboard navigable without requiring scroll events.
- Provide an end message when it clarifies completion.