# Pager

Previous, a position such as “2 of 5”, and next, for stepping through a short set.

Previous, a position ("2 of 3", optionally with rolling digits) and next, for stepping through a short set: insights, questions, results. The buttons are ghost square buttons that stop at the ends unless the pager loops; arrow keys, Home and End page while focus is inside; the position is announced through a status region. Mirrored right-to-left.

Kind: component · Category: Navigation · Status: beta · Targets: React, Vue, Svelte

## Add

```bash
npx fndn add pager
```

It writes the folder `pager/` into the project's ui folder (`index.ts`, `pager.tsx`, `Pager.vue`, `Pager.svelte`, `pager.recipe.ts`), and what it is built from: [Button](/components/button.md), [Tooltip](/components/tooltip.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Pager } from "@/components/ui/pager";
```

```vue
import { Pager } from "@/components/ui/pager";
```

```svelte
import { Pager } from "$lib/components/ui/pager";
```

## Parts

In the DOM the root carries `data-slot="pager"` and each part `data-slot="pager-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `pager` |  | role=group with an aria-label; data-slot="pager" |
| `prev` | `button` | `pager-prev` |  | The real Button (ghost, sm, square, muted), marked data-pager="prev"; aria-label; aria-disabled="true" at the first page unless it loops (stays focusable) With our tooltip, its aria-label (the adapters wrap it in Tooltip). |
| `position` | `span` | `pager-position` |  | role=status. Plain: the text "2 of 3" (plus a visually hidden unit). Rolling: visually hidden text, digits and total (aria-hidden). |
| `text` | `span` | `pager-text` | optional | Without reel, when spokenSeparator differs from separator: the visible position ("2 / 3"), aria-hidden; a visually hidden span beside it says "2 of 3". |
| `digits` | `span` | `pager-digits` | optional | Rolling digits, aria-hidden |
| `digit` | `span` | `pager-digit` | optional, repeats | One 1em window per digit of the page number |
| `reel` | `span` | `pager-reel` | optional, repeats | The column 0–9 inside a digit; the recipe's digit variant is its value; carries kbd-instant |
| `total` | `span` | `pager-total` | optional | " of 3" after the digits, aria-hidden |
| `next` | `button` | `pager-next` |  | The real Button (ghost, sm, square, muted), marked data-pager="next"; aria-disabled="true" at the last page unless it loops With our tooltip, its aria-label (the adapters wrap it in Tooltip). |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `page` | `number` | `0` | Bindable (React: `defaultPage` and `onPageChange`; Vue: `v-model:page`; Svelte: `bind:page`). Current page, 0-based. React: page / defaultPage / onPageChange; Vue: v-model:page; Svelte: bind:page. |
| `count` | `number` |  | Number of pages. With 0 or 1 both buttons are disabled. |
| `loop` | `boolean` | `false` | Wrap around at the ends; the buttons never disable. |
| `reel` | `boolean` | `false` | Roll each digit of the position like a counter (approval-card). |
| `layout` | `"between" \| "start"` | `"between"` | between: prev · position · next. start: position · prev · next (the DOM order stays prev · position · next). |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | horizontal: left and right carets, mirrored right-to-left. vertical: up and down carets for content that slides vertically; Up and Down page too. |
| `separator` | `string` | `"of"` | Between page and count: "of" (2 of 3) or "/" (2 / 3). |
| `spokenSeparator` | `string` | `"of"` | What screen readers hear between page and count, whatever the visible separator ("2 / 3" is read "2 of 3"). |
| `unit` | `string` |  | Read before the position by screen readers only: "Question" → "Question 2 of 3". |
| `label` | `string` | `"Pages"` | The group's accessible name (aria-label). |
| `prevLabel` | `string` | `"Previous"` | aria-label of the previous button. |
| `nextLabel` | `string` | `"Next"` | aria-label of the next button. |
| `prevProps` | `object` |  | Attributes for the previous button, so a host can tie it to what it pages (carousel: aria-controls="<its slide group>"). The pager's own aria-label and aria-disabled win. |
| `nextProps` | `object` |  | Attributes for the next button, as prevProps. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `pageChange` | (page: number) |

## States

`hover`, `press`, `focus-visible`, `disabled`

## Keyboard and accessibility

Role: group of two buttons + status.

Keyboard:

