Pagination

Moves between pages of a long list.

Set pageCount, and page with onPageChange to control it. The list’s function child gets the page numbers to show, with gaps where pages are left out.

Preview
basic.tsx
import {
  Pagination,
  PaginationEllipsis,
  PaginationItem,
  PaginationLink,
  PaginationList,
  PaginationNext,
  PaginationPrevious,
} from "~/components/ui/pagination"

export default function Example() {
  return (
    <Pagination aria-label="Pages" pageCount={24} defaultPage={6}>
      <PaginationList>
        {({ items }) => (
          <>
            <PaginationItem>
              <PaginationPrevious />
            </PaginationItem>
            {items.map((item) => (
              <PaginationItem key={item}>
                {typeof item === "number" ? <PaginationLink page={item} /> : <PaginationEllipsis />}
              </PaginationItem>
            ))}
            <PaginationItem>
              <PaginationNext />
            </PaginationItem>
          </>
        )}
      </PaginationList>
    </Pagination>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesButtonprimitive
Used byNothing yet.
Packages @ui-sdk/core@0.1.0-beta.20@ui-sdk/react@0.1.0-beta.20lucide-react@1.73.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/pagination.json

Manually

Install its packages:

terminal
npm install @ui-sdk/core@0.1.0-beta.20 @ui-sdk/react@0.1.0-beta.20 lucide-react@1.73.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/pagination.tsx
import {
  Pagination as PaginationPrimitive,
  PaginationEllipsis as PaginationEllipsisPrimitive,
  PaginationItem,
  PaginationLink as PaginationLinkPrimitive,
  PaginationList as PaginationListPrimitive,
  PaginationNext as PaginationNextPrimitive,
  PaginationPrevious as PaginationPreviousPrimitive,
} from "@ui-sdk/react/pagination"
import { ChevronLeft, ChevronRight } from "lucide-react"
import type * as React from "react"

import { buttonVariants } from "~/components/ui/button"
import { cnState } from "~/lib/utils"

/** Page controls in a labelled navigation landmark. Set `pageCount`, and `page` with `onPageChange` to control it. */
function Pagination({ className, ...props }: React.ComponentProps<typeof PaginationPrimitive>) {
  return (
    <PaginationPrimitive
      className={cnState("mx-auto flex w-full justify-center", className)}
      {...props}
    />
  )
}

/** The row of controls. Pass a function child to render its `items`: page numbers and gaps. */
function PaginationList({
  className,
  ...props
}: React.ComponentProps<typeof PaginationListPrimitive>) {
  return (
    <PaginationListPrimitive
      className={cnState("flex flex-row items-center gap-3xs", className)}
      {...props}
    />
  )
}

/** A page number. The current one is outlined. */
function PaginationLink({
  className,
  ...props
}: React.ComponentProps<typeof PaginationLinkPrimitive>) {
  return (
    <PaginationLinkPrimitive
      className={cnState(
        `${buttonVariants({ variant: "ghost", size: "icon" })} tabular-nums data-current:border-border data-current:bg-background dark:data-current:bg-transparent`,
        className,
      )}
      {...props}
    />
  )
}

/** Goes back a page, with its chevron built in. */
function PaginationPrevious({
  className,
  children = "Previous",
  ...props
}: React.ComponentProps<typeof PaginationPreviousPrimitive>) {
  return (
    <PaginationPreviousPrimitive
      className={cnState(`${buttonVariants({ variant: "ghost" })} pl-sm`, className)}
      {...props}
    >
      {(state) => (
        <>
          <ChevronLeft />
          <span className="sr-only sm:not-sr-only">
            {typeof children === "function" ? children(state) : children}
          </span>
        </>
      )}
    </PaginationPreviousPrimitive>
  )
}

/** Goes forward a page, with its chevron built in. */
function PaginationNext({
  className,
  children = "Next",
  ...props
}: React.ComponentProps<typeof PaginationNextPrimitive>) {
  return (
    <PaginationNextPrimitive
      className={cnState(`${buttonVariants({ variant: "ghost" })} pr-sm`, className)}
      {...props}
    >
      {(state) => (
        <>
          <span className="sr-only sm:not-sr-only">
            {typeof children === "function" ? children(state) : children}
          </span>
          <ChevronRight />
        </>
      )}
    </PaginationNextPrimitive>
  )
}

/** Marks pages left out of the list. */
function PaginationEllipsis({
  className,
  ...props
}: React.ComponentProps<typeof PaginationEllipsisPrimitive>) {
  return (
    <PaginationEllipsisPrimitive
      className={cnState(
        "flex size-8 items-center justify-center text-muted-foreground",
        className,
      )}
      {...props}
    />
  )
}

export {
  Pagination,
  PaginationEllipsis,
  PaginationItem,
  PaginationLink,
  PaginationList,
  PaginationNext,
  PaginationPrevious,
}

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Pagination

Page controls in a labelled navigation landmark. Set pageCount, and page with onPageChange to control it.

boundaries

How many pages always show at each end.

Type number · Default 1

className

Type string | ((state: NoInfer<PaginationState>) => string)

controller

Pages it from anywhere. Create one with createPaginationController().

Type { getPage: () => number; getPageCount: () => number; setPage: (page: number, options?: PaginationChangeOptions) => boolean; next: (options?: PaginationChangeOptions) => boolean; previous: (options?: PaginationChangeOptions | undefined) => boolean; first: (options?: PaginationChangeOptions | u...

debug

Logs every state change with its reason to the console, in development builds.

Type boolean

defaultPage

The page it starts on, when uncontrolled.

Type number · Default 1

disabled

Whether every page and step does nothing, like a step at its end. A controller still pages it.

Type boolean · Default false

getHref

The URL of a page. With it, parts are links: they navigate, open in a new tab with a modifier key, and work before JavaScript loads. Without it, they're buttons.

Type (page: number) => string

onPageChange

Runs when the page wants to change: a page link ("item-press"), the Previous, Next, First or Last part, or a controller ("imperative"). details.cancel() keeps the page, and stops a link from navigating.

Type (page: number, details: ChangeDetails<PaginationPageChangeReason>) => void

page

The current page, from 1. Pass it to control the pagination.

Type number

pageCount

How many pages there are. Or pass total and pageSize.

Type number

pageSize

How many items fit on a page, with total.

Type number · Default 10

render

Type RenderProp<NoInfer<PaginationState>>

siblings

How many pages show on each side of the current one.

Type number · Default 1

style

Type CSSProperties | ((state: NoInfer<PaginationState>) => CSSProperties)

total

How many items there are, for pageCount from pageSize.

Type number

PaginationEllipsis

Marks pages left out of the list.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: {}) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: {}) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<{}>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: {}) => CSSProperties)

