Pagination
The bar under any list or table that is longer than a page: it says how many things there are, which ones are on screen, and lets the person move.
import { Pagination } from '@sypnia/ui/react'
Moving through pages
First, last, the current page and its neighbours; the rest are gaps.
51–75 of 1,204
Code
import { useState } from 'react';
import { Pagination } from '@sypnia/ui/react';
// How many there are, which ones are on screen, and how to move. On a phone: previous, «Page 3 of 49», next.
export default function Basic() {
const [page, setPage] = useState(3);
return <Pagination page={page} pageSize={25} total={1204} onPage={setPage} />;
} Choosing how many per page
Change the size: the count and the pages follow.
1–25 of 318
Code
import { useState } from 'react';
import { Pagination } from '@sypnia/ui/react';
// With onPageSize the person chooses how many per page. Changing it goes back to the first page.
export default function PageSize() {
const [page, setPage] = useState(1);
const [size, setSize] = useState(25);
return <Pagination page={page} pageSize={size} total={318} onPage={setPage} onPageSize={(s) => { setSize(s); setPage(1); }} />;
} Under a list
A list of customers, four at a time.
Customers
LMLaura MéndezDRDiego RuizACAna CastañoMSMarco Silva
Code
import { useState } from 'react';
import { Identity, Pagination, Panel } from '@sypnia/ui/react';
const names = ['Laura Méndez', 'Diego Ruiz', 'Ana Castaño', 'Marco Silva', 'Julia Prieto', 'Tomás Vera', 'Sara Lozano', 'Iván Peña', 'Clara Ossa', 'Felipe Rey', 'Nora Vidal', 'Hugo Marín', 'Elena Soto', 'Pablo Gil'];
const size = 4;
// Any list longer than a page paginates, not only tables.
export default function WithAList() {
const [page, setPage] = useState(1);
const shown = names.slice((page - 1) * size, page * size);
return (
<Panel title="Customers" footer={<div className="sy-grow"><Pagination page={page} pageSize={size} total={names.length} onPage={setPage} /></div>}>
<div className="sy-stack">
{shown.map((n) => <Identity key={n} name={n} meta={`${n.split(' ')[0].toLowerCase()}@northwind.io`} />)}
</div>
</Panel>
);
} One page, the last page, nothing
The bar stays truthful in every case.
1–18 of 18
1,201–1,204 of 1,204
0–0 of 0
Code
import { Pagination } from '@sypnia/ui/react';
// One page, the last page, and nothing at all: the bar always says the truth.
export default function Edges() {
return (
<div className="sy-stack">
<Pagination page={1} pageSize={25} total={18} onPage={() => {}} />
<Pagination page={49} pageSize={25} total={1204} onPage={() => {}} />
<Pagination page={1} pageSize={25} total={0} onPage={() => {}} />
</div>
);
} When to use it
Use it when
- Any list or table that can grow past one page. This is a rule of Sypnia, not an option.
- Table uses it by itself; use it directly under lists, grids of cards and search results.
- Let the person choose how many per page with
onPageSizewhen the rows are short.
Do not use it when
- The list always fits on one page.
- The total is not known, or the list is a feed: CursorPagination or LoadMore.
- You would hide the count. «51–75 of 1,204» is what tells people where they are.
For everyone
- It is a navigation region named «Pages». Each number is a button named «Go to page 4», and the current one is marked.
- The summary («51–75 of 1,204») is a live region: screen readers say it after each move.
- «Previous» and «Next» are disabled at the ends instead of disappearing, so nothing jumps.
- On phones it becomes previous, «Page 3 of 49», next: three large targets. Numbers follow the language of the page.
Props
| Pagination | Type | Default |
|---|---|---|
onPagerequired | (page: number) => void | |
pagerequiredCurrent page, starting at 1. | number | |
pageSizerequired | number | |
totalrequiredHow many items exist in total. | number | |
loadingThe rows are on their way: the bar is drawn in its place, every word and button as a skeleton of its own box. Pass the last known `total`. | boolean | |
onPageSizeWith it, the person can choose how many per page. | ((size: number) => void) | |
pageSizes | number[] | [10, 25, 50, 100] |