Skip to content

Pagination

The footer of a list: Pagination pages a list the browser holds ("16–30 of 120"); CursorPagination pages a server list that only knows whether another page exists. Both render nothing when there is nowhere to go.

Language
Appearance

A list in the browser

  • Contact 1
  • Contact 2
  • Contact 3
  • Contact 4
  • Contact 5
  • Contact 6
  • Contact 7
  • Contact 8
  • Contact 9
  • Contact 10
  • Contact 11
  • Contact 12
  • Contact 13
  • Contact 14
  • Contact 15
import { useState } from 'react';
import { PAGE_SIZE, Pagination } from '@arbol-ai/ui';

const CONTACTS = Array.from({ length: 52 }, (_, index) => `Contact ${index + 1}`);

const ContactsExample = () => {
  const [page, setPage] = useState(1);
  return (
    <div className="flex max-w-sm flex-col gap-3">
      <ul className="text-sm">
        {CONTACTS.slice((page - 1) * PAGE_SIZE, page * PAGE_SIZE).map((contact) => (
          <li key={contact}>{contact}</li>
        ))}
      </ul>
      <Pagination onPage={setPage} page={page} total={CONTACTS.length} />
    </div>
  );
};

<ContactsExample />

A server list with cursors

While a page loads, the pressed button waits with a spinner and keeps focus.

import { useState } from 'react';
import { CursorPagination } from '@arbol-ai/ui';

const ConversationsExample = () => {
  const [page, setPage] = useState(1);
  const [isFetching, setIsFetching] = useState(false);
  const go = (next: number) => {
    setIsFetching(true);
    setTimeout(() => {
      setPage(next);
      setIsFetching(false);
    }, 800);
  };
  return (
    <div className="max-w-sm">
      <CursorPagination
        hasNextPage={page < 4}
        hasPreviousPage={page > 1}
        isFetching={isFetching}
        onNext={() => {
          go(page + 1);
        }}
        onPrevious={() => {
          go(page - 1);
        }}
        page={page}
      />
    </div>
  );
};

<ConversationsExample />

Source

The examples above are src/components/pagination/pagination.examples.tsx, which the library’s tests render too.

Type to search…

↑↓ navigate↵ selectEsc close