# Pagination

> A navigation bar that pages through long lists with jump and size controls.

```tsx title="hero.tsx"
import {
  Pagination,
  PaginationContent,
  PaginationNavigation,
  PaginationNext,
  PaginationPageJump,
  PaginationPageList,
  PaginationPageSize,
  PaginationPrevious,
} from '@langgenius/dify-ui/pagination'
import * as React from 'react'

const totalDocuments = 1284
const pageSizeOptions = [10, 25, 50] as const

type PageSize = (typeof pageSizeOptions)[number]

export function PaginationHeroDemo() {
  const [page, setPage] = React.useState(1)
  const [pageSize, setPageSize] = React.useState<PageSize>(25)

  return (
    <Pagination
      aria-label="Documents pagination"
      page={page}
      totalPages={Math.ceil(totalDocuments / pageSize)}
      onPageChange={setPage}
      className="px-0 py-0"
    >
      <PaginationContent>
        <PaginationNavigation>
          <PaginationPrevious />
          <PaginationPageJump />
          <PaginationNext />
        </PaginationNavigation>
        <PaginationPageList />
        <PaginationPageSize
          value={pageSize}
          options={pageSizeOptions}
          onValueChange={(value) => {
            setPageSize(value)
            setPage(1)
          }}
        />
      </PaginationContent>
    </Pagination>
  )
}
```

## Usage guidelines

- Use `Pagination` for a list that loads one page at a time, such as documents or run logs. Use `Tabs` to switch between views and `Breadcrumb` to show a location in a hierarchy.
- Control the page with `page` and `onPageChange`. Pages are one-based, out-of-range values clamp to the valid range, and `onPageChange` fires only when the page actually changes.
- Derive `totalPages` from the item count and page size. When the page size changes, reset `page` to the first page in the same handler.
- The bar renders nothing when `totalPages` is zero, negative or non-finite. Show `PaginationSkeleton` while the first page count is still loading.
- Pass localized text: `aria-label` names the navigation landmark (default `Pagination`), and `labels` supplies the default names and labels for all built-in controls. Give each landmark a distinct name when a page has more than one.
- `Pagination` provides shared page state and labels. Compose its parts to choose the layout; use `PaginationContent` for the three-column bar (which wraps cleanly in narrow containers) or omit it for a compact layout.

## Anatomy

```tsx
import {
  Pagination,
  PaginationContent,
  PaginationNavigation,
  PaginationNext,
  PaginationPageJump,
  PaginationPageList,
  PaginationPageSize,
  PaginationPrevious,
} from '@langgenius/dify-ui/pagination'
```

```tsx
<Pagination>
  <PaginationContent>
    <PaginationNavigation>
      <PaginationPrevious />
      <PaginationPageJump />
      <PaginationNext />
    </PaginationNavigation>
    <PaginationPageList />
    <PaginationPageSize />
  </PaginationContent>
</Pagination>
```

`PaginationPageList` renders a `PaginationPage` for each number in the computed range and a `PaginationEllipsis` for each gap. Render `PaginationPage` directly for a custom page list; it reads the current page from `Pagination`.

## Examples

### Page range

`siblingCount` sets how many pages show on each side of the current page, and `boundaryCount` how many stay pinned at each end. Both default to 1. This bar composes `Pagination` with only the previous button, the page list and the next button.

```tsx title="page-range.tsx"
import {
  Pagination,
  PaginationNext,
  PaginationPageList,
  PaginationPrevious,
} from '@langgenius/dify-ui/pagination'
import * as React from 'react'

export function PaginationPageRangeDemo() {
  const [page, setPage] = React.useState(48)

  return (
    <Pagination
      aria-label="Run history pagination"
      page={page}
      totalPages={120}
      siblingCount={2}
      boundaryCount={2}
      onPageChange={setPage}
      className="justify-center gap-1 px-0 py-0"
    >
      <PaginationPrevious />
      <PaginationPageList />
      <PaginationNext />
    </Pagination>
  )
}
```

### Localized labels

`labels` localizes the previous, next and page-jump controls, page buttons (`goToPage` and `currentPage`), and the visible and accessible page-size label (`perPage`). Omitted entries fall back to English. Individual controls can override their accessible names; `PaginationPageSize` also accepts its own `label` and can be used independently of `Pagination`.

