Web · Components
Pagination
Pagination: numbered pages, previous/next, first/last, page size, "go to page", a record summary and "show more" in one component.
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
In a table: sorting + page size + summary (Table pagination)
| Orders | Status | ||
|---|---|---|---|
| #4200 | Ayşe Yılmaz | Paid | $120.00 |
| #4201 | Mehmet Kaya | Returned | $3,239.00 |
| #4202 | Zeynep Demir | Paid | $1,558.00 |
| #4203 | Can Şahin | Paid | $4,677.00 |
| #4204 | Elif Çelik | Pending | $2,996.00 |
| #4205 | Emre Ak | Paid | $1,315.00 |
| #4206 | Ayşe Yılmaz | Paid | $4,434.00 |
| #4207 | Mehmet Kaya | Paid | $2,753.00 |
| #4208 | Zeynep Demir | Pending | $1,072.00 |
| #4209 | Can Şahin | Paid | $4,191.00 |
1–10 of 137
Your own grid: usePagination, scrolls to the top of the list on change
Product 1
Product 2
Product 3
Product 4
Product 5
Product 6
Product 7
Product 8
1–8 of 58
Cursor API, total unknown: useCursorPagination
Show more (variant="more"), async
Narrow card: compact, pick by searching across 10,000 pages
Archive
Linked: Next.js server component, page from the URL (?page=)
A table paged on the server; pages are real links, the filter parameter is kept,
on the first page ?page=1 isn't written.
Simple, with text: previous / next
Import
Pagination: numbered pages, previous/next, first/last, page size, "go to page", a record summary and "show more" in one component.
It works in three ways:
- Buttons (uncontrolled or
page+onPageChange): data changes in the browser or via a request. IfonPageChangereturns a Promise, the clicked button shows loading. For pagination in the browser seeusePagination. - Linked (
searchParamsorhref): each page is a real link; in a Next.js server component the data is paginated on the server, search engines see all pages and filter parameters are kept. - Cursor / unknown total (
hasNext): only previous/next; for a cursor stack seeuseCursorPagination.
Details:
- The button count stays constant while the page changes; buttons don't slide from under the cursor. Clicking a (…) gap jumps a few pages.
- On phones (
responsive) it becomes "‹ [3 ▾] / 10 ›"; any page is picked by searching in the middle box. Touch targets are at least 44 px. - When the page size changes, the current first record stays visible.
- Page changes are announced to screen readers; the current page carries
aria-current="page". - Built-in integrations:
Tablepagination,GallerypageSize.
It is a client component; in server components it can be used in linked form (searchParams, a string href) without passing functions.
Examples
usePagination
functionKeeps the page and page size state; if an array is given it slices the current page. For paginating content you render yourself, like a card grid or list, with Pagination.
When the record count drops (e.g. a filter) the page is pulled to the last valid page.
Examples
useCursorPagination
functionCursor pagination: the API returns a "next cursor" and the total is unknown. Cursors of visited pages are stored, so "Previous" and "First page" work without rebuilding a request chain.
Examples
readPageParam
functionReads a positive integer from the parameters (e.g. ?page=3 → 3). Returns undefined if invalid or missing.
Examples
paginationRange
functionProduces the order of page buttons: the first and last pages, the current page and its neighbours, and the gaps (…) between them.
The array length doesn't change as the page changes (with enough pages it is always 2 × siblings + 2 × boundaries + 3), so buttons don't shift under the pointer. If a gap would hide a single page, that page is shown instead.
Examples
pageHref
functionGenerates the URL of a page. Existing parameters (filter, sort) are kept; on the first page the page parameter is removed so the same page doesn't have two URLs (SEO).
Examples
getPageInfo
functionComputes the record range from a page number. Used when writing queries on the server and in summaries like "1–20 / 245".
Examples
paginate
functionReturns one page of an array (pagination in the browser or on the server).
Examples
Look of the pagination.
"pages": Numbered pages with (…) gaps in between and previous/next on both sides. If the total is unknown, the seen pages and (…) are shown."simple": Only "‹ Previous" and "Next ›" with "Page 3 / 10" in the middle. For cursor pagination and lists with an unknown total."compact": "‹ [3 ▾] / 10 ›"; any page is picked from the box in the middle (searchable even with thousands of pages). For narrow areas and phones."more": A "Show more" button, how many are shown and a progress bar. The next page is appended below the old ones (e-commerce lists).
PaginationClassNames
interfaceClasses that can be passed to the inner parts of Pagination.
| Name | Type | Description |
|---|---|---|
nav? | string | The <nav> carrying the page buttons. |
item? | string | Each page, previous/next and gap button. |
active? | string | Button of the current page. |
summary? | string | The "1–20 / 245" summary. |
pageSize? | string | Page size picker. |
jumper? | string | "Go to page" field. |
PaginationProps
interfaceProps of the Pagination component.
Extends: Omit<ComponentProps<"div">, "children" | "onChange">
| Name | Type | Default | Description |
|---|---|---|---|
page? | number | Current page when controlled (starts at 1). In linked use it is read from searchParams if given; you don't need to pass it. | |
defaultPage? | number | 1 | Initial page when uncontrolled. |
onPageChange? | (page: number) => unknown | Called when the page changes (in controlled and uncontrolled use). If it returns a Promise (e.g. an async function fetching data), a loading spinner turns on the clicked button until it settles and new clicks wait. In linked use it is only informational (e.g. analytics). | |
total? | number | Total record count. The page count is computed with pageSize; the summary ("1–20 / 245") and the "more" look use it. | |
pageCount? | number | Total page count (instead of total). | |
hasNext? | boolean | false | If the total is unknown (no total or pageCount; e.g. a cursor API), is there a next page? See useCursorPagination. |
pageSize? | number | Records per page when controlled. | |
defaultPageSize? | number | pageSizeOptions[0]` or `10 | Records per page when uncontrolled. |
onPageSizeChange? | (pageSize: number) => unknown | Called when the page size changes. The page also changes so the first record of the previous page stays visible (e.g. page 3 × 10 → page 2 × 20) and onPageChange is called as well. | |
pageSizeOptions? | readonly number[] | If given, a "Per page [20 ▾]" picker appears (e.g. [10, 20, 50, 100]). | |
variant? | PaginationVariant | "pages"` (`"simple"` if the total is unknown) | Look. See PaginationVariant. |
siblings? | number | 1 | Number of pages shown on each side of the current page. |
boundaries? | number | 1 | Number of pages always shown at the start and end. |
firstLast? | boolean | false | « » buttons going to the first and last page. |
jumper? | boolean | false | "Go to page" field: type a number and press Enter to go to that page. Not shown if the total is unknown. |
summary? | boolean | ((info: PageInfo) => ReactNode) | false | Record range summary ("1–20 of 245"). Requires total. With a function you generate the text. See PageInfo. |
responsive? | boolean | true | The "pages" look becomes "compact" automatically on small screens (below 640 px); texts get shorter. Done with CSS only; renders correctly on the server too. |
size? | Size | "md" | Button size. |
activeVariant? | Extract<ButtonVariant, "solid" | "soft" | "outline"> | "soft" | Button style of the current page. |
align? | "start" | "center" | "end" | "between" | "between"` if there is a summary or page size, otherwise `"center" | Layout of the parts. "between": the summary and page size on the left, the pages on the right. |
hideOnSinglePage? | boolean | false | Renders nothing when there is a single page. |
disabled? | boolean | All buttons are disabled (e.g. while data loads). | |
scrollTo? | RefObject<HTMLElement | null> | string | When the page changes (in button use), scrolls to this element if its top has gone above the screen: a ref or a CSS selector ("#list"). | |
href? | string | ((page: number, pageSize: number) => string) | Linked use: pages become links rather than buttons (with the UIProvider link component, e.g. Next.js Link); search engines can crawl all pages and pages can open in a new tab. - A string pattern: {page} and {pageSize} are replaced with numbers ("/blog/page/{page}"). Can be given from a server component too. - A function: (page, pageSize) => url (in client components only). For working with address bar parameters, searchParams is easier. | |
searchParams? | SearchParamsRecord | Linked use: the page's URL parameters (the Next.js page's await searchParams). When given, the current page is read from searchParam, other parameters (filter, sort) are kept in the links and ?page=1 isn't written on the first page. | |
searchParam? | string | "page" | Name of the page parameter. |
pageSizeParam? | string | Name of the page size parameter. If given, the page size is also read from and written to the URL (e.g. "pageSize" → ?pageSize=50). | |
pathname? | string | Path used in links (e.g. "/products"). If omitted, links consist only of the query ("?page=2") and resolve against the current path. | |
label? | string | "Sayfalar" | Screen reader name of the page buttons region. |
classNames? | PaginationClassNames | Classes added to the inner parts. See PaginationClassNames. |
UsePaginationOptions
interfaceusePagination options.
| Name | Type | Default | Description |
|---|---|---|---|
items? | readonly T[] | All records to paginate in the browser. If given, the returned items is the current page and total is computed from it. | |
total? | number | Total records (for server pagination; when items isn't given). | |
defaultPage? | number | 1 | Initial page. |
pageSize? | number | pageSizeOptions[0]` or `10 | Initial page size. |
pageSizeOptions? | readonly number[] | Page size options; passed through to Pagination. | |
onPageChange? | (page: number) => unknown | When the page changes (e.g. to fetch data). | |
onPageSizeChange? | (pageSize: number) => unknown | When the page size changes. |
UsePaginationResult
interfaceReturn value of usePagination.
Extends: PageInfo
| Name | Type | Description |
|---|---|---|
items | T[] | Records of the current page (if items was given). |
setPage | (page: number) => void | Changes the page. |
setPageSize | (pageSize: number) => void | Changes the page size; moves to the page where the first record stays visible. |
props | { page: number; total: number | undefined; pageSize: number; pageSizeOptions: readonly number[] | undefined; onPageChange: (page: number) => unknown; onPageSizeChange: (pageSize: number) => void; } | Props to spread onto Pagination: <Pagination {...props} />. |
UseCursorPaginationResult
interfaceReturn value of useCursorPagination.
| Name | Type | Description |
|---|---|---|
cursor | C | undefined | Cursor of the current page; initialCursor on the first page (usually undefined). |
page | number | The current page (starts at 1). |
reset | () => void | Returns to the first page (e.g. when a filter changes). |
props | (nextCursor: C | null | undefined) => { page: number; hasNext: boolean; onPageChange: (page: number) => void; } | Props to spread onto Pagination. Pass the "next cursor" from the last response; if null/undefined there is no next page. |
PaginationGap
typeGap (…) showing pages hidden between page buttons.
An element of the paginationRange array: a page number or a gap.
Address bar parameters; a Next.js page's searchParams is in this shape. Can be passed from the server to a client component (a plain object).
PaginationRangeOptions
interfacepaginationRange options.
| Name | Type | Default | Description |
|---|---|---|---|
page | number | Current page (starts at 1). | |
pageCount | number | Total page count. | |
siblings? | number | 1 | Number of pages shown on each side of the current page. |
boundaries? | number | 1 | Number of pages always shown at the start and end. |
PageHrefOptions
interfacepageHref options.
| Name | Type | Default | Description |
|---|---|---|---|
page | number | Page to go to. | |
pageSize? | number | undefined | Records per page; for {pageSize} in the href pattern and for pageSizeParam. | |
href? | string | undefined | URL pattern: {page} and {pageSize} are replaced with numbers (e.g. "/blog/page/{page}"). If given, the other options are ignored. | |
searchParams? | SearchParamsRecord | undefined | Existing parameters to keep (e.g. filters). | |
searchParam? | string | undefined | "page" | Name of the page parameter. |
pageSizeParam? | string | undefined | Name of the page size parameter. If given, pageSize is also written to the URL (not written if it equals defaultPageSize). | |
defaultPageSize? | number | undefined | Default page size that isn't written to the URL. | |
pathname? | string | undefined | Path of the URL (e.g. "/products"). If omitted, the URL consists only of the query ("?page=2") and the browser resolves it against the current path. |
PageInfoOptions
interfacegetPageInfo options.
| Name | Type | Description |
|---|---|---|
page | number | Requested page (starts at 1). Clamped to the nearest valid page when out of range. |
pageSize | number | Records per page. |
total? | number | undefined | Total records. Omitted when unknown (cursor pagination). |
PageInfo
interfacePosition of a page; returned by getPageInfo.
| Name | Type | Description |
|---|---|---|
page | number | Page clamped to the valid range (starts at 1). |
pageSize | number | Records per page. |
total | number | undefined | Total records; undefined when unknown. |
pageCount | number | undefined | Total pages; undefined when the total is unknown. 1 when there are no records. |
offset | number | Number of records to skip (SQL OFFSET, Array.slice start). |
start | number | Index of the first record on the page (starts at 1); 0 without records. |
end | number | Index of the last record on the page; offset + pageSize when the total is unknown. |
hasPrevious | boolean | Is there a previous page? |
hasNext | boolean | Is there a next page? true when the total is unknown. |