Pagination

Pagination on a small table card: page numbers in a soft track with a raised pill that slides to the current page, previous and next, and a compact "5 of 12" in the header.

Invoices
  • INV-1042Maya Okafor$4,800.00
  • INV-1041Sam Jensen$1,250.00
  • INV-1040Ella Marsh$980.00
  • INV-1039Logoipsum Inc.$3,400.00

17 to 20 of 48

Options

Size

Code

import { useState } from "react";
import "./pagination.css";

const back = "M9.72 3.22a.75.75 0 0 1 1.06 1.06L7.06 8l3.72 3.72a.75.75 0 1 1-1.06 1.06L5.47 8.53a.75.75 0 0 1 0-1.06l4.25-4.25Z";
const on = "M6.28 3.22a.75.75 0 0 0-1.06 1.06L8.94 8l-3.72 3.72a.75.75 0 1 0 1.06 1.06l4.25-4.25a.75.75 0 0 0 0-1.06L6.28 3.22Z";

/* Up to seven slots: the first and last page, the pages around the current one, and gaps (0). */
function around(page, total) {
  if (total <= 7) return Array.from({ length: total }, (unused, index) => index + 1);
  if (page <= 4) return [1, 2, 3, 4, 5, 0, total];
  if (page >= total - 3) return [1, 0, total - 4, total - 3, total - 2, total - 1, total];
  return [1, 0, page - 1, page, page + 1, 0, total];
}

/* Page numbers with a pill that slides to the current page, or a compact "5 of 12".

   <Pagination total={12} page={page} onChange={setPage} /> */
export function Pagination({ total, page, onChange, compact = false, label = "Pagination" }) {
  const slots = around(page, total);

  const step = (by, name, path) => (
    <button
      type="button"
      className="dh-pages-step"
      aria-label={name}
      disabled={by < 0 ? page === 1 : page === total}
      onClick={() => onChange(page + by)}
    >
      <svg viewBox="0 0 16 16" aria-hidden="true">
        <path d={path} />
      </svg>
    </button>
  );

  return (
    <nav className="dh-pages" aria-label={label} style={compact ? undefined : { "--i": slots.indexOf(page) }}>
      {step(-1, "Previous page", back)}
      {compact ? (
        <p className="dh-pages-count" aria-live="polite">
          <b>{page}</b> of {total}
        </p>
      ) : (
        <div className="dh-pages-list">
          <i className="dh-pages-mark" aria-hidden="true" />
          {slots.map((number, index) =>
            number ? (
              <button
                type="button"
                key={index}
                aria-label={"Page " + number}
                aria-current={number === page ? "page" : undefined}
                onClick={() => onChange(number)}
              >
                {number}
              </button>
            ) : (
              <button type="button" key={index} disabled aria-hidden="true">
                …
              </button>
            ),
          )}
        </div>
      )}
      {step(1, "Next page", on)}
    </nav>
  );
}

export function Example() {
  const [page, setPage] = useState(5);
  const rows = [
    ["INV-1042", "Maya Okafor", "$4,800.00"],
    ["INV-1041", "Sam Jensen", "$1,250.00"],
    ["INV-1040", "Ella Marsh", "$980.00"],
    ["INV-1039", "Logoipsum Inc.", "$3,400.00"],
  ];

  return (
    <div className="dh-pages-card">
      <header>
        <b>Invoices</b>
        <Pagination total={12} page={page} onChange={setPage} compact label="Pagination, compact" />
      </header>
      <ul className="dh-pages-rows">
        {rows.map(([id, name, amount]) => (
          <li key={id}>
            <code>{id}</code>
            <span>{name}</span>
            <em>{amount}</em>
          </li>
        ))}
      </ul>
      <footer>
        <p>
          {(page - 1) * 4 + 1} to {page * 4} of 48
        </p>
        <Pagination total={12} page={page} onChange={setPage} />
      </footer>
    </div>
  );
}
Invoices
  • INV-1042Maya Okafor$4,800.00
  • INV-1041Sam Jensen$1,250.00
  • INV-1040Ella Marsh$980.00
  • INV-1039Logoipsum Inc.$3,400.00

17 to 20 of 48