# Stepper

Fewer and more around a small number, such as parallel runs or seats, where the number rolls up or down the way it moved and each end disables its button.

Tier: free. Type: component. Category: inputs. Tags: motion, keyboard.

## Install

```bash
npx shadcn@latest add @pixilui/stepper
```

One-time setup: add the registry to `components.json`:

```json
{
  "registries": {
    "@pixilui": "https://pixilui.com/r/{name}.json"
  }
}
```

Files: `components/pixilui/stepper.tsx`.

## Preview

Live preview: https://pixilui.com/components/stepper

Signature: The number rolls up or down the way it moved, never just swapping.

Styles: `quiet` (default), `lively`, set with `data-style` on <html>. The item reads the style through the foundation; it needs no prop. Standalone files: quiet https://pixilui.com/r/standalone/stepper.tsx, lively https://pixilui.com/r/standalone/stepper.lively.tsx.

## Code

`components/pixilui/stepper.tsx`

```tsx
"use client";

import { forwardRef, useEffect, useRef, useState, type ComponentPropsWithoutRef, type ReactNode } from "react";
import { cn } from "./lib/utils";
import { Button } from "./button";

/*
  The locked stepper (exploration 04): fewer and more around a number that rolls the way it
  moved. The roll is the foundation's (pixilui-btn-digit with data-roll, section 7); this file keeps
  the leaving digit until its roll ends, then drops it.
*/

/** How long a leaving digit stays in the DOM: the roll out in foundation.css section 7. */
const ROLL_OUT_MS = 150;

type Digit = { key: number; value: number; roll?: string };

export type StepperProps = Omit<ComponentPropsWithoutRef<"div">, "defaultValue" | "onChange" | "children"> & {
  /** The value, controlled. Pair with onValueChange. */
  value?: number;
  /** The starting value when uncontrolled. Defaults to min. */
  defaultValue?: number;
  /** Runs with the new value after a press of fewer or more. */
  onValueChange?: (value: number) => void | Promise<void>;
  /** The lowest value; fewer is disabled there. */
  min?: number;
  /** The highest value; more is disabled there. */
  max?: number;
  /** How much one press changes the value. */
  step?: number;
  /** The fewer icon (Lucide Minus). */
  decrementIcon: ReactNode;
  /** The more icon (Lucide Plus with data-stroke="heavy"). */
  incrementIcon: ReactNode;
  /** Accessible name of the fewer button. */
  decrementLabel?: string;
  /** Accessible name of the more button. */
  incrementLabel?: string;
  /** What the number counts, for screen readers ("Parallel runs"). */
  "aria-label": string;
};

/** A number between fewer and more, rolling up or down the way it moved. */
export const Stepper = forwardRef<HTMLDivElement, StepperProps>(function Stepper(
  { value: valueProp, defaultValue, onValueChange, min = 0, max = Number.POSITIVE_INFINITY, step = 1, decrementIcon, incrementIcon, decrementLabel = "Fewer", incrementLabel = "More", className, ...rest },
  ref,
) {
  const [inner, setInner] = useState(defaultValue ?? min);
  const value = valueProp ?? inner;
  const [digits, setDigits] = useState<Digit[]>([{ key: 0, value }]);
  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);
  useEffect(() => () => clearTimeout(timer.current), []);

  // A new value (pressed here, or set from outside) rolls in the way it moved.
  const shown = digits[digits.length - 1]!;
  if (shown.value !== value) {
    const dir = value > shown.value ? "up" : "down";
    setDigits([
      { ...shown, roll: `out-${dir}` },
      { key: shown.key + 1, value, roll: `in-${dir}` },
    ]);
  }
  useEffect(() => {
    if (!digits.some((d) => d.roll?.startsWith("out"))) return;
    clearTimeout(timer.current);
    timer.current = setTimeout(() => setDigits((current) => current.filter((d) => !d.roll?.startsWith("out"))), ROLL_OUT_MS);
  }, [digits]);

  const set = (next: number) => {
    const clamped = Math.max(min, Math.min(max, next));
    if (clamped === value) return;
    if (valueProp === undefined) setInner(clamped);
    void onValueChange?.(clamped);
  };

  return (
    <div ref={ref} role="group" className={cn("pixilui-btn-stepper font-body", className)} data-value={value} {...rest}>
      <Button variant="secondary-2" aria-label={decrementLabel} icon={decrementIcon} disabled={value <= min} onClick={() => set(value - step)} />
      <span className="pixilui-btn-stepper-value" aria-live="polite">
        {digits.map((d) => (
          <span key={d.key} className="pixilui-btn-digit" data-roll={d.roll}>
            {d.value}
          </span>
        ))}
      </span>
      <Button variant="secondary-2" aria-label={incrementLabel} icon={incrementIcon} disabled={value >= max} onClick={() => set(value + step)} />
    </div>
  );
});
```

