Components
Pagination
Previous and Next around the page numbers: 32px ghost buttons that fill under the pointer, the current page in a #666666 hairline box in White. First and last always show, an ellipsis stands for the rest.
Install
React apps get the React files; every other stack gets the web component.
npx spyglass-ds add pagination<script type="module" src="/spyglass-ds/elements/pagination.js"></script>Usage
Pages of a long list people come back to by place: a board's results, an archive of ads.
Never
- A filled White current page: one primary per view, and this is not it
- Infinite scroll for a list people need to come back to
- Pages for a short list that fits on one screen
Adds with it: packages lucide-react
Props
The web component takes the same as attributes or properties (<spyglass-pagination>); title is heading there.
<Pagination>
| Prop | Type | Default |
|---|---|---|
page | number The page shown, from 1. | |
defaultPage | number | 1 |
onPageChange | (page: number) => void | |
pages | number How many pages. | |
siblings | number Pages shown either side of the current one. | 1 |
label | string The nav's name for assistive tech. | 'Pages' |
className | string |
Examples
The same example in React and as web components; both render identically.
import { Pagination } from '@/react/components/ui/Pagination'
// The pages under a board's results: twelve pages, the second one open.
export default function Results() {
return <Pagination pages={12} defaultPage={2} label="Board pages" />
}<!-- The pages under a board's results: twelve pages, the second one open. -->
<spyglass-pagination pages="12" page="2" label="Board pages"></spyglass-pagination>Source
'use client'
import { ChevronLeft, ChevronRight, Ellipsis } from 'lucide-react'
import { useControllable } from '@/react/hooks/useControllable'
import { cn } from '@/react/lib/cn'
// Pages of a long list: Previous and Next around the page numbers, 32px ghost buttons in DM Sans 14px that fill under
// the pointer. The current page sits in a #666666 hairline box in White, the others muted; the first and last pages
// always show, `siblings` pages either side of the current one, and an ellipsis for the rest. On phones Previous and
// Next keep only their chevrons. The page is `page` with `onPageChange` (or `defaultPage` alone), from 1.
export interface PaginationProps {
/** The page shown, from 1. */
page?: number
defaultPage?: number
onPageChange?: (page: number) => void
/** How many pages. */
pages: number
/** Pages shown either side of the current one. */
siblings?: number
/** The nav's name for assistive tech. */
label?: string
className?: string
}
/** The pages to draw: numbers, and 'gap' where an ellipsis stands for the rest. */
export function pageRange(page: number, pages: number, siblings = 1): (number | 'gap')[] {
const out: (number | 'gap')[] = []
const from = Math.max(2, page - siblings)
const to = Math.min(pages - 1, page + siblings)
out.push(1)
if (from > 2) out.push(from === 3 ? 2 : 'gap')
for (let p = from; p <= to; p++) out.push(p)
if (to < pages - 1) out.push(to === pages - 2 ? pages - 1 : 'gap')
if (pages > 1) out.push(pages)
return out
}
const BASE = 'inline-flex h-control-sm min-w-control-sm cursor-pointer items-center justify-center gap-1.5 rounded-control px-2 type-ui whitespace-nowrap select-none transition-[background-color,color] duration-160 ease-out disabled:cursor-not-allowed disabled:text-fg-faint'
const IDLE = 'text-fg-muted hover:not-disabled:bg-action-ghost-hover hover:not-disabled:text-fg'
const CURRENT = 'border border-edge-strong text-fg'
export function Pagination({ page: given, defaultPage = 1, onPageChange, pages, siblings = 1, label = 'Pages', className }: PaginationProps) {
const [page, setPage] = useControllable(given, defaultPage, onPageChange)
const go = (p: number) => setPage(Math.min(pages, Math.max(1, p)))
return (
<nav data-ds="pagination" aria-label={label} className={cn('flex justify-center', className)}>
<ul className="flex items-center gap-1">
<li>
<button type="button" className={cn(BASE, IDLE)} aria-label="Go to previous page" disabled={page <= 1} onClick={() => go(page - 1)}>
<ChevronLeft size={16} strokeWidth={1.5} aria-hidden="true" />
<span className="max-sm:hidden">Previous</span>
</button>
</li>
{pageRange(page, pages, siblings).map((p, i) => (
<li key={p === 'gap' ? `gap-${i}` : p}>
{p === 'gap'
? <span className="flex size-control-sm items-center justify-center text-fg-muted" aria-hidden="true"><Ellipsis size={16} strokeWidth={1.5} /></span>
: <button type="button" className={cn(BASE, p === page ? CURRENT : IDLE)} aria-current={p === page ? 'page' : undefined} aria-label={`Page ${p}`} onClick={() => go(p)}>{p}</button>}
</li>
))}
<li>
<button type="button" className={cn(BASE, IDLE)} aria-label="Go to next page" disabled={page >= pages} onClick={() => go(page + 1)}>
<span className="max-sm:hidden">Next</span>
<ChevronRight size={16} strokeWidth={1.5} aria-hidden="true" />
</button>
</li>
</ul>
</nav>
)
}
export default Pagination'use client'
import { useCallback, useRef, useState } from 'react'
// Vue's defineModel in React: a value that is controlled when the caller passes it (value + onValueChange, as Radix
// does) and kept inside otherwise (defaultValue). setValue calls onChange either way.
export function useControllable<T>(value: T | undefined, defaultValue: T, onChange?: (value: T) => void) {
const [inner, setInner] = useState<T>(defaultValue)
const controlled = value !== undefined
const current = controlled ? value : inner
const ref = useRef(onChange)
ref.current = onChange
const set = useCallback((next: T) => {
if (!controlled) setInner(next)
ref.current?.(next)
}, [controlled])
return [current, set] as const
}// Joins class names, skipping falsy ones: cn('a', on && 'b', ['c', undefined]). It does not merge conflicting
// utilities (no tailwind-merge), as Vue's class binding does not: the library's classes are written not to compete,
// and a className from the caller is appended after them, as Vue appends a fallthrough class.
export type ClassValue = string | false | null | undefined | 0 | ClassValue[]
export function cn(...values: ClassValue[]): string {
const out: string[] = []
for (const v of values) {
if (!v) continue
if (Array.isArray(v)) {
const s = cn(...v)
if (s) out.push(s)
}
else out.push(v)
}
return out.join(' ')
}import { html } from 'lit'
import { ifDefined } from 'lit/directives/if-defined.js'
import { ChevronLeft, ChevronRight, Ellipsis } from 'lucide'
import { DsElement, define, HostLayoutController } from '@/elements/lib/element'
import { icon } from '@/elements/lib/icon'
import styles from 'virtual:tw'
// Pagination.tsx as <spyglass-pagination>: Previous and Next around the page numbers, 32px ghost buttons that fill
// under the pointer, the current page in a #666666 hairline box in White. First and last always show, `siblings`
// either side of the current page, an ellipsis for the rest. On phones Previous and Next keep only their chevrons.
// `page` (from 1) with `page-change` (detail.value).
/** The pages to draw: numbers, and 'gap' where an ellipsis stands for the rest. */
export function pageRange(page: number, pages: number, siblings = 1): (number | 'gap')[] {
const out: (number | 'gap')[] = []
const from = Math.max(2, page - siblings)
const to = Math.min(pages - 1, page + siblings)
out.push(1)
if (from > 2) out.push(from === 3 ? 2 : 'gap')
for (let p = from; p <= to; p++) out.push(p)
if (to < pages - 1) out.push(to === pages - 2 ? pages - 1 : 'gap')
if (pages > 1) out.push(pages)
return out
}
const BASE = 'inline-flex h-control-sm min-w-control-sm cursor-pointer items-center justify-center gap-1.5 rounded-control px-2 type-ui whitespace-nowrap select-none transition-[background-color,color] duration-160 ease-out disabled:cursor-not-allowed disabled:text-fg-faint'
const IDLE = 'text-fg-muted hover:not-disabled:bg-action-ghost-hover hover:not-disabled:text-fg'
const CURRENT = 'border border-edge-strong text-fg'
/**
* @summary Previous and next around the page numbers.
* @fires page-change - The page changed (detail.value; read `page`).
*/
export class Pagination extends DsElement {
static styles = [DsElement.styles, styles]
protected readonly layout = new HostLayoutController(this, () => this.renderRoot.querySelector<HTMLElement>('nav'))
static properties = { page: { type: Number, reflect: true }, pages: { type: Number }, siblings: { type: Number }, label: {} }
/** The page shown, from 1. */
page = 1
/** How many pages. */
pages = 1
/** Pages shown either side of the current one. */
siblings = 1
/** The nav's name for assistive tech. */
label = 'Pages'
private go(p: number) {
this.commit(Math.min(this.pages, Math.max(1, p)), 'page')
}
render() {
const page = this.page
return html`<nav data-ds="pagination" aria-label=${this.label} class="flex justify-center"><ul class="flex items-center gap-1">
<li><button type="button" class=${`${BASE} ${IDLE}`} aria-label="Go to previous page" ?disabled=${page <= 1} @click=${() => this.go(page - 1)}>${icon(ChevronLeft, 'chevron-left', { size: 16, strokeWidth: 1.5, attrs: { 'aria-hidden': 'true' } })}<span class="max-sm:hidden">Previous</span></button></li>
${pageRange(page, this.pages, this.siblings).map((p) => html`<li>${p === 'gap'
? html`<span class="flex size-control-sm items-center justify-center text-fg-muted" aria-hidden="true">${icon(Ellipsis, 'ellipsis', { size: 16, strokeWidth: 1.5 })}</span>`
: html`<button type="button" class=${`${BASE} ${p === page ? CURRENT : IDLE}`} aria-current=${ifDefined(p === page ? 'page' : undefined)} aria-label=${`Page ${p}`} @click=${() => this.go(p)}>${p}</button>`}</li>`)}
<li><button type="button" class=${`${BASE} ${IDLE}`} aria-label="Go to next page" ?disabled=${page >= this.pages} @click=${() => this.go(page + 1)}><span class="max-sm:hidden">Next</span>${icon(ChevronRight, 'chevron-right', { size: 16, strokeWidth: 1.5, attrs: { 'aria-hidden': 'true' } })}</button></li>
</ul></nav>`
}
}
define('pagination', Pagination)Reference: Derived from the brand guidelines (no pagination in the brand assets): the ghost button, the neutral steps for the hairline and muted numbers.