Stepper
FreeFewer 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.
npx shadcn@latest add @pixilui/stepperPreview
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" />} />;
}When to use
Use it when
- 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.
Use something else when
- Large or precise numbers such as a spend limit: use an input.
- A continuous range such as temperature: use a slider.
Installation
npx shadcn@latest add @pixilui/stepperAdds stepper.tsx with button.tsx, and installs clsx and tailwind-merge. First PixilUI item in this app? Add the registry once, as in Installation.
"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>
);
});
It uses Button: add it the same way. Then npm i clsx tailwind-merge, and add the foundation once per app, as in Installation.
Usage
import { Stepper } from "@/components/pixilui/stepper";
<Stepper
aria-label={aria-label}
decrementIcon={decrementIcon}
incrementIcon={incrementIcon}
onValueChange={onValueChange}
/>Fixed width from its two buttons and the number; fits anywhere a control does.
API reference
aria-labelRequiredstring–What the number counts, for screen readers ("Parallel runs").decrementIconRequiredReactNode–The fewer icon (Lucide Minus).incrementIconRequiredReactNode–The more icon (Lucide Plus with data-stroke="heavy").valuenumber–The value, controlled. Pair with onValueChange.defaultValuenumber–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.minnumber0The lowest value; fewer is disabled there.maxnumberNumber.POSITIVE_INFINITYThe highest value; more is disabled there.stepnumber1How much one press changes the value.decrementLabelstring"Fewer"Accessible name of the fewer button.incrementLabelstring"More"Accessible name of the more button.Also takes every native <div> attribute except defaultValue, onChange, and a ref.
Keyboard
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.