> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Pagination

Let users move through pages of results with a row of page numbers between previous and next arrows.

## Installation

```bash
npx @blotch/dolan add pagination
```

```typescript
import { UiPagination } from '../components/ui/pagination';
```

This also adds [Icon](/dolan/icon.md), whose theme colors draw the arrows.

## Usage

```typescript
import { Component, signal } from '@angular/core';
import { UiPagination } from '../components/ui/pagination';

@Component({
  selector: 'app-results-footer',
  imports: [UiPagination],
  template: `<ui-pagination [(page)]="page" [totalPages]="12" />`,
})
export class ResultsFooter {
  protected readonly page = signal(1);
}
```

- `[(page)]` two-way binds the current page. Pages count from `1`.
- Tapping a number goes to that page. The arrows step one page back or forward, and dim at the first and last page.
- The first and last pages always show. Around the current page, `siblingCount` pages show on each side, and `...` fills the gaps.
- With `5 + 2 × siblingCount` pages or fewer, every page shows and there are no gaps.
- The arrows draw in the theme's foreground, like the numbers. For dark mode, see [Icon](/dolan/icon.md#dark-mode).

### Show the current page's items

Derive the visible slice from the page signal with `computed()`. It updates whenever the user changes page.

```typescript
protected readonly pageSize = 20;
protected readonly items = signal<readonly Item[]>([]);
protected readonly page = signal(1);

protected readonly totalPages = computed(() =>
  Math.max(1, Math.ceil(this.items().length / this.pageSize)),
);
protected readonly pageItems = computed(() => {
  const start = (this.page() - 1) * this.pageSize;
  return this.items().slice(start, start + this.pageSize);
});
```

```angular-html
<ui-pagination [(page)]="page" [totalPages]="totalPages()" />
```

### More neighbors

`siblingCount` sets how many pages show on each side of the current one. Fewer fit a narrow screen better.

```angular-html
<ui-pagination [(page)]="page" [totalPages]="50" [siblingCount]="2" />
```

### Screen readers

Every arrow and number is a button with a label: "Previous page", "Next page", "Page 3", and "Page 4, current page" for the current one. The `...` gaps are skipped. Change the labels, for example to translate them, with `previousLabel`, `nextLabel` and `pageLabel`:

```angular-html
<ui-pagination
  [(page)]="page"
  [totalPages]="12"
  previousLabel="Earlier results"
  nextLabel="Later results"
  [pageLabel]="pageLabel"
/>
```

```typescript
protected readonly pageLabel = (page: number, current: boolean) =>
  current ? `Results page ${page}, showing now` : `Results page ${page}`;
```

### React to a change

Listen to `pageChange` to run code when the user changes page. It fires inside the tap, so it can change signals or navigate, as any tap handler can.

```angular-html
<ui-pagination [(page)]="page" [totalPages]="12" (pageChange)="scrollToTop()" />
```

## API

### UiPagination

| Input           | Type                                         | Default                          | Description                                       |
| --------------- | -------------------------------------------- | -------------------------------- | ------------------------------------------------- |
| `page`          | `number`                                     | `1`                              | The current page, from `1`. Supports `[(page)]`.  |
| `totalPages`    | `number`                                     | Required                         | The number of pages.                              |
| `siblingCount`  | `number`                                     | `1`                              | Pages shown on each side of the current one.      |
| `previousLabel` | `string`                                     | `'Previous page'`                | What a screen reader says for the previous arrow. |
| `nextLabel`     | `string`                                     | `'Next page'`                    | What a screen reader says for the next arrow.     |
| `pageLabel`     | `(page: number, current: boolean) => string` | `Page 3`, `Page 4, current page` | What a screen reader says for a page number.      |
| `class`         | `string`                                     | `''`                             | Extra Tailwind classes for the row.               |

| Output       | Type     | Description                                                                     |
| ------------ | -------- | ------------------------------------------------------------------------------- |
| `pageChange` | `number` | Emits the new page when the user taps a number or an arrow. Part of `[(page)]`. |

The component doesn't clamp `page`. If you set it yourself, keep it between `1` and `totalPages`.

## Related

- [List](/dolan/list.md): infinite scroll instead of pages
- [Empty State](/dolan/empty-state.md): what to show when there are no results
