antlitz
Menu

Pagination

Links to the other pages of a list. Renders a labelled nav around an unordered list and gives every entry the same hit target.

import { Pagination } from "antlitz";

In a Next.js app, take the entries from antlitz/next so they navigate on the client, query-string links such as ?page=2 included.

import { Link } from "antlitz/next";

API

PropTypeDefault
childrenReactNode—
labelstringPagination

Each child becomes one entry, wrapped in an li. Empty children — false, null — are skipped, so an entry rendered conditionally leaves no gap in the list. Mark the current page with aria-current="page" and it is announced as current and filled in — the attribute does both jobs, so nothing has to be styled by hand.

Like Breadcrumb, it takes children rather than a page count, so an entry can be a link, a Button that submits, or a span.

Usage

export function Basic() {
  return (
    <Pagination>
      <Link href="?page=1" variant="plain">
        1
      </Link>
      <Link href="?page=2" variant="plain" aria-current="page">
        2
      </Link>
      <Link href="?page=3" variant="plain">
        3
      </Link>
    </Pagination>
  );
}

The list is unordered. Breadcrumb uses an ordered list because its entries nest inside one another; pages sit beside each other, and a screen reader counting them off as "1 of 3" would be describing the widget rather than the content.

Truncating

export function Truncated() {
  return (
    <Pagination>
      <Link href="?page=1" variant="plain">
        1
      </Link>
      <span aria-hidden="true">…</span>
      <Link href="?page=6" variant="plain">
        6
      </Link>
      <Link href="?page=7" variant="plain" aria-current="page">
        7
      </Link>
      <Link href="?page=8" variant="plain">
        8
      </Link>
      <span aria-hidden="true">…</span>
      <Link href="?page=20" variant="plain">
        20
      </Link>
    </Pagination>
  );
}

The ellipsis is aria-hidden, so the trail is announced as the pages that are actually reachable.

Which pages to show is not decided here. The range around the current page — how many siblings, where the gaps fall — depends on how long the list is and how wide the layout is, and a component that guessed would be wrong often enough to need overriding every time. It is a few lines of arithmetic that belong next to the data.

Previous and next

export function PrevNext() {
  return (
    <Pagination>
      <Link href="?page=1" variant="plain" aria-label="Previous page">
        <Icon size={16}>
          <path d="m15 18-6-6 6-6" />
        </Icon>
      </Link>
      <Link href="?page=1" variant="plain">
        1
      </Link>
      <Link href="?page=2" variant="plain" aria-current="page">
        2
      </Link>
      <Link href="?page=3" variant="plain">
        3
      </Link>
      <Link href="?page=3" variant="plain" aria-label="Next page">
        <Icon size={16}>
          <path d="m9 18 6-6-6-6" />
        </Icon>
      </Link>
    </Pagination>
  );
}

An arrow with no text needs a name. Label the link rather than the icon: the link is what is announced.

At the edges

export function Edges() {
  return (
    <Pagination>
      <span aria-disabled="true" aria-label="Previous page">
        <Icon size={16}>
          <path d="m15 18-6-6 6-6" />
        </Icon>
      </span>
      <Link href="?page=1" variant="plain" aria-current="page">
        1
      </Link>
      <Link href="?page=2" variant="plain">
        2
      </Link>
      <Link href="?page=2" variant="plain" aria-label="Next page">
        <Icon size={16}>
          <path d="m9 18 6-6-6-6" />
        </Icon>
      </Link>
    </Pagination>
  );
}

On the first page there is no previous page to link to. Render the arrow as a non-link carrying aria-disabled="true" — Pagination dims it and takes it out of the pointer flow. A disabled a with an href would still be focusable and still navigate.