- Tab reaches the previous and the next button (a disabled end stays focusable through aria-disabled)
- Enter / Space on a button page once; nothing happens on a disabled end
- ArrowLeft / ArrowRight page back and forward while focus is inside (swapped right-to-left)
- ArrowUp / ArrowDown page back and forward too when the pager is vertical
- Home / End jump to the first and the last page
- Focus stays on the button that was used, also when it becomes disabled

Notes:

- Give the group a label that names what is paged ("Choose insight").
- The position is a status region: every change is announced ("Question 2 of 3" with a unit).

## Motion

- reel: a digit rolls to its value over duration.move easing.out-strong: the outgoing figure leaves and the incoming one arrives (transform and opacity); the current figure rests untransformed, so it lines up with the text beside it at every display scale; instant from the keyboard (kbd-instant)
- buttons: the button's own press scale and hover
- first paint: nothing plays

## Tokens

`color.ink`, `color.ink-2`, `text.2xs`, `duration.move`, `easing.out-strong`

## Dependencies

- [Button](/components/button.md)
- [Tooltip](/components/tooltip.md)

## Notes

- Composition: the host owns what is paged (the pages, the sliding track) and listens for the page change.

## Examples

### Step through review comments

React · `review-comments.tsx`

```tsx
import { useState } from "react";
import { Pager } from "@/components/ui/pager";

const comments = [
  { author: "Priya Raman", file: "deliver.ts:42", text: "Should the backoff cap at 5 minutes? A dead endpoint retries for hours now." },
  { author: "Lucas Moreau", file: "retry-policy.ts:8", text: "Rename MAX to MAX_ATTEMPTS so it reads at the call site." },
  { author: "Priya Raman", file: "deliver.test.ts:117", text: "Add a case for a 410 Gone: we should stop at once." },
  { author: "Aiko Tanaka", file: "README.md:56", text: "Document the new retry headers for customers." },
];

export default function ReviewComments() {
  const [page, setPage] = useState(0);
  const comment = comments[page];
  return (
    <div className="flex w-80 flex-col gap-2">
      <div className="flex items-center justify-between gap-3">
        <span className="text-sm font-medium text-ink">Unresolved comments</span>
        <Pager
          count={comments.length}
          page={page}
          onPageChange={setPage}
          layout="start"
          unit="Comment"
          label="Unresolved comments"
          prevLabel="Previous comment"
          nextLabel="Next comment"
        />
      </div>
      {comment && (
        <div className="flex flex-col gap-0.5 text-sm">
          <span className="text-xs text-ink-2">{comment.author} on <span className="font-mono">{comment.file}</span></span>
          <p className="m-0 text-ink-2">{comment.text}</p>
        </div>
      )}
    </div>
  );
}
```

Vue · `review-comments.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { Pager } from "@/components/ui/pager";

const comments = [
  { author: "Priya Raman", file: "deliver.ts:42", text: "Should the backoff cap at 5 minutes? A dead endpoint retries for hours now." },
  { author: "Lucas Moreau", file: "retry-policy.ts:8", text: "Rename MAX to MAX_ATTEMPTS so it reads at the call site." },
  { author: "Priya Raman", file: "deliver.test.ts:117", text: "Add a case for a 410 Gone: we should stop at once." },
  { author: "Aiko Tanaka", file: "README.md:56", text: "Document the new retry headers for customers." },
];

const page = ref(0);
const comment = computed(() => comments[page.value]);
</script>

<template>
  <div class="flex w-80 flex-col gap-2">
    <div class="flex items-center justify-between gap-3">
      <span class="text-sm font-medium text-ink">Unresolved comments</span>
      <Pager
        v-model:page="page"
        :count="comments.length"
        layout="start"
        unit="Comment"
        label="Unresolved comments"
        prevLabel="Previous comment"
        nextLabel="Next comment"
      />
    </div>
    <div v-if="comment" class="flex flex-col gap-0.5 text-sm">
      <span class="text-xs text-ink-2">{{ comment.author }} on <span class="font-mono">{{ comment.file }}</span></span>
      <p class="m-0 text-ink-2">{{ comment.text }}</p>
    </div>
  </div>
</template>
```

Svelte · `review-comments.svelte`

