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.
INV-1042Maya Okafor$4,800.00INV-1041Sam Jensen$1,250.00INV-1040Ella Marsh$980.00INV-1039Logoipsum Inc.$3,400.00
Options
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>
);
}/* Pagination on a small table card: page numbers in a soft track with a
raised pill that slides to the current page, and a compact "5 of 12" in
the header. Sizes are in em, on a base a little under the text around it. */
.dh-pages-card {
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
overflow: hidden;
width: 30em;
max-width: 100%;
border-radius: 1em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 0.8125em;
line-height: 1.54;
text-align: left;
}
.dh-pages-card :is(ul, p) {
margin: 0;
padding: 0;
list-style: none;
}
.dh-pages-card > header,
.dh-pages-card > footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.9em;
}
.dh-pages-card > header {
padding: 0.77em 0.77em 0.77em 1.23em;
}
.dh-pages-card > header > b {
font-size: 1.077em;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-pages-card > footer {
padding: 0.62em 0.77em 0.62em 1.23em;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, transparent);
color: var(--dh-muted, #5c5c5c);
font-size: 1em;
}
.dh-pages-card > footer > p {
font-size: 0.846em;
white-space: nowrap;
}
.dh-pages-rows li {
display: flex;
align-items: center;
gap: 0.9em;
height: 2.77em;
margin: 0 1.23em;
border-top: 1px solid var(--line);
}
.dh-pages-rows code {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.846em;
}
.dh-pages-rows span {
flex: 1;
overflow: hidden;
min-width: 0;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-pages-rows em {
font-style: normal;
font-variant-numeric: tabular-nums;
}
.dh-pages {
display: flex;
align-items: center;
gap: 0.46em;
}
.dh-pages button {
display: grid;
place-items: center;
padding: 0;
border: 0;
background: none;
color: var(--dh-muted, #5c5c5c);
font: inherit;
font-variant-numeric: tabular-nums;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
}
.dh-pages button:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
/* Previous and next: small raised buttons. */
.dh-pages .dh-pages-step {
width: 2em;
height: 2em;
border-radius: 0.54em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
transition:
transform 150ms var(--ease),
opacity 150ms var(--ease);
}
.dh-pages-step svg {
width: 0.92em;
height: 0.92em;
fill: currentColor;
}
.dh-pages-step:active {
transform: scale(0.94);
}
.dh-pages-step:disabled {
opacity: 0.4;
cursor: default;
}
/* The numbers: seven equal slots in a track, and a pill under the current one. */
.dh-pages-list {
--slot: 2em;
position: relative;
display: flex;
padding: 2px;
border-radius: 0.62em;
background: var(--dh-well, #ededeb);
}
.dh-pages-list button {
position: relative;
z-index: 1;
width: 2.167em;
height: 2.167em;
border-radius: 0.5em;
font-size: 0.923em;
transition: color 150ms var(--ease);
}
.dh-pages-list button[aria-current="page"] {
color: var(--dh-fg, #171717);
font-weight: 500;
}
.dh-pages-list button:disabled {
color: var(--dh-faint, #8a8a8a);
cursor: default;
}
.dh-pages-mark {
position: absolute;
top: 2px;
bottom: 2px;
left: 2px;
width: var(--slot);
border-radius: 0.46em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 14%, transparent);
translate: calc(var(--i, 0) * var(--slot)) 0;
transition: translate 320ms var(--ease);
}
.dh-pages-count {
min-width: 4.4em;
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.846em;
text-align: center;
white-space: nowrap;
}
.dh-pages-count b {
color: var(--dh-fg, #171717);
font-weight: 500;
}
@media (hover: hover) and (pointer: fine) {
.dh-pages-list button:not(:disabled):hover {
color: var(--dh-fg, #171717);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-pages-mark {
transition: none;
}
.dh-pages-step:active {
transform: none;
}
}<div class="dh-pages-card" data-pages>
<header>
<b>Invoices</b>
<nav class="dh-pages" aria-label="Pagination, compact" data-total="12" data-page="5">
<button type="button" class="dh-pages-step" data-step="-1" aria-label="Previous page">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="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" /></svg>
</button>
<p class="dh-pages-count" aria-live="polite"><b>5</b> of 12</p>
<button type="button" class="dh-pages-step" data-step="1" aria-label="Next page">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="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" /></svg>
</button>
</nav>
</header>
<ul class="dh-pages-rows">
<li><code>INV-1042</code><span>Maya Okafor</span><em>$4,800.00</em></li>
<li><code>INV-1041</code><span>Sam Jensen</span><em>$1,250.00</em></li>
<li><code>INV-1040</code><span>Ella Marsh</span><em>$980.00</em></li>
<li><code>INV-1039</code><span>Logoipsum Inc.</span><em>$3,400.00</em></li>
</ul>
<footer>
<p data-range="4">17 to 20 of 48</p>
<nav class="dh-pages" aria-label="Pagination" data-total="12" data-page="5" style="--i: 3">
<button type="button" class="dh-pages-step" data-step="-1" aria-label="Previous page">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="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" /></svg>
</button>
<div class="dh-pages-list">
<i class="dh-pages-mark" aria-hidden="true"></i>
<button type="button" aria-label="Page 1">1</button>
<button type="button" disabled aria-hidden="true">…</button>
<button type="button" aria-label="Page 4">4</button>
<button type="button" aria-label="Page 5" aria-current="page">5</button>
<button type="button" aria-label="Page 6">6</button>
<button type="button" disabled aria-hidden="true">…</button>
<button type="button" aria-label="Page 12">12</button>
</div>
<button type="button" class="dh-pages-step" data-step="1" aria-label="Next page">
<svg viewBox="0 0 16 16" aria-hidden="true"><path d="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" /></svg>
</button>
</nav>
</footer>
</div>/* Pagination on a small table card: page numbers in a soft track with a
raised pill that slides to the current page, and a compact "5 of 12" in
the header. Sizes are in em, on a base a little under the text around it. */
.dh-pages-card {
--line: color-mix(in srgb, var(--dh-fg, #171717) 9%, transparent);
--ease: cubic-bezier(0.23, 1, 0.32, 1);
overflow: hidden;
width: 30em;
max-width: 100%;
border-radius: 1em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
font-family: inherit;
font-size: 0.8125em;
line-height: 1.54;
text-align: left;
}
.dh-pages-card :is(ul, p) {
margin: 0;
padding: 0;
list-style: none;
}
.dh-pages-card > header,
.dh-pages-card > footer {
display: flex;
align-items: center;
justify-content: space-between;
gap: 0.9em;
}
.dh-pages-card > header {
padding: 0.77em 0.77em 0.77em 1.23em;
}
.dh-pages-card > header > b {
font-size: 1.077em;
font-weight: 500;
letter-spacing: -0.015em;
}
.dh-pages-card > footer {
padding: 0.62em 0.77em 0.62em 1.23em;
border-top: 1px solid var(--line);
background: color-mix(in srgb, var(--dh-fg, #171717) 3%, transparent);
color: var(--dh-muted, #5c5c5c);
font-size: 1em;
}
.dh-pages-card > footer > p {
font-size: 0.846em;
white-space: nowrap;
}
.dh-pages-rows li {
display: flex;
align-items: center;
gap: 0.9em;
height: 2.77em;
margin: 0 1.23em;
border-top: 1px solid var(--line);
}
.dh-pages-rows code {
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.846em;
}
.dh-pages-rows span {
flex: 1;
overflow: hidden;
min-width: 0;
text-overflow: ellipsis;
white-space: nowrap;
}
.dh-pages-rows em {
font-style: normal;
font-variant-numeric: tabular-nums;
}
.dh-pages {
display: flex;
align-items: center;
gap: 0.46em;
}
.dh-pages button {
display: grid;
place-items: center;
padding: 0;
border: 0;
background: none;
color: var(--dh-muted, #5c5c5c);
font: inherit;
font-variant-numeric: tabular-nums;
cursor: pointer;
outline: none;
-webkit-tap-highlight-color: transparent;
}
.dh-pages button:focus-visible {
box-shadow: 0 0 0 2px var(--dh-faint, #8a8a8a);
}
/* Previous and next: small raised buttons. */
.dh-pages .dh-pages-step {
width: 2em;
height: 2em;
border-radius: 0.54em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 10%, transparent);
color: var(--dh-fg, #171717);
transition:
transform 150ms var(--ease),
opacity 150ms var(--ease);
}
.dh-pages-step svg {
width: 0.92em;
height: 0.92em;
fill: currentColor;
}
.dh-pages-step:active {
transform: scale(0.94);
}
.dh-pages-step:disabled {
opacity: 0.4;
cursor: default;
}
/* The numbers: seven equal slots in a track, and a pill under the current one. */
.dh-pages-list {
--slot: 2em;
position: relative;
display: flex;
padding: 2px;
border-radius: 0.62em;
background: var(--dh-well, #ededeb);
}
.dh-pages-list button {
position: relative;
z-index: 1;
width: 2.167em;
height: 2.167em;
border-radius: 0.5em;
font-size: 0.923em;
transition: color 150ms var(--ease);
}
.dh-pages-list button[aria-current="page"] {
color: var(--dh-fg, #171717);
font-weight: 500;
}
.dh-pages-list button:disabled {
color: var(--dh-faint, #8a8a8a);
cursor: default;
}
.dh-pages-mark {
position: absolute;
top: 2px;
bottom: 2px;
left: 2px;
width: var(--slot);
border-radius: 0.46em;
background: var(--dh-raised, #fff);
box-shadow:
0 0 0 1px var(--line),
0 1px 2px color-mix(in srgb, var(--dh-fg, #171717) 14%, transparent);
translate: calc(var(--i, 0) * var(--slot)) 0;
transition: translate 320ms var(--ease);
}
.dh-pages-count {
min-width: 4.4em;
color: var(--dh-faint, #8a8a8a);
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
font-size: 0.846em;
text-align: center;
white-space: nowrap;
}
.dh-pages-count b {
color: var(--dh-fg, #171717);
font-weight: 500;
}
@media (hover: hover) and (pointer: fine) {
.dh-pages-list button:not(:disabled):hover {
color: var(--dh-fg, #171717);
}
}
@media (prefers-reduced-motion: reduce) {
.dh-pages-mark {
transition: none;
}
.dh-pages-step:active {
transform: none;
}
}/* Works out which numbers to show for the current page, fills the slots,
and slides the pill to the slot the current page is in. Pagers inside the
same [data-pages] element stay on the same page. */
document.querySelectorAll(".dh-pages").forEach((nav) => {
const total = Number(nav.dataset.total);
const slots = Array.from(nav.querySelectorAll(".dh-pages-list button"));
const count = nav.querySelector(".dh-pages-count b");
const group = nav.closest("[data-pages]");
let page = Number(nav.dataset.page);
/* Up to seven slots: the first and last page, the pages around the current one, and gaps (0). */
const around = () => {
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];
};
const draw = () => {
around().forEach((number, index) => {
const slot = slots[index];
if (!slot) return;
slot.textContent = number || "…";
slot.disabled = !number;
slot.dataset.page = number;
if (number) {
slot.removeAttribute("aria-hidden");
slot.setAttribute("aria-label", "Page " + number);
} else {
slot.setAttribute("aria-hidden", "true");
slot.removeAttribute("aria-label");
}
if (number === page) {
slot.setAttribute("aria-current", "page");
nav.style.setProperty("--i", index);
} else {
slot.removeAttribute("aria-current");
}
});
if (count) count.textContent = page;
const range = group && group.querySelector("[data-range]");
if (range) {
const each = Number(range.dataset.range);
range.textContent = (page - 1) * each + 1 + " to " + page * each + " of " + total * each;
}
nav.querySelector('[data-step="-1"]').disabled = page === 1;
nav.querySelector('[data-step="1"]').disabled = page === total;
};
nav.addEventListener("click", (event) => {
const button = event.target.closest("button");
if (!button || button.disabled) return;
page = button.dataset.step ? page + Number(button.dataset.step) : Number(button.dataset.page);
draw();
if (group) group.dispatchEvent(new CustomEvent("dh-page", { detail: { page: page, from: nav } }));
});
if (group) {
group.addEventListener("dh-page", (event) => {
if (event.detail.from === nav) return;
page = event.detail.page;
draw();
});
}
draw();
});Pagination is not in the Figma file yet.
Use the React or HTML code for now.