A page number. The current one is outlined.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: PaginationLinkState) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: PaginationLinkState) => string)

page*

The page the link goes to, from 1.

Type number

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<PaginationLinkState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: PaginationLinkState) => CSSProperties)

PaginationList

The row of controls. Pass a function child to render its items: page numbers and gaps.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: NoInfer<PaginationState>) => ReactNode)

className

Class names, or a function of the part's state that returns them.

Type string | ((state: NoInfer<PaginationState>) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<NoInfer<PaginationState>>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: NoInfer<PaginationState>) => CSSProperties)

PaginationNext

Goes forward a page, with its chevron built in.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: PaginationStepState) => ReactNode) · Default Next

className

Class names, or a function of the part's state that returns them.

Type string | ((state: PaginationStepState) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<PaginationStepState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: PaginationStepState) => CSSProperties)

PaginationPrevious

Goes back a page, with its chevron built in.

children

Content, or a function of the part's state that returns it.

Type ReactNode | ((state: PaginationStepState) => ReactNode) · Default Previous

className

Class names, or a function of the part's state that returns them.

Type string | ((state: PaginationStepState) => string)

render

Renders your own element: one that receives the part's props, or a function of the props and the state.

Type RenderProp<PaginationStepState>

style

Styles, or a function of the part's state that returns them.

Type CSSProperties | ((state: PaginationStepState) => CSSProperties)