```tsx title="localized-labels.tsx"
import {
  Pagination,
  PaginationContent,
  PaginationNavigation,
  PaginationNext,
  PaginationPageJump,
  PaginationPageList,
  PaginationPageSize,
  PaginationPrevious,
} from '@langgenius/dify-ui/pagination'
import * as React from 'react'

const totalApps = 386
const pageSizeOptions = [20, 50, 100] as const

type PageSize = (typeof pageSizeOptions)[number]

export function PaginationLocalizedLabelsDemo() {
  const [page, setPage] = React.useState(3)
  const [pageSize, setPageSize] = React.useState<PageSize>(20)
  const totalPages = Math.ceil(totalApps / pageSize)
  const start = (page - 1) * pageSize + 1
  const end = Math.min(page * pageSize, totalApps)

  return (
    <div lang="zh-Hans" className="flex w-full flex-col items-center gap-3">
      <p className="system-xs-regular text-text-tertiary tabular-nums">
        应用列表 · 第 {start}–{end} 项，共 {totalApps} 项
      </p>
      <Pagination
        aria-label="应用列表分页"
        page={page}
        totalPages={totalPages}
        onPageChange={setPage}
        className="px-0 py-0"
        labels={{
          previous: '上一页',
          next: '下一页',
          editPageNumber: (current, total) => `第 ${current}/${total} 页：编辑页码`,
          pageNumberInput: '页码',
          perPage: '每页条数',
          goToPage: (target) => `前往第 ${target} 页`,
          currentPage: (target) => `第 ${target} 页，当前页`,
        }}
      >
        <PaginationContent>
          <PaginationNavigation>
            <PaginationPrevious />
            <PaginationPageJump>
              第 {page} / {totalPages} 页
            </PaginationPageJump>
            <PaginationNext />
          </PaginationNavigation>
          <PaginationPageList />
          <PaginationPageSize
            value={pageSize}
            options={pageSizeOptions}
            onValueChange={(value) => {
              setPageSize(value)
              setPage(1)
            }}
          />
        </PaginationContent>
      </Pagination>
    </div>
  )
}
```

### Compact bar

Compose `Pagination` with only the navigation group for a narrow panel. The page-jump summary opens a number input when clicked.

```tsx title="compact.tsx"
import {
  Pagination,
  PaginationNavigation,
  PaginationNext,
  PaginationPageJump,
  PaginationPrevious,
} from '@langgenius/dify-ui/pagination'
import * as React from 'react'

export function PaginationCompactDemo() {
  const [page, setPage] = React.useState(1)

  return (
    <Pagination
      aria-label="Segments pagination"
      page={page}
      totalPages={18}
      onPageChange={setPage}
      className="justify-center px-0 py-0"
    >
      <PaginationNavigation>
        <PaginationPrevious />
        <PaginationPageJump />
        <PaginationNext />
      </PaginationNavigation>
    </Pagination>
  )
}
```

### Loading

`PaginationSkeleton` keeps the bar's height while the first page count loads and is hidden from assistive technology. Toggle between the skeleton and the loaded bar to compare their alignment.

```tsx title="loading.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Pagination,
  PaginationContent,
  PaginationNavigation,
  PaginationNext,
  PaginationPageJump,
  PaginationPageList,
  PaginationPageSize,
  PaginationPrevious,
  PaginationSkeleton,
} from '@langgenius/dify-ui/pagination'
import * as React from 'react'

const totalLogs = 640
const pageSizeOptions = [10, 25, 50] as const

type PageSize = (typeof pageSizeOptions)[number]

export function PaginationLoadingDemo() {
  const [loading, setLoading] = React.useState(true)
  const [page, setPage] = React.useState(1)
  const [pageSize, setPageSize] = React.useState<PageSize>(25)

  return (
    <div className="flex w-full flex-col items-center gap-4">
      <Button size="small" variant="secondary" onClick={() => setLoading((current) => !current)}>
        {loading ? 'Show loaded bar' : 'Show skeleton'}
      </Button>
      {loading ? (
        <PaginationSkeleton className="px-0 py-0" />
      ) : (
        <Pagination
          aria-label="Workflow logs pagination"
          page={page}
          totalPages={Math.ceil(totalLogs / pageSize)}
          onPageChange={setPage}
          className="px-0 py-0"
        >
          <PaginationContent>
            <PaginationNavigation>
              <PaginationPrevious />
              <PaginationPageJump />
              <PaginationNext />
            </PaginationNavigation>
            <PaginationPageList />
            <PaginationPageSize
              value={pageSize}
              options={pageSizeOptions}
              onValueChange={(value) => {
                setPageSize(value)
                setPage(1)
              }}
            />
          </PaginationContent>
        </Pagination>
      )}
    </div>
  )
}
```

## Keyboard

| Key                       | Action                                                                                                 |
| ------------------------- | ------------------------------------------------------------------------------------------------------ |
| `Enter`, `Space`          | Activates the focused previous, next or page button. On the page summary, opens the page number input. |
| `ArrowUp`, `ArrowDown`    | In the page number input, adjusts the draft page without leaving edit mode.                            |
| `Home`, `End`             | In the page number input, sets the draft page to the first or last page without leaving edit mode.     |
| `Enter`                   | In the page number input, goes to the edited page and returns focus to the summary.                    |
| `Tab`                     | In the page number input, goes to the edited page and moves focus to the next control.                 |
| `Escape`                  | In the page number input, discards the edited page and returns focus to the summary.                   |
| `ArrowLeft`, `ArrowRight` | On the page-size selector, selects the previous or next size, wrapping at the ends.                    |

