InfiniteScroll
InfiniteScroll loads the next page of a list when its end comes into view, down or up, with a load more fallback for keyboard users. It has no dependency and is built on IntersectionObserver.
Import #
import { InfiniteScroll } from '@exhaustive-ui/core/infinitescroll';Basic #
Add pInfiniteScroll to a list that scrolls and load the next page on scrolled. The event is emitted when the end of the list comes within distance of the bottom of the container, 200 pixels by default, and again after the new items are rendered if the end is still in view, so a first page that does not fill the container keeps asking. Bind loading so that nothing is asked while a page is on its way, and set hasMore to false once the last page has arrived. itemSelector tells the directive which elements are the items, here the articles and not the loading message.
Window Scroll #
When the page itself scrolls, add scrollWindow: the end of the list is then measured against the viewport. Without it, the directive uses the host when it scrolls, otherwise its nearest scrolling ancestor, otherwise the page. scrollContainer names another container, as an element or a CSS selector. Scroll this page down past the grid: it stops after 48 photos.
Reverse #
With direction="up", the list starts at its end and scrolledUp asks for older items when its start comes into view, like the history of a chat. The directive keeps the visible messages in place while older ones are inserted above them, so the reader does not lose their position, and keeps the list at its end when a new message arrives while the reader is already there. scrollToEnd() brings the newest message into view, after sending one for example.
DataView and Table #
Components that render all their rows, like DataView, Listbox or a Table without virtual scrolling, work the same way: put the directive on an element around them that scrolls, or name their own scroll container with scrollContainer, and append the new page to their value. Leave their paginator out. Below, a DataView in a scrolling container, then a scrollable Table whose body scrolls inside .p-datatable-table-container, made focusable through pt so that it scrolls with the keyboard.
| Code | Name | Category | Price |
|---|
Load More Button #
Infinite scrolling alone is hard to use with a keyboard: the content after the list, like a footer, moves away each time a page is added, and a page loads only when the end of the list is scrolled into view. With manual, scrolling loads nothing and a button calls loadMore(), reached through the template reference #feed="pInfiniteScroll". Binding manual to an expression gives the best of both: here the first three pages load by scrolling, then the button takes over. After a load from the button, the focus moves to the first new story, so that keyboard and screen reader users continue reading where the new content starts.
Versus VirtualScroller #
InfiniteScroll and the lazy mode of the VirtualScroller both load data while the user scrolls, but they solve different problems. InfiniteScroll appends pages to a list that renders all of its items, so the DOM grows with every page. The Scroller renders only the rows in view, with a fixed item size, and asks for the rows of any range the user jumps to, so it stays fast with hundreds of thousands of rows.
| Feature | InfiniteScroll | Scroller lazy loading |
|---|---|---|
| Rendered items | All the items loaded so far | Only the visible rows and a buffer |
| Item size | Any, variable, images, cards, grids | Fixed, given with itemSize |
| Total count | Not needed, hasMore says when to stop | Needed to size the scrollbar |
| Data access | Next page from a cursor or an offset, in order | Any range, out of order |
| Reverse loading | Yes, with the scroll position kept | No |
| Browser find, links to items, printing | Work on everything loaded | Only on the rendered rows |
| Works with | Any list: @for, DataView, Listbox, Table without virtual scrolling | p-scroller, and the virtual scroll option of Table, Listbox, Select... |
Choose InfiniteScroll for feeds, search results, galleries, chats and lists of a few hundred to a few thousand items of varying size, read from start to end. Choose the Scroller for very long uniform lists or tables where the user jumps around and only the visible rows should exist in the DOM. Choose a paginator when users need to come back to a known page.
Accessibility #
Screen Reader
Mark the list up with the WAI-ARIA feed pattern: the container has the feed role and a label, each item is an article that can take the focus, labelled by its title, with aria-posinset for its position and aria-setsize for the total, or -1 while it is unknown. The directive sets aria-busy="true" on its host while loading is true, so that assistive technologies wait for the new page before reading the changes.
A polite live region next to the host announces "Loading more items" when a page is asked for, then "10 items loaded" when the new items are in the list, and "End of the list" when hasMore becomes false. The texts are replaced through labels, where {count} stands for the number of new items, and announce turns the announcements off when the application makes its own.
<div pInfiniteScroll itemSelector="article" [loading]="loading()" [hasMore]="hasMore()" (scrolled)="load()"
role="feed" aria-label="Latest news"
[labels]="{ loading: 'Loading more news', loaded: '{count} news loaded', end: 'No more news' }">
@for (item of news(); track item.id; let i = $index) {
<article tabindex="0" [attr.aria-labelledby]="'news-' + item.id"
[attr.aria-posinset]="i + 1" [attr.aria-setsize]="hasMore() ? -1 : news().length">
<h3 [id]="'news-' + item.id">{{ item.title }}</h3>
</article>
}
</div>Keyboard Support
Loading by scrolling alone does not suit keyboard users: the content after the list keeps moving away. The directive moves the focus between the items, as in the feed pattern, and Page Down on the last item loads the next page. Offer a load more button as well, with manual, at least after a few pages, so that the footer can be reached, and move the focus to the first new item after a load.
| Key | Function |
|---|---|
| page down | Moves focus to the next item. On the last item, loads more items in the down direction. |
| page up | Moves focus to the previous item. On the first item, loads older items in the up direction. |
| ctrl / cmd + end | Moves focus to the first focusable element after the list. |
| ctrl / cmd + home | Moves focus to the last focusable element before the list. |
| tab | Moves focus through the items and their content, then to the load more button. |
The keys are ignored in text fields, and keyboardNavigation turns them off.