Skip to content
Esc
↑↓navigate↵open⌘Jpreview
On this page

Pagination for Astro

Native Astro page links with current-page and previous/next styles.

Installation

bunx --bun shadcn@latest add @gnostic/pagination
npx shadcn@latest add @gnostic/pagination
pnpm dlx shadcn@latest add @gnostic/pagination

Component classes: Default Gnostic.

Complete the Installation guide first. Then copy the component files below.

No extra packages are required.

Copy the files

src/components/ui/pagination/pagination-content.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"ul">
const { class: className, ...props } = Astro.props
---

<ul
  data-slot="pagination-content"
  class={cn("flex flex-row items-center gap-1", className)}
  {...props}
>
  <slot />
</ul>
src/components/ui/pagination/pagination-link.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import { buttonVariants, type ButtonVariantProps } from "../button"
type Props = HTMLAttributes<"a"> & {
  isActive?: boolean
  size?: ButtonVariantProps["size"]
}
const {
  class: className,
  isActive = false,
  size = "icon",
  ...props
} = Astro.props
---

<a
  data-slot="pagination-link"
  data-size={size}
  data-variant={isActive ? "outline" : "ghost"}
  data-active={isActive ? "" : undefined}
  aria-current={isActive ? "page" : undefined}
  class={cn(
    buttonVariants({ size, variant: isActive ? "outline" : "ghost" }),
    className,
  )}
  {...props}
>
  <slot />
</a>
src/components/ui/pagination/pagination-item.astro
---
import type { HTMLAttributes } from "astro/types"
type Props = HTMLAttributes<"li">
---

<li data-slot="pagination-item" {...Astro.props}>
  <slot />
</li>
src/components/ui/pagination/index.ts
export { default as Pagination } from './pagination.astro'
export { default as PaginationContent } from './pagination-content.astro'
export { default as PaginationItem } from './pagination-item.astro'
export { default as PaginationLink } from './pagination-link.astro'
export { default as PaginationPrevious } from './pagination-previous.astro'
export { default as PaginationNext } from './pagination-next.astro'
export { default as PaginationEllipsis } from './pagination-ellipsis.astro'
src/components/ui/pagination/pagination.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
type Props = HTMLAttributes<"nav">
const { class: className, ...props } = Astro.props
---

<nav
  data-slot="pagination"
  class={cn("mx-auto flex w-full justify-center", className)}
  aria-label="Pagination"
  {...props}
>
  <slot />
</nav>
src/components/ui/pagination/pagination-previous.astro
---
import type { ComponentProps } from "astro/types"
import { cn } from "cn"
import Icon from "@lucide/astro/icons/chevron-left"
import PaginationLink from "./pagination-link.astro"
type Props = ComponentProps<typeof PaginationLink>
const { class: className, ...props } = Astro.props
---

<PaginationLink
  aria-label="Previous page"
  size="default"
  class={cn("max-sm:aspect-square max-sm:p-0", className)}
  {...props}
>
  <Icon aria-hidden="true" />
  <span class="max-sm:hidden">
    <slot>Previous</slot>
  </span>
</PaginationLink>
src/components/ui/pagination/pagination-ellipsis.astro
---
import type { HTMLAttributes } from "astro/types"
import { cn } from "cn"
import MoreHorizontal from "@lucide/astro/icons/ellipsis"
type Props = HTMLAttributes<"span">
const { class: className, ...props } = Astro.props
---

<span
  data-slot="pagination-ellipsis"
  aria-hidden="true"
  class={cn("flex min-w-7 justify-center", className)}
  {...props}
>
  <MoreHorizontal class="size-5 sm:size-4" />
</span>
src/components/ui/pagination/pagination-next.astro
---
import type { ComponentProps } from "astro/types"
import { cn } from "cn"
import Icon from "@lucide/astro/icons/chevron-right"
import PaginationLink from "./pagination-link.astro"
type Props = ComponentProps<typeof PaginationLink>
const { class: className, ...props } = Astro.props
---

<PaginationLink
  aria-label="Next page"
  size="default"
  class={cn("max-sm:aspect-square max-sm:p-0", className)}
  {...props}
>
  <span class="max-sm:hidden">
    <slot>Next</slot>
  </span>
  <Icon aria-hidden="true" />
</PaginationLink>

Usage

import { Pagination } from "@/components/ui/pagination"

Use the Code tab above for the complete Astro composition.

API reference

Read the registry source for the exported parts and prop types.

When to use Pagination

Build Pagination from a list of PaginationItem parts and real links. Calculate hrefs and the current page in your route or data layer. Set isActive on the current PaginationLink so it receives aria-current="page".

Keyboard and behavior

Navigation stays native and works without a client controller. Omit unavailable previous or next links, or render noninteractive text; an anchor has no native disabled attribute. Keep a useful label when more than one pagination region exists.

Theme and layout

Number links use Button sizing and surface tokens. Previous and next text can hide at small widths while accessible names remain. Ellipsis is a visual gap, not a control that loads more pages.