Stepper

A fully interactive, accessible, user interface component that allows users to increment or decrement a value. It is commonly used for inputting numerical values, such as quantities or prices.

Installation #

You can install this component via the shadcn CLI.

npx shadcn@latest add https://jakeisonline.com/components/registry/stepper.json

Usage #

import {
  Stepper,
  StepperButton,
  StepperInput,
  StepperLabel,
} from "@/components/ui/stepper"
<Stepper start={0}>
  <StepperLabel htmlFor="stepper">Quantity</StepperLabel>
  <StepperButton direction="down">-</StepperButton>
  <StepperInput id="stepper" />
  <StepperButton direction="up">+</StepperButton>
</Stepper>

Accessibility #

Adheres to the Spinbutton WAI-ARIA design pattern . At its core, this component is simply a native number field .

<StepperInput> requires the property id (string) to be set, and should match the htmlFor (string) of the <StepperLabel>.

Keyboard interactions #

KeyAction
ArrowUpIncrement value by step, or 1
ArrowDownDecrement value by step, or 1
Shift + ArrowUpIncrement value by shift
Shift + ArrowDownDecrement value by shift
PageUpIncrement value by shift
PageDownDecrement value by shift
HomeSet value to min
EndSet value to max

Component Reference #

Stepper #

Used to wrap a single stepper field. Also provides context for all child components.

PropTypeDescription
startnumberThe initial value of the field. Default 0.
stepnumberThe step size of the field. Default 1.
shiftnumberThe step size of the field when holding Shift or PageUp/PageDown.
minnumberThe minimum value of the field.
maxnumberThe maximum value of the field.

StepperLabel #

The <label> of the stepper field.

PropTypeDescription
htmlForstringShould match the id of the StepperInput.

StepperButton #

The controller <button> of a stepper field, which users can interact with to step the value up or down.

PropTypeDescription
directionstringup or down depending on desired direction. Required.

StepperInput #

The <input> of the stepper field.

PropTypeDescription
idstringShould match the htmlFor of the StepperLabel.

StepperBadge #

An optional floating badge that displays the current value of the field.

PropTypeDescription
hideWhennumberAutomatically hides the badge when the value is equal to this number. Defaults to 0.

Examples #

Allow shift stepping #

Shift stepping allowing users to increment or decrement a value by a larger amount. When shift is set, users can shift step by holding Shift while clicking a <StepperButton>, or by using the keyboard interactions.

Collapsing stepper fields #

You might want to display multiple quantities in a single row. For example, when ordering multiple product variants in bulk, or providing measurements for a product.

3