Examples

Automatic loading

Loads when the end comes into view; the number of new items is announced.

Preview
  • Result 1
  • Result 2
  • Result 3
  • Result 4
  • Result 5
  • Result 6
  • Result 7
  • Result 8
Example.tsx
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.

Preview
  1. Changelog entry 1
  2. Changelog entry 2
  3. Changelog entry 3
  4. Changelog entry 4
  5. Changelog entry 5
Example.tsx
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

InfiniteScroll props
PropTypeDefault
hasMorerequired
More items exist.
boolean—
onLoadMorerequired
Load the next page.
() => void—
loading
A page is loading (marks the region busy).
boolean—
itemCount
Items loaded so far, used to announce how many were added.
number—
auto
Load when the end comes into view.
booleantrue
rootMargin
How early to start loading.
string'200px'
messages
Button, 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

VariableDefault
--pl-infinite-scroll-paddingvar(--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-busy while loading, and “N more items, M in total.” is announced afterwards.
  • Focus stays where it was when items are added.
Keyboard interactions
KeyAction
TabReach the “Load more” button after the last item
Enter / SpaceLoad 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.