pagination
import { lucide, pagination } from '@markless/ui';export default function PaginationBasic() @{ <pagination.root class="pages" aria-label="Results pages" count={5} page={3}> <pagination.backtrigger class="page-box" aria-label="Previous page"> <lucide.chevronleft aria-hidden="true" /> </pagination.backtrigger> <pagination.item value={1}> <pagination.itemtrigger class="page-box">1</pagination.itemtrigger> </pagination.item> <pagination.item value={2}> <pagination.itemtrigger class="page-box">2</pagination.itemtrigger> </pagination.item> <pagination.item value={3}> <pagination.itemtrigger class="page-box">3</pagination.itemtrigger> </pagination.item> <pagination.item value={4}> <pagination.itemtrigger class="page-box">4</pagination.itemtrigger> </pagination.item> <pagination.item value={5}> <pagination.itemtrigger class="page-box">5</pagination.itemtrigger> </pagination.item> <pagination.forwardtrigger class="page-box" aria-label="Next page"> <lucide.chevronright aria-hidden="true" /> </pagination.forwardtrigger> </pagination.root>;}
.pages { display: flex; flex-wrap: wrap; gap: 0.4em; justify-content: center; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1; color: var(--ink);}.page-box { display: grid; place-items: center; box-sizing: border-box; min-width: 2.2em; height: 2.2em; padding: 0 0.4em; border: 1px solid var(--ink); border-radius: 6px; background: var(--paper); color: var(--ink); font: inherit; font-family: 'Shantell Sans', system-ui, sans-serif; font-variant-numeric: tabular-nums; cursor: pointer;}.page-box svg { width: 1em; height: 1em;}.page-box:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.page-box[ui-active] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.page-box:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px;}.page-box:disabled,.page-box[aria-disabled='true'] { cursor: not-allowed; opacity: 0.45;}.page-box:disabled:hover { background: var(--paper);}
pagination is a row of page numbers with back and next controls. Use it when a list or a table
splits its rows across pages.
import { pagination } from '@markless/ui';
export default function Pages() @{
<pagination.root count={5} page={1} aria-label="Results pages">
<pagination.backtrigger>Previous</pagination.backtrigger>
<pagination.item value={1}>
<pagination.itemtrigger>1</pagination.itemtrigger>
</pagination.item>
<pagination.item value={2}>
<pagination.itemtrigger>2</pagination.itemtrigger>
</pagination.item>
<pagination.forwardtrigger>Next</pagination.forwardtrigger>
</pagination.root>
}You write the page number once, on pagination.item. The control inside it reads that number back.
So a button cannot say page 2 and go to page 3.
count is the number of pages, not rows. How many rows fit on a page is your data's job.
Anatomy
The <nav> landmark that holds the page controls.
pagination.rootis a<nav>landmark. It holds the current page and the page count.pagination.itemis the box for one page. Itsvalueis that page's number.pagination.itemtriggeris a<button>that goes to its item's page.pagination.itemlinkis the same control as an<a>, for pages that have real URLs.pagination.backtriggerandpagination.forwardtriggerare<button>s that step one page.
The current page's item and control write ui-active. Its control also writes aria-current="page".
The root writes ui-disabled when you set disabled.
The back control is disabled on page 1, and the forward control is disabled on the last page.
They read "Previous page" and "Next page" to a screen reader unless you write your own aria-label.
Keyboard
Every control is a native button or link, so the browser's own keys work.
| Key | What it does |
|---|---|
| Tab | Moves through the controls in page order. It skips a disabled step control. |
| Enter or Space | Goes to the focused page, or steps one page. |
Long runs of pages
Twenty buttons do not fit in a row. pagination.entries(page, count, siblingCount) returns the pages
to draw, with gaps where pages are left out. You loop over that list and draw each entry yourself:
const entries = computed(() => pagination.entries(nav.page, 20, 1));
@for (const entry of entries; key entry.key) {
<li>
@if (entry.type === 'page') {
<pagination.item value={entry.value}>
<pagination.itemtrigger>{entry.value}</pagination.itemtrigger>
</pagination.item>
} @else {
<span aria-hidden="true">…</span>
}
</li>
}The third argument is the number of pages on each side of the current one. An entry is either
{ type: 'page', value, key } or { type: 'ellipsis', key }. Loop on
entry.key, not on the index. A page keeps its key as the range slides. A gap never stands for one
page. The family shows that page instead.
Name the landmark
pagination.root writes no aria-label. A page with a site nav and a pagination has two <nav>
landmarks. A screen reader lists them by name, so give this one a name of its own.
Props on pagination.root
| Prop | Type | What it does |
|---|---|---|
count |
number |
How many pages there are. Required. |
page |
number |
The page that shows. The first page is 1, and it is the default. |
siblingCount |
number |
How many pages to show on each side of the current one. The default is 1. |
disabled |
boolean |
Stops all page changes. Every control says it is unavailable. |
onChange |
(page) => void |
Gets the new page number when a person moves to another page. |
The root passes anything a <nav> accepts to its element. pagination.item takes value, which is
required. A click on the page that already shows does not call onChange.