usePagination
Turns a page and a page count into the list of pages and ellipses to render, keeping its length steady as the page moves.
7 itemspnpm dlx shadcn@latest add https://hextaui.com/r/use-pagination.jsonAdds the hook and anything it depends on to your project.
Copy and paste the following code into your project.
hooks/use-pagination.ts Update the import paths to match your project setup.
The hook only does the math. It returns the list of pages and ellipses to render and leaves the markup to you, which is how Pagination builds its links. Use it to build your own pager, like dots for a carousel or a page picker in a table footer.
The list always shows the first and last boundaries pages, and siblings pages on each side of the current one. An ellipsis fills any gap of two pages or more. A gap of exactly one page shows that page instead, because an ellipsis there would hide no more than it takes up.
Once there are enough pages, the list always has 2 × boundaries + 2 × siblings + 3 items. Near the ends, the window widens instead of shrinking. Because the length never changes, the pager keeps its width, and the next and previous buttons stay under the pointer as you click through.
countandpageare clamped: a page past the end becomes the last page, and anything that isn't a finite number falls back to the default.- A
countof 0 returns no items and apageof 0, so an empty table can render nothing without a special case. siblingsandboundariesgo from 0 to 10.- Ellipses have a stable
positionofstartorend. Use it as the React key.
Dots
With boundaries: 0 the list is just a window around the current page. Ellipses become small dots, so a long set of slides never needs more than five targets.
- Mark the current page with
aria-current="page"and wrap the list in a<nav>with a label. - Hide ellipses from screen readers with
aria-hidden. They carry no information that the page numbers don't. - Show page numbers with
tabular-numsso the buttons don't change width as the digits change.
| Prop | Type | Default |
|---|---|---|
countTotal number of pages. | number | – |
pageThe current page, starting at 1. | number | 1 |
siblingsPages to show on each side of the current page. | number | 1 |
boundariesPages to always show at the start and the end. | number | 1 |
| Property | Description |
|---|---|
items | PaginationItemData[] to render, in order. |
page | The clamped current page. |
count | The clamped page count. |
hasPrevious | Whether there's a page before this one. |
hasNext | Whether there's a page after this one. |
Pagination.