```svelte
<script lang="ts">
  import { Pager } from "$lib/components/ui/pager";

  const comments = [
    { author: "Priya Raman", file: "deliver.ts:42", text: "Should the backoff cap at 5 minutes? A dead endpoint retries for hours now." },
    { author: "Lucas Moreau", file: "retry-policy.ts:8", text: "Rename MAX to MAX_ATTEMPTS so it reads at the call site." },
    { author: "Priya Raman", file: "deliver.test.ts:117", text: "Add a case for a 410 Gone: we should stop at once." },
    { author: "Aiko Tanaka", file: "README.md:56", text: "Document the new retry headers for customers." },
  ];

  let page = $state(0);
  const comment = $derived(comments[page]);
</script>

<div class="flex w-80 flex-col gap-2">
  <div class="flex items-center justify-between gap-3">
    <span class="text-sm font-medium text-ink">Unresolved comments</span>
    <Pager
      bind:page
      count={comments.length}
      layout="start"
      unit="Comment"
      label="Unresolved comments"
      prevLabel="Previous comment"
      nextLabel="Next comment"
    />
  </div>
  {#if comment}
    <div class="flex flex-col gap-0.5 text-sm">
      <span class="text-xs text-ink-2">{comment.author} on <span class="font-mono">{comment.file}</span></span>
      <p class="m-0 text-ink-2">{comment.text}</p>
    </div>
  {/if}
</div>
```

### Insights that wrap around

React · `usage-insights.tsx`

```tsx
import { useState } from "react";
import { Pager } from "@/components/ui/pager";

const insights = [
  "Bandwidth is up 18% this week, mostly from docs-site.",
  "billing-api builds take 2m 10s; caching node_modules would save about 50s.",
  "3 preview deploys have been idle for 30 days.",
];

/* a short set read in a loop: it wraps at the ends and the digits roll */
export default function UsageInsights() {
  const [page, setPage] = useState(0);
  return (
    <div className="flex w-80 flex-col gap-2">
      <p className="m-0 min-h-10 text-sm text-ink-2">{insights[page]}</p>
      <Pager
        count={insights.length}
        page={page}
        onPageChange={setPage}
        loop
        reel
        layout="start"
        label="Usage insights"
        prevLabel="Previous insight"
        nextLabel="Next insight"
      />
    </div>
  );
}
```

Vue · `usage-insights.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Pager } from "@/components/ui/pager";

const insights = [
  "Bandwidth is up 18% this week, mostly from docs-site.",
  "billing-api builds take 2m 10s; caching node_modules would save about 50s.",
  "3 preview deploys have been idle for 30 days.",
];

/* a short set read in a loop: it wraps at the ends and the digits roll */
const page = ref(0);
</script>

<template>
  <div class="flex w-80 flex-col gap-2">
    <p class="m-0 min-h-10 text-sm text-ink-2">{{ insights[page] }}</p>
    <Pager
      v-model:page="page"
      :count="insights.length"
      loop
      reel
      layout="start"
      label="Usage insights"
      prevLabel="Previous insight"
      nextLabel="Next insight"
    />
  </div>
</template>
```

Svelte · `usage-insights.svelte`

```svelte
<script lang="ts">
  import { Pager } from "$lib/components/ui/pager";

  const insights = [
    "Bandwidth is up 18% this week, mostly from docs-site.",
    "billing-api builds take 2m 10s; caching node_modules would save about 50s.",
    "3 preview deploys have been idle for 30 days.",
  ];

  /* a short set read in a loop: it wraps at the ends and the digits roll */
  let page = $state(0);
</script>

<div class="flex w-80 flex-col gap-2">
  <p class="m-0 min-h-10 text-sm text-ink-2">{insights[page]}</p>
  <Pager
    bind:page
    count={insights.length}
    loop
    reel
    layout="start"
    label="Usage insights"
    prevLabel="Previous insight"
    nextLabel="Next insight"
  />
</div>
```

### Up and down through stacked alerts

React · `incident-alerts.tsx`

