InfiniteScroll

Composable infinite-scroll slots for paginated lists.

Quick Preview

Loading...

Composition

Use this public composition when building InfiniteScroll:

InfiniteScroll
├── InfiniteScrollEmpty
├── InfiniteScrollContent
├── InfiniteScrollLoading
├── InfiniteScrollError
├── InfiniteScrollEnd
└── InfiniteScrollSentinel

Usage 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.

Loading...

Props

Prop/APITypeDefaultDescription
fetchNextPage() => unknown-Called when the sentinel enters view. Pass the function returned by useInfiniteQuery.
hasNextPage`booleanundefined`-
isLoadingboolean-Initial page loading state. Usually query.isLoading.
isEmptyboolean-Whether the flattened list has no items.
isFetchingNextPageboolean-Whether another page is currently loading. Usually query.isFetchingNextPage.
error`Errornull`-
rootMargin on InfiniteScrollSentinelstring"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.

On this page