Data display
InfiniteScroll
Loads more items as the reader nears the end of a list, with a real “Load more” button so keyboard and screen-reader users are never stuck.
import { InfiniteScroll } from '@reactloom/ui';Examples
Automatic loading
Loads when the end comes into view; the number of new items is announced.
- Result 1
- Result 2
- Result 3
- Result 4
- Result 5
- Result 6
- Result 7
- Result 8
import { useState } from 'react';
import { InfiniteScroll } from '@reactloom/ui';
const PAGE = 8;
const TOTAL = 40;
export default function Example() {
const [items, setItems] = useState(() => Array.from({ length: PAGE }, (_, i) => i + 1));
const [loading, setLoading] = useState(false);
const loadMore = () => {
setLoading(true);
// Stand-in for a request.
setTimeout(() => {
setItems((prev) => [...prev, ...Array.from({ length: PAGE }, (_, i) => prev.length + i + 1)]);
setLoading(false);
}, 600);
};
return (
<div style={{ width: '100%', maxWidth: 420, maxHeight: 320, overflow: 'auto' }}>
<InfiniteScroll hasMore={items.length < TOTAL} loading={loading} onLoadMore={loadMore} itemCount={items.length}>
<ul style={{ margin: 0, paddingInlineStart: 20 }}>
{items.map((n) => (
<li key={n} style={{ padding: '6px 0' }}>
Result {n}
</li>
))}
</ul>
</InfiniteScroll>
</div>
);
}Button only
auto={false} loads only when the button is pressed.
- Changelog entry 1
- Changelog entry 2
- Changelog entry 3
- Changelog entry 4
- Changelog entry 5
import { useState } from 'react';
import { InfiniteScroll } from '@reactloom/ui';
export default function Example() {
const [count, setCount] = useState(5);
return (
<div style={{ width: '100%', maxWidth: 420 }}>
<InfiniteScroll auto={false} hasMore={count < 15} onLoadMore={() => setCount((c) => c + 5)} itemCount={count} messages={{ loadMore: 'Show 5 more' }}>
<ol style={{ margin: 0 }}>
{Array.from({ length: count }, (_, i) => (
<li key={i}>Changelog entry {i + 1}</li>
))}
</ol>
</InfiniteScroll>
</div>
);
}API reference
| Prop | Type | Default |
|---|---|---|
hasMorerequiredMore items exist. | boolean | — |
onLoadMorerequiredLoad the next page. | () => void | — |
loadingA page is loading (marks the region busy). | boolean | — |
itemCountItems loaded so far, used to announce how many were added. | number | — |
autoLoad when the end comes into view. | boolean | true |
rootMarginHow early to start loading. | string | '200px' |
messagesButton, loading, end and announcement text. | Partial<InfiniteScrollMessages> | — |
Customization
Extracted from the component’s source on every build, so it matches the code. How the layers fit together is explained in Customization.
CSS variables
| Variable | Default |
|---|---|
--pl-infinite-scroll-padding | var(--pl-space-4) |
Theme tokens it reads
--pl-color-fg-muted --pl-color-fg-subtle --pl-font-size-sm
Accessibility
- The “Load more” button is always present, so loading never depends on scrolling.
- The region is
aria-busywhile loading, and “N more items, M in total.” is announced afterwards. - Focus stays where it was when items are added.
| Key | Action |
|---|---|
| Tab | Reach the “Load more” button after the last item |
| Enter / Space | Load more |
Usage guidelines
Do
- Prefer pagination when people need to return to a position or reach the footer.
Don’t
- Don’t put important links in a footer below an endless list.