```tsx
import { useState } from "react";
import { Pager } from "@/components/ui/pager";

const alerts = [
  "Error rate above 2% on billing-api",
  "p95 latency 1.8 s on web-app in Singapore",
  "data-pipeline job failed 3 times since 09:00",
  "Certificate for docs.northwind.example expires in 6 days",
];

/* alerts stacked on top of each other page up and down */
export default function IncidentAlerts() {
  const [page, setPage] = useState(0);
  return (
    <div className="flex w-80 items-center gap-3">
      <p className="m-0 flex-1 text-sm text-ink">{alerts[page]}</p>
      <Pager
        count={alerts.length}
        page={page}
        onPageChange={setPage}
        orientation="vertical"
        separator="/"
        unit="Alert"
        label="Open alerts"
        prevLabel="Previous alert"
        nextLabel="Next alert"
      />
    </div>
  );
}
```

Vue · `incident-alerts.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Pager } from "@/components/ui/pager";

const alerts = [
  "Error rate above 2% on billing-api",
  "p95 latency 1.8 s on web-app in Singapore",
  "data-pipeline job failed 3 times since 09:00",
  "Certificate for docs.northwind.example expires in 6 days",
];

/* alerts stacked on top of each other page up and down */
const page = ref(0);
</script>

<template>
  <div class="flex w-80 items-center gap-3">
    <p class="m-0 flex-1 text-sm text-ink">{{ alerts[page] }}</p>
    <Pager
      v-model:page="page"
      :count="alerts.length"
      orientation="vertical"
      separator="/"
      unit="Alert"
      label="Open alerts"
      prevLabel="Previous alert"
      nextLabel="Next alert"
    />
  </div>
</template>
```

Svelte · `incident-alerts.svelte`

```svelte
<script lang="ts">
  import { Pager } from "$lib/components/ui/pager";

  const alerts = [
    "Error rate above 2% on billing-api",
    "p95 latency 1.8 s on web-app in Singapore",
    "data-pipeline job failed 3 times since 09:00",
    "Certificate for docs.northwind.example expires in 6 days",
  ];

  /* alerts stacked on top of each other page up and down */
  let page = $state(0);
</script>

<div class="flex w-80 items-center gap-3">
  <p class="m-0 flex-1 text-sm text-ink">{alerts[page]}</p>
  <Pager
    bind:page
    count={alerts.length}
    orientation="vertical"
    separator="/"
    unit="Alert"
    label="Open alerts"
    prevLabel="Previous alert"
    nextLabel="Next alert"
  />
</div>
```

### Only one match

React · `one-result.tsx`

```tsx
import { Pager } from "@/components/ui/pager";

/* with a single page both buttons are disabled, and the pager stays in its place */
export default function OneResult() {
  return (
    <div className="flex w-80 items-center justify-between gap-3">
      <span className="text-sm text-ink-2">1 deploy matches “usage-alerts”</span>
      <Pager count={1} label="Matching deploys" prevLabel="Previous match" nextLabel="Next match" />
    </div>
  );
}
```

Vue · `one-result.vue`

```vue
<script setup lang="ts">
import { Pager } from "@/components/ui/pager";
</script>

<!-- with a single page both buttons are disabled, and the pager stays in its place -->
<template>
  <div class="flex w-80 items-center justify-between gap-3">
    <span class="text-sm text-ink-2">1 deploy matches “usage-alerts”</span>
    <Pager :count="1" label="Matching deploys" prevLabel="Previous match" nextLabel="Next match" />
  </div>
</template>
```

Svelte · `one-result.svelte`

```svelte
<script lang="ts">
  import { Pager } from "$lib/components/ui/pager";
</script>

<!-- with a single page both buttons are disabled, and the pager stays in its place -->
<div class="flex w-80 items-center justify-between gap-3">
  <span class="text-sm text-ink-2">1 deploy matches “usage-alerts”</span>
  <Pager count={1} label="Matching deploys" prevLabel="Previous match" nextLabel="Next match" />
</div>
```

## Recipe

