Skip to content

Pagination

Moves between pages of a long list with numbered links and previous and next controls.

Installation

pnpm dlx shadcn@latest add @silkware/pagination

To copy the component by hand, install its dependencies and the Button component it builds on:

pnpm add react-aria-components cn lucide-react

Then copy the source into components/ui/pagination.tsx:

components/ui/pagination.tsx
import * as React from "react"import { cn } from "cn"
import { LinkButton } from "@/components/ui/button"import { ChevronLeftIcon, ChevronRightIcon, MoreHorizontalIcon } from "lucide-react"
function Pagination({ className, ...props }: React.ComponentProps<"nav">) {  return (    <nav      role="navigation"      aria-label="pagination"      data-slot="pagination"      className={cn("mx-auto flex w-full justify-center", className)}      {...props}    />  )}
function PaginationContent({  className,  ...props}: React.ComponentProps<"ul">) {  return (    <ul      data-slot="pagination-content"      className={cn("flex items-center gap-1", className)}      {...props}    />  )}
function PaginationItem({ ...props }: React.ComponentProps<"li">) {  return <li data-slot="pagination-item" {...props} />}
type PaginationLinkProps = {  isActive?: boolean} & Omit<React.ComponentProps<typeof LinkButton>, "variant">
function PaginationLink({  className,  isActive,  size = "icon",  ...props}: PaginationLinkProps) {  return (    <LinkButton      variant={isActive ? "outline" : "ghost"}      size={size}      className={cn(className)}      aria-current={isActive ? "page" : undefined}      data-slot="pagination-link"      data-active={isActive}      {...props}    />  )}
function PaginationPrevious({  className,  text = "Previous",  ...props}: React.ComponentProps<typeof PaginationLink> & { text?: string }) {  return (    <PaginationLink      aria-label="Go to previous page"      size="default"      className={cn("pl-2!", className)}      {...props}    >      <ChevronLeftIcon data-icon="inline-start" />      <span className="hidden sm:block">{text}</span>    </PaginationLink>  )}
function PaginationNext({  className,  text = "Next",  ...props}: React.ComponentProps<typeof PaginationLink> & { text?: string }) {  return (    <PaginationLink      aria-label="Go to next page"      size="default"      className={cn("pr-2!", className)}      {...props}    >      <span className="hidden sm:block">{text}</span>      <ChevronRightIcon data-icon="inline-end" />    </PaginationLink>  )}
function PaginationEllipsis({  className,  ...props}: React.ComponentProps<"span">) {  return (    <span      aria-hidden      data-slot="pagination-ellipsis"      className={cn(        "flex size-9 items-center justify-center [&_svg:not([class*='size-'])]:size-4",        className      )}      {...props}    >      <MoreHorizontalIcon      />      <span className="sr-only">More pages</span>    </span>  )}
export {  Pagination,  PaginationContent,  PaginationEllipsis,  PaginationItem,  PaginationLink,  PaginationNext,  PaginationPrevious,}

Usage

import {  Pagination,  PaginationContent,  PaginationEllipsis,  PaginationItem,  PaginationLink,  PaginationNext,  PaginationPrevious,} from "@/components/ui/pagination"
<Pagination aria-label="Orders pages">  <PaginationContent>    <PaginationItem>      <PaginationPrevious href="/orders?page=1" />    </PaginationItem>    <PaginationItem>      <PaginationLink href="/orders?page=2" isActive>        2      </PaginationLink>    </PaginationItem>    <PaginationItem>      <PaginationNext href="/orders?page=3" />    </PaginationItem>  </PaginationContent></Pagination>

Every control is a link with an href, so each page has an address that can be shared and reloaded. To change the page without leaving the view, keep the href and also handle onPress.

Examples

Numbered pages

Previous, next, the pages around the current one, and an ellipsis for the gap. Mark the current page with isActive.

Previous and next

Use only the two controls when the user moves through the list in order and the page number means nothing, or when the total is unknown.

Icons only

PaginationLink is square by default, so an icon fits without text. The icon has no visible text, so add an aria-label.

Controlled

Keep the page in state, show the range and disable the ends with isDisabled.

Showing 1 to 10 of 42 orders

Accessibility

KeyAction
TabMoves focus to the next control. A disabled control is skipped.
ShiftTabMoves focus to the previous control.
EnterFollows the focused link.

React Aria provides:

  • Link behaviour on every control: keyboard activation, press events and client navigation through the router.
  • aria-disabled and removal from the tab order with isDisabled.
  • A focus ring for keyboard focus only.

The component adds:

  • A nav landmark with the label "pagination", and a list of items inside it.
  • aria-current="page" on the active link.
  • Labels "Go to previous page" and "Go to next page" on the two arrows, which hide their text below the sm breakpoint.

You provide:

  • A specific aria-label on Pagination, such as "Orders pages", when the page has more than one list with pages.
  • An aria-label on every icon-only link.
  • A visible count, such as "Showing 21 to 30 of 142 orders", so users know how much there is.

Guidelines

When to use

  • For a list the user can scan, jump around in and come back to: orders, suppliers, audit entries.
  • When a page should have an address, so a colleague can open the same page.

When not to use

  • For an endless feed, or when each batch continues the last. Use a "Load more" Button or load on scroll.
  • For a short list. Show everything.
  • To move between sections of content. Use Tabs.
  • To page through a table with sorting and filters on the client. Use the Data Table, which composes paging with the table.

Content

  • Show the first page, the last page, the current page and its neighbours. Replace the rest with PaginationEllipsis.
  • Show a count of results beside it, and say what is counted: "142 orders".
  • Keep the arrows labelled "Previous" and "Next". Do not use "Back".
  • Return to the first page when the user changes a filter.

Showing 21 to 30 of 142 orders

Do

Show the count and collapse long ranges with an ellipsis.

Don’t

Don’t list every page. Fifteen links wrap and bury the current one.

API reference

Pagination

The landmark. Renders a nav with aria-label="pagination", centred. It accepts every nav prop.

PropTypeDefaultDescription
aria-label?string"pagination"The name of the landmark.
className?stringNoneClasses merged after the base classes.

PaginationContent

Renders the ul that holds the items. It accepts every ul prop.

PropTypeDefaultDescription
className?stringNoneClasses merged after the base classes.

PaginationItem

Renders an li. It accepts every li prop.

A numbered page. Renders a LinkButton, which is a React Aria Link with the button styles, so it accepts every Link prop and every LinkButton prop except variant.

PropTypeDefaultDescription
href?stringNoneThe address of the page.
isActive?booleanfalseMarks the current page: outline style and aria-current set to page.
size?"xs" | "sm" | "default" | "lg" | "icon-xs" | "icon-sm" | "icon" | "icon-lg""icon"The button size. The default makes a square.
isDisabled?booleanfalseSkips the link in the tab order and ignores presses.
onPress?(event: PressEvent) => voidNoneCalled when the link is pressed.

PaginationPrevious

A link to the previous page, with a chevron and the text "Previous". The text is hidden below the sm breakpoint. Accepts every PaginationLink prop.

PropTypeDefaultDescription
text?string"Previous"The visible text.
href?stringNoneThe address of the previous page.

PaginationNext

A link to the next page, with the text "Next" and a chevron. Accepts every PaginationLink prop.

PropTypeDefaultDescription
text?string"Next"The visible text.
href?stringNoneThe address of the next page.

PaginationEllipsis

A three-dot marker for skipped pages, hidden from assistive technology.

PropTypeDefaultDescription
className?stringNoneClasses merged after the base classes.