## API reference

### Pagination

Provides pagination state, page-range computation, and default control labels for composed pagination layouts. Renders a `<nav>` element, or `null` when `totalPages <= 0`.

**Props**

| Prop             | Type                        | Default | Description                                                                                                                                   |
| ---------------- | --------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `page`           | `number`                    | —       | The current page, starting at 1. Values outside `1` to `totalPages` are clamped.                                                              |
| `totalPages`     | `number`                    | —       | The total number of pages. When it is 0 or less, the pagination bar renders nothing.                                                          |
| `onPageChange`   | `(page: number) => void`    | —       | Callback fired when the user navigates to a different page. Receives the clamped target page and is not called when the page does not change. |
| `siblingCount?`  | `number`                    | `1`     | The number of pages shown on each side of the current page before an ellipsis.                                                                |
| `boundaryCount?` | `number`                    | `1`     | The number of pages always shown at the start and end of the page list.                                                                       |
| `labels?`        | `Partial<PaginationLabels>` | —       | Accessible names and labels for the built-in controls, usually translated strings. Missing entries fall back to English.                      |

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).

### PaginationContent

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).

### PaginationEllipsis

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).

### PaginationNavigation

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).

### PaginationNext

A button component that can be used to trigger actions. Renders a `<button>` element.

**Props**

| Prop        | Type              | Default | Description                                                  |
| ----------- | ----------------- | ------- | ------------------------------------------------------------ |
| `children?` | `React.ReactNode` | —       | The button content. Renders a right arrow icon when omitted. |

Inherits `className`, `render`, `style`, `nativeButton`, `focusableWhenDisabled` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### PaginationPage

A button component that can be used to trigger actions. Renders a `<button>` element.

**Props**

| Prop        | Type              | Default | Description                                                                                       |
| ----------- | ----------------- | ------- | ------------------------------------------------------------------------------------------------- |
| `page`      | `number`          | —       | The page this button navigates to. Marked with `aria-current="page"` when it is the current page. |
| `children?` | `React.ReactNode` | —       | The button content. Renders the page number when omitted.                                         |

Inherits `className`, `render`, `style`, `nativeButton`, `focusableWhenDisabled` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### PaginationPageJump

A button component that can be used to trigger actions. Renders a `<button>` element.

**Props**

| Prop          | Type              | Default | Description                                                                                                                                   |
| ------------- | ----------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
| `inputLabel?` | `string`          | —       | The accessible name of the page number input shown while editing. Defaults to `labels.pageNumberInput` from `Pagination`, or `'Page number'`. |
| `children?`   | `React.ReactNode` | —       | The button content. Renders the current page and the total pages when omitted.                                                                |

Inherits `className`, `render`, `style`, `nativeButton`, `focusableWhenDisabled` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### PaginationPageList

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).

### PaginationPageSize

**Props**

| Prop            | Type                     | Default | Description                                                                                                                              |
| --------------- | ------------------------ | ------- | ---------------------------------------------------------------------------------------------------------------------------------------- |
| `value`         | `Value`                  | —       | The selected page size.                                                                                                                  |
| `options`       | `readonly Value[]`       | —       | The page sizes to choose from.                                                                                                           |
| `onValueChange` | `(value: Value) => void` | —       | Callback fired when a different page size is selected.                                                                                   |
| `label?`        | `React.ReactNode`        | —       | The visible label shown while the selector is hovered or focused. Defaults to `labels.perPage` from `Pagination`, or `'Items per page'`. |
| `aria-label?`   | `string`                 | —       | The accessible name of the page size options. Defaults to `labels.perPage` from `Pagination`, or `'Items per page'`.                     |
| `className?`    | `string`                 | —       | CSS class applied to the container element.                                                                                              |

### PaginationPrevious

A button component that can be used to trigger actions. Renders a `<button>` element.

**Props**

| Prop        | Type              | Default | Description                                                 |
| ----------- | ----------------- | ------- | ----------------------------------------------------------- |
| `children?` | `React.ReactNode` | —       | The button content. Renders a left arrow icon when omitted. |

Inherits `className`, `render`, `style`, `nativeButton`, `focusableWhenDisabled` from [Base UI Button](https://base-ui.com/react/components/button).

**Data attributes**

| Attribute       | Description                          |
| --------------- | ------------------------------------ |
| `data-disabled` | Present when the button is disabled. |

### PaginationSkeleton

Inherits `render` from [Base UI null](https://base-ui.com/react/handbook/composition).
