仕組み
A pager that handles page numbers only; fetch or slice your data in onPageChange. The first and last page stay visible, with siblingCount pages on each side of the current one and ellipses in the gaps. The current page is an ink pill that slides from the old cell to the new one; its position is computed in CSS from the cell index, so nothing is measured. The number of cells is constant and each cell is sized by the digit count of the last page, so the pager keeps its width as you page through. Page and page size work controlled or uncontrolled. On touch screens the controls grow to 44px and the numbers collapse to “3 / 12”.
向いている場面
- Lists, card walls and search results that need a visible total and direct access to a page.
- Under a table that has no pager of its own.
- Narrow places such as sidebars and dialog footers: use simple mode.
向かない場面
- Feeds that people only scroll down: use “load more” or infinite loading.
- Cursor-based APIs without a total: offer previous and next only.
- A dozen items in total: show them all.
使い方
import { useState } from 'react'
import { Pagination } from './Pagination.jsx'
// Controlled: fetch or slice the data in onPageChange.
export function RouteList({ total }) {
const [page, setPage] = useState(1)
const [pageSize, setPageSize] = useState(20)
return (
<Pagination
label="Routes pagination"
total={total}
page={page}
onPageChange={setPage}
pageSize={pageSize}
onPageSizeChange={setPageSize}
pageSizeOptions={[20, 50, 100]}
showTotal
showJumper
/>
)
}
// Narrow space: compact and uncontrolled.
<Pagination simple size="sm" total={128} onPageChange={(page) => load(page)} />プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
total | number | — | Total number of items. Required. The page count is total ÷ page size rounded up, at least 1. |
page | number | — | Controlled current page, starting at 1. Out-of-range values are clamped. |
defaultPage | number | 1 | Initial page when uncontrolled. |
onPageChange | (page, pageSize) => void | — | Called when the page changes, including when a new page size moves the page. |
pageSize | number | — | Controlled page size. |
defaultPageSize | number | 10 | Initial page size when uncontrolled. |
onPageSizeChange | (pageSize) => void | — | Called when the page size changes. |
pageSizeOptions | number[] | — | Page sizes to offer, such as [10, 20, 50]. The menu appears only when this is set. |
siblingCount | number | 1 | Page numbers shown on each side of the current page. Use 0 in narrow spaces. |
showTotal | boolean | ((total, [from, to]) => ReactNode) | false | Shows the total. Pass a function to write your own text; it receives the range of the current page. |
showJumper | boolean | false | Shows a digits-only jump field that commits on Enter or blur, then clears. |
simple | boolean | false | Compact mode: previous, “2 / 12”, next. |
size | 'sm' | 'md' | 'lg' | 'md' | Button height 28 / 36 / 44px. Use sm under a dense table. |
disabled | boolean | false | Disables every control, for example while loading. |
label | string | 'Pagination' | Accessible name of the navigation region. Give each pager on a page its own name. |
labels | object | — | Replaces built-in text (previous, next, page, pageOf, total, pageSize, perPage, jumpBefore, jumpAfter, jump). Shallow-merged over the English defaults. |
...rest | HTMLAttributes | — | Other attributes, such as className and style, go to the root <nav>. |
アクセシビリティ
- The root is a <nav> with a label; each page is a button named “Page 3”, and the current one carries aria-current="page".
- Inside the pager, Left and Right move one page, Home and End go to the first and last; focus lands on the new current page.
- When a step button becomes disabled, focus moves to the current page instead of getting stuck.
- Each page change is announced through a polite live region.
- Reduced motion: the pill moves without gliding, the number does not scale and colours switch without a transition.
モーション
The ink pill glides to the new page in 340ms with ease-out and the new number settles from 82% scale as it arrives. Hover colours and focus rings: 160ms standard; a pressed cell or arrow scales to 0.92 for 100ms. The width never changes.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
Pagination.jsxpagination.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。