# Pagination usage guidelines

> Discover Cimpress UI's pagination component.

Source: https://ui.cimpress.io/components/pagination/

Pagination allows navigating across multiple pages of related content.

### When to use

- When displaying large datasets that cannot fit on a single page (e.g., search results, tables, or product listings).
- To improve page load performance by only rendering a subset of data.
- To enhance readability by breaking long lists into manageable sections.

### When not to use

- For small datasets that can be displayed in a single scrollable view.
- If users need to compare multiple records across different pages, consider putting everything on a single-page view instead.

## Usage

```tsx
import { Pagination } from '@cimpress-ui/react';
import { useState } from 'react';

export default function Demo() {
  const [currentPage, setCurrentPage] = useState(1);

  return <Pagination aria-label="Pagination" currentPage={currentPage} pageCount={10} onPageChange={setCurrentPage} />;
}
```

## Formatting and layout

### Placement

Pagination controls should be placed at the bottom of the content section, with 16px spacing between the content section and the pagination component.

### Items per page

For some content like data tables and lists it may be helpful to allow users to choose how many items they see per page. A menu should be used in conjunction with the pagination component to achieve this. The menu should always be left-aligned and the pagination component should always be right-aligned.

**Do:**

```tsx
import {
  Button,
  Menu,
  MenuItem,
  MenuRoot,
  Pagination,
  Stack,
  Table,
  TableBody,
  TableBodyCell,
  TableBodyRow,
  TableContainer,
  TableHeader,
  TableHeaderCell,
  TableHeaderRow,
} from '@cimpress-ui/react';
import { IconChevronDown } from '@cimpress-ui/react/icons';
import { useState } from 'react';

interface Album {
  title: string;
  releaseDate: string;
  label: string;
  chartPosition: number;
}

const labels = ['Interscope', 'Universal'];

const albums: Album[] = Array(1000)
  .fill(0)
  .map((_, i) => ({
    title: `Album #${i + 1}`,
    releaseDate: `July 8, ${1997 + i}`,
    label: labels[i % labels.length],
    chartPosition: 1000 - i,
  }));

export default function Demo() {
  const [currentPage, setCurrentPage] = useState(1);
  const [rowsPerPage, setRowsPerPage] = useState(10);

  const firstDisplayedRowIdx = rowsPerPage * (currentPage - 1);

  return (
    <Stack gap={16}>
      <TableContainer>
        <Table id="album-table" aria-label="Albums" totalRowCount={albums.length + 1}>
          <TableHeader>
            <TableHeaderRow rowNumber={1}>
              <TableHeaderCell columnKey="title" columnWidth="40%" columnContentAlignment="start">
                Title
              </TableHeaderCell>
              <TableHeaderCell columnKey="release-date" columnWidth="20%" columnContentAlignment="end">
                Release date
              </TableHeaderCell>
              <TableHeaderCell columnKey="label" columnWidth="20%" columnContentAlignment="start">
                Label
              </TableHeaderCell>
              <TableHeaderCell columnKey="chart-position" columnWidth="20%" columnContentAlignment="end">
                Chart position
              </TableHeaderCell>
            </TableHeaderRow>
          </TableHeader>

          <TableBody>
            {albums.slice(firstDisplayedRowIdx, firstDisplayedRowIdx + rowsPerPage).map((row, index) => (
              <TableBodyRow key={row.title} rowNumber={firstDisplayedRowIdx + index + 2}>
                <TableBodyCell columnKey="title">{row.title}</TableBodyCell>
                <TableBodyCell columnKey="release-date">{row.releaseDate}</TableBodyCell>
                <TableBodyCell columnKey="label">{row.label}</TableBodyCell>
                <TableBodyCell columnKey="chart-position">{row.chartPosition}</TableBodyCell>
              </TableBodyRow>
            ))}
          </TableBody>
        </Table>
      </TableContainer>

      <Stack gap={16} direction="horizontal" justify="space-between" align="start">
        <MenuRoot>
          <Button iconEnd={<IconChevronDown />}>Per page: {rowsPerPage}</Button>
          <Menu
            selectionMode="single"
            selectedKeys={[rowsPerPage]}
            onSelectionChange={(keys) => {
              if (keys === 'all') return;

              const selectedKey = keys.values().next().value;
              setCurrentPage(1);
              setRowsPerPage(selectedKey as number);
            }}
          >
            <MenuItem id={10}>10</MenuItem>
            <MenuItem id={25}>25</MenuItem>
            <MenuItem id={50}>50</MenuItem>
            <MenuItem id={100}>100</MenuItem>
          </Menu>
        </MenuRoot>

        <Pagination
          aria-label="Table pagination"
          currentPage={currentPage}
          pageCount={Math.ceil(albums.length / rowsPerPage)}
          paginatedElementId="album-table"
          onPageChange={setCurrentPage}
        />
      </Stack>
    </Stack>
  );
}
```

### Table alignment

View the [table documentation](/components/table) to learn more about using pagination with the table component.