`pager.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* pager — previous · position · next. The buttons are the real button (ghost, sm, square, muted:
 * ink-3 until hovered or pressed); the position is the role="status" region that announces "2 of 3".
 *
 * Layout: between (prev · position · next) or start (position · prev · next). Orientation:
 * horizontal carets point left and right and mirror right-to-left; vertical carets point up and
 * down and never mirror.
 *
 * Rolling digits: each digit is the ten figures stacked in one cell. The current one rests in
 * normal layout with no transform, so it sits on the same pixel rows as the text beside it at every
 * display scale (a reel parked at translateY(-n em) rests on a fraction of a device pixel at 125%
 * or 175% and some figures then render one pixel off). Lower figures wait one
 * line above, higher ones one line below, unseen; a change moves the outgoing figure out and the
 * incoming one in, by transform and opacity, instantly from the keyboard. Nothing at first paint.
 *
 * The previous and next buttons are
 * the real Button, marked data-pager="prev|next"; the other parts carry data-slot="pager-<part>".
 * A reel's figures are placed by their order against the reel's `digit` variant (the digit it
 * shows); the figures have no part of their own, so the reel styles them as its children. */

export const pager = tv({
  slots: {
    base: "inline-flex shrink-0 items-center gap-0.5",
    /* passed to the buttons as className, after their own classes */
    prev: "",
    next: "",
    /* "2 of 3" reads left to right on every page, and its reels keep their order */
    position: "inline-flex items-center text-2xs leading-none text-ink-2 tabular-nums whitespace-pre [direction:ltr] [unicode-bidi:isolate]",
    /* the visible text when it is not what is spoken: it reads exactly like plain text */
    text: "[white-space:inherit]",
    digits: "inline-flex",
    digit: "inline-block h-[1em] overflow-hidden",
    /* every figure in one cell, unseen, waiting one line below; it moves by transform and
     * opacity, instantly from the keyboard (the reel carries kbd-instant; its figures are
     * what moves) */
    reel: [
      "kbd-instant",
      "grid [&>span]:[grid-area:1/1] [&>span]:h-[1em] [&>span]:opacity-0 [&>span]:[transform:translateY(1em)]",
      "[&>span]:motion-transform-move [&>span]:motion-opacity-move [&>span]:kbd-instant",
    ],
    total: "whitespace-pre",
  },
  variants: {
    layout: {
      between: { position: "px-1" },
      start: { position: "order-[-1] pe-1" },
    },
    orientation: {
      horizontal: {
        prev: "[&:dir(rtl)_[data-slot=icon]]:[transform:scaleX(-1)]",
        next: "[&:dir(rtl)_[data-slot=icon]]:[transform:scaleX(-1)]",
      },
      vertical: {},
    },
    /* the digit a reel shows: lower figures wait one line above, the current one is in place,
     * untransformed (so it sits on the same pixel rows as the text beside it at every display
     * scale; a reel at a transform would land a pixel off after a few changes) */
    digit: {
      "0": { reel: "[&>:nth-child(1)]:opacity-100 [&>:nth-child(1)]:[transform:none]" },
      "1": { reel: "[&>:nth-child(-n+1)]:[transform:translateY(-1em)] [&>:nth-child(2)]:opacity-100 [&>:nth-child(2)]:[transform:none]" },
      "2": { reel: "[&>:nth-child(-n+2)]:[transform:translateY(-1em)] [&>:nth-child(3)]:opacity-100 [&>:nth-child(3)]:[transform:none]" },
      "3": { reel: "[&>:nth-child(-n+3)]:[transform:translateY(-1em)] [&>:nth-child(4)]:opacity-100 [&>:nth-child(4)]:[transform:none]" },
      "4": { reel: "[&>:nth-child(-n+4)]:[transform:translateY(-1em)] [&>:nth-child(5)]:opacity-100 [&>:nth-child(5)]:[transform:none]" },
      "5": { reel: "[&>:nth-child(-n+5)]:[transform:translateY(-1em)] [&>:nth-child(6)]:opacity-100 [&>:nth-child(6)]:[transform:none]" },
      "6": { reel: "[&>:nth-child(-n+6)]:[transform:translateY(-1em)] [&>:nth-child(7)]:opacity-100 [&>:nth-child(7)]:[transform:none]" },
      "7": { reel: "[&>:nth-child(-n+7)]:[transform:translateY(-1em)] [&>:nth-child(8)]:opacity-100 [&>:nth-child(8)]:[transform:none]" },
      "8": { reel: "[&>:nth-child(-n+8)]:[transform:translateY(-1em)] [&>:nth-child(9)]:opacity-100 [&>:nth-child(9)]:[transform:none]" },
      "9": { reel: "[&>:nth-child(-n+9)]:[transform:translateY(-1em)] [&>:nth-child(10)]:opacity-100 [&>:nth-child(10)]:[transform:none]" },
    },
  },
  defaultVariants: { layout: "between", orientation: "horizontal" },
});

export type PagerRecipeProps = VariantProps<typeof pager>;
```