## Usage

When to use:

- Small counts people nudge by one: parallel runs (1 to 8), seats, retries, replicas.
- A value with a clear floor and ceiling that should never be typed out of range.

When not to use:

- Large or precise numbers such as a spend limit: use an input.
- A continuous range such as temperature: use a slider.

```tsx
import { IconAdd } from "@/components/pixilui/icons/add";
import { Stepper } from "@/components/pixilui/stepper";

// One finished element, no rows.
export const knobs = {} as const;

/** The static Hugeicons minus-sign (MIT, Copyright (c) 2025 Hugeicons): minus has no animated icon. */
function IconMinus() {
  return (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
      <path d="M20 12H4" />
    </svg>
  );
}

export default function StepperDemo() {
  return <Stepper aria-label="Parallel runs" min={1} max={8} defaultValue={3} decrementIcon={<IconMinus />} incrementIcon={<IconAdd data-stroke="heavy" />} />;
}
```

## Examples

None.

## API

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `aria-label` (required) | `string` |  | What the number counts, for screen readers ("Parallel runs"). |
| `value` | `number` |  | The value, controlled. Pair with onValueChange. |
| `defaultValue` | `number` |  | The starting value when uncontrolled. Defaults to min. |
| `onValueChange` | `(value: number) => void \| Promise<void>` |  | Runs with the new value after a press of fewer or more. |
| `min` | `number` | `0` | The lowest value; fewer is disabled there. |
| `max` | `number` | `Number.POSITIVE_INFINITY` | The highest value; more is disabled there. |
| `step` | `number` | `1` | How much one press changes the value. |
| `decrementIcon` (required) | `ReactNode` |  | The fewer icon (Lucide Minus). |
| `incrementIcon` (required) | `ReactNode` |  | The more icon (Lucide Plus with data-stroke="heavy"). |
| `decrementLabel` | `string` | `"Fewer"` | Accessible name of the fewer button. |
| `incrementLabel` | `string` | `"More"` | Accessible name of the more button. |

Also accepts every native `<div>` attribute except `defaultValue`, `onChange`, and a ref to it.

## Keyboard

- `Tab`: Moves focus to fewer, then more.
- `Enter`: Presses the focused button: one step down or up.
- `Space`: The same as Enter, on release.

## Accessibility

- A group named by aria-label (required), holding two native buttons named Fewer and More (decrementLabel, incrementLabel).
- The number is a polite live region, so each change is read once.
- At min or max the button is disabled; data-value carries the number for styling and tests.

## Motion

- The number rolls the way it moved: up for more, down for fewer, the old digit leaving as the new one arrives.
- The leaving digit is dropped after its 150ms roll.
- The buttons press like every button: scale .96 in 150ms.
- Reduced motion: the number changes at once.

## Responsive

- Fixed width from its two buttons and the number; fits anywhere a control does.

## Performance

- At most two digits in the DOM, and one timer.

## Notes for AI

- Control it with value and onValueChange, or start it with defaultValue. Set min and max; step defaults to 1.
- Pass decrementIcon and incrementIcon (Lucide Minus and Plus, with data-stroke="heavy" on Plus); never import an icon library inside the component.
- aria-label is required: name what the number counts ("Parallel runs").

## Related

- [Button](https://pixilui.com/components/button/markdown): The PixilUI button in four main styles and four more, five sizes from 24 to 44, with icons, loading, disabled, a count and a shortcut.
- [Segmented control](https://pixilui.com/components/segmented-control/markdown): Two to five mutually exclusive choices on one glass track, with a thumb that glides to the one you pick and jumps at once when you arrow through them.
- [Pagination](https://pixilui.com/components/pagination/markdown): Previous, every page and next as quiet buttons, with one glass marker that glides to the page you pick.
