Navigation
Pagination
Buttons for moving between pages, with an ellipsis for the ones it doesn't show.
- Element
<slab-pagination>- Built on
<nav> + <button>s- As Markdown
- pagination.md
Example
Here's a styled version, <slab-styled-pagination>. It's not
in the package — it's an example of what you can build with the
headless element. Copy it and make it yours at the
bottom of this page. Switch to "As it ships" to see the same markup
with the headless element and no CSS at all. That's your starting
point.
<slab-styled-pagination label="Pour log" page="5" total="12"></slab-styled-pagination>
<slab-pagination label="Pour log" page="5" total="12"></slab-pagination>
Usage
pnpm add slab-elements lit
<script type="module">
import 'slab-elements/pagination.js';
</script>
<slab-pagination page="1" total="20"></slab-pagination>
When it dispatches “change”, read the new page and navigate.
Behavior
Pagination with no visual styles: a <nav> around a list of <button>s, one for each page shown, between a previous and a next one.
The first and last pages are always shown, and so are siblings pages either side of the current one. The pages between those are an ellipsis, so there are always as many items, and the buttons don't move as the page changes.
The current page's button has aria-current="page". Previous and Next are aria-disabled rather than disabled at either end, so focus stays on them when they reach it. Clicking a page sets page and dispatches "change".
Style it through its parts and :state(first) and :state(last), which are set on the first and last pages. The current page's button is also ::part(current).
API
<slab-pagination>
| Name | Type | Default | Description |
|---|---|---|---|
total
Required
|
number |
How many pages there are. | |
label
|
string |
Names the navigation. "Pagination" when unset. | |
next-label
|
string |
The next button's text and so its accessible name. "Next" when unset. | |
page
|
number |
1 |
The current page, from 1 to total. |
previous-label
|
string |
The previous button's text and so its accessible name. "Previous" when unset. | |
siblings
|
number |
1 |
How many pages are shown either side of the current one. |
version
|
string |
| Name | Type | Description |
|---|---|---|
pageLabel
|
(page: number) => string |
Names each page's button, which shows only the number, from that number. "Page" and the number when unset. |
| Name | Type | Description |
|---|---|---|
change
|
Event |
| Name | Description |
|---|---|
current
|
The current page's button |
ellipsis
|
In place of the pages that aren't shown |
item
|
Each button's or ellipsis's list item |
list
|
The buttons |
nav
|
The navigation |
next
|
The button to the next page |
page
|
Each page's button |
previous
|
The button to the previous page |
Copy and own it
<slab-styled-pagination> isn't in the package. It's an example —
copy it into your project and change whatever you want. Under the
hood, it's just the headless element, subclassed, with
styles set. That's it. Keyboard, focus, ARIA, forms, and
events all come from the headless element, and the styles only reach
in through ::part() and :state(), same as
yours would.
To use it, copy these files, swap the tag in
@customElement() for one of your own, and import it.
tokens.styles.ts holds the colors and fonts every styled
example shares, so you only need it once.
pagination.ts
import { customElement } from 'lit/decorators.js';
import Pagination from 'slab-elements/pagination.js';
import styles from './pagination.styles.js';
import tokens from './tokens.styles.js';
declare global {
interface HTMLElementTagNameMap {
'slab-styled-pagination': StyledPagination;
}
}
/**
* Pagination, styled: a course of square buttons, with the current page in
* ink.
*/
@customElement('slab-styled-pagination')
export default class StyledPagination extends Pagination {
static override styles = [tokens, styles];
}
pagination.styles.ts
import { css } from 'lit';
/*
At the first and last pages, Previous and Next are "aria-disabled" rather
than disabled, so they keep focus. They're styled from Pagination's
":state(first)" and ":state(last)".
*/
export default css`
[part~='list'] {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
list-style: none;
margin: 0;
padding: 0;
}
[part~='page'],
[part~='previous'],
[part~='next'] {
background-color: var(--paper);
block-size: 2.5rem;
border: var(--border);
box-shadow: 3px 3px 0 var(--ink);
color: var(--ink);
cursor: pointer;
font-family: var(--font-display);
font-size: 1rem;
font-weight: 600;
min-inline-size: 2.5rem;
padding: 0 0.75rem;
transition:
background-color 100ms,
box-shadow 100ms;
&:hover {
background-color: var(--chalk);
}
&:active {
box-shadow: 1px 1px 0 var(--ink);
}
&:focus-visible {
outline: 3px solid var(--ember);
outline-offset: 3px;
}
}
[part~='current'] {
background-color: var(--ink);
box-shadow: 3px 3px 0 var(--ember);
color: var(--paper);
&:hover {
background-color: var(--ink);
}
}
[part~='ellipsis'] {
color: var(--ash);
font-family: var(--font-mono);
padding-inline: 0.25rem;
}
:host(:state(first)) [part~='previous'],
:host(:state(last)) [part~='next'] {
background-color: var(--chalk);
border-style: dashed;
box-shadow: none;
color: var(--ash);
cursor: not-allowed;
}
@media (prefers-reduced-motion: reduce) {
[part~='page'],
[part~='previous'],
[part~='next'] {
transition: none;
}
}
`;
tokens.styles.ts
import { css } from 'lit';
/*
The styled components' palette and type, taken from the logo: ink for the
courses, ember for the base, paper behind them.
Each is a short alias for a "--slab-styled-*" property, with the default as
its fallback. Retheming is "--slab-styled-ember" and the rest on ":root",
which every shadow root inherits. Were the defaults declared under those
names here instead, each host's own declaration would shadow the page's.
Each default is "light-dark()", so the components follow the page's
"color-scheme": "dark" for dark, "light" for light, and "light dark" to
follow the operating system. Ink and paper trade places in the dark, so
ink is always the text and paper always what's behind it. Contrast is given
as light, then dark.
*/
export default css`
:host {
/* Secondary text. 6.8:1 and 6.9:1 on paper. */
--ash: var(--slab-styled-ash, light-dark(#5e5a55, #a8a19a));
--border: 2px solid var(--ink);
/* Behind disabled and read-only controls, and hovered rows. */
--chalk: var(--slab-styled-chalk, light-dark(#f4f1ec, #2b2926));
/*
The logo's orange, brighter in the dark. 4.25:1 and 5.2:1 on paper is
enough for borders, fills and large text, but not always for small text,
which takes "--ember-deep".
*/
--ember: var(--slab-styled-ember, light-dark(#d9492a, #ec5f3b));
/*
Orange for small text on paper, and for paper text on it. 5.7:1 and
7.4:1.
*/
--ember-deep: var(--slab-styled-ember-deep, light-dark(#b3381a, #ff8566));
--font-body: var(
--slab-styled-font-body,
'IBM Plex Sans',
system-ui,
sans-serif
);
--font-display: var(
--slab-styled-font-display,
'Zilla Slab',
rockwell,
'Roboto Slab',
serif
);
--font-mono: var(
--slab-styled-font-mono,
'IBM Plex Mono',
ui-monospace,
monospace
);
--ink: var(--slab-styled-ink, light-dark(#1c1c1c, #f4f1ec));
--paper: var(--slab-styled-paper, light-dark(#fff, #1a1918));
/* The hard, offset shadow a raised surface casts. */
--shadow: 4px 4px 0 var(--ink);
}
`;