suiss/ui ยท Interaction
Stepper
A numeric field with minus and plus buttons that step the value, accelerating while held.
Stepper is an open-source interaction from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Stepper recreates the iOS stepper on top of a number field primitive. It pairs an editable StepperInput with a segmented StepperGroup of StepperDecrement and StepperIncrement buttons. Each press changes the value by step; holding a button repeats the change after a short delay and speeds up over time. Each button disables itself when the value reaches min or max.
Use it for small, bounded quantities such as copies, servings, or scale, where people usually adjust a value by a few steps.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a Stepper:
Examples
A print settings list with labelled steppers. The scale row uses a percent format with a step of 5, and the last row sets disabled.
Accessibility
- Label
StepperInputwitharia-labeloraria-labelledby. - The arrow keys step the value from the input, as provided by the number field primitive.
- The buttons are labelled
DecreaseandIncrease, and are disabled at the limits or when the stepper is disabled. StepperGrouphasrole="group", andStepperSeparatorisaria-hidden.
API Reference
Stepper
The Number Field root that holds the value and its limits.
| Prop | Type | Default |
|---|---|---|
step | number | 1 |
value | number | - |
defaultValue | number | 0 |
onValueChange | (value: number) => void | - |
min | number | - |
max | number | - |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
format | Intl.NumberFormatOptions | - |
Also accepts the props of the primitive part it wraps.
Compose StepperInput and a StepperGroup containing StepperDecrement, StepperSeparator and StepperIncrement inside Stepper. The value is uncontrolled by default; pass value and onValueChange to control it. min and max have no limit by default, and disabled disables the input and both buttons. Other number field props pass through to the root. Values are rounded to the precision of step, so decimal steps do not accumulate floating-point errors. Each part sets a data-slot, such as stepper-input, for styling.
format sets how the number is formatted in the input.
StepperInput
The editable text field that shows the value.
Also accepts the props of the primitive part it wraps.
StepperGroup
The segmented container for the decrement and increment buttons.
Also accepts the props of React.ComponentProps<"div">.
StepperDecrement
The button that decreases the value by one step.
Also accepts the props of React.ComponentProps<"button">.
StepperIncrement
The button that increases the value by one step.
Also accepts the props of React.ComponentProps<"button">.
Both buttons render minus and plus icons by default; pass children to replace them. They suppress the long-press context menu so holding them on touch devices keeps stepping.
StepperSeparator
A thin divider between the two buttons.
Also accepts the props of React.ComponentProps<"span">.
Web preview
Stepper is a number field laid out as a right-aligned value followed by a field holding minus and plus buttons, inset from its edges. Each step snaps to step and stays within min and max, and a button that can go no further is shown as disabled.
Pressing and holding a button steps once, then repeats every 60 ms after 400 ms. The value can also be typed. Use it for quantities, copies or percentages in settings rows.
Installation
The CLI also adds what it builds on: Icon.
- Install the following dependencies:
-
Add the pieces it builds on: Icon.
-
Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/stepper.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Add the pieces it builds on: Icon.
-
Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/stepper.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
-
Add the pieces it builds on: Icon.
-
Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/interactions/stepper.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
A print settings list with copies, a percentage scale stepping by 5, and a disabled row.
Web preview
Accessibility
- The minus and plus buttons have the
buttonrole. - Label the value with
accessibilityLabeloraccessibilityLabelledByonStepperInput. - The separator is hidden from screen readers.
API Reference
Stepper
The root that holds the value and limits.
| Prop | Type | Default |
|---|---|---|
value | number | - |
defaultValue | number | 0 |
onValueChange | ( value: number, eventDetails: NumberFieldPrimitive.ChangeEventDetails ) => void | - |
Also accepts the props of React.ComponentProps<typeof NumberFieldPrimitive.Root>.
Compose StepperInput and a StepperGroup containing StepperDecrement, StepperSeparator and StepperIncrement inside Stepper. Use it uncontrolled with defaultValue or controlled with value and onValueChange, which is called with the new number and change details. Clearing the field never reports null; the change is cancelled instead. min, max, step, format (Intl.NumberFormatOptions), locale and disabled are passed to the number field. A selection haptic plays on each step, if the app provides an adapter through HapticsProvider.
StepperInput
The editable, right-aligned value.
Also accepts the props of React.ComponentProps<typeof NumberFieldPrimitive.Input>.
StepperGroup
The rounded field that holds the buttons.
Also accepts the props of React.ComponentProps<typeof NumberFieldPrimitive.Group>.
StepperDecrement
The minus button.
Also accepts the props of React.ComponentProps<typeof NumberFieldPrimitive.Decrement>.
StepperIncrement
The plus button.
Also accepts the props of React.ComponentProps<typeof NumberFieldPrimitive.Increment>.
StepperSeparator
A thin divider between the buttons.
Also accepts the props of ViewProps.
ValueStepper
| Parameter | Type | Default |
|---|---|---|
value | Binding<Double> | - |
in | ClosedRange<Double> | -Double.infinity...Double.infinity |
step | Double | 1 |
format | F | - |
content | () -> Content | - |
Overloads: init(value:in:step:format:content:), init(value:in:step:content:), init(value:in:step:content:).
StepperInput
The editable, right-aligned value.
Takes no parameters.
StepperGroup
The rounded field that holds the buttons.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
StepperDecrement
The minus button.
| Parameter | Type | Default |
|---|---|---|
label | () -> Label | - |
Overloads: init(label:), init().
StepperIncrement
The plus button.
| Parameter | Type | Default |
|---|---|---|
label | () -> Label | - |
Overloads: init(label:), init().
StepperSeparator
A thin divider between the buttons.
Takes no parameters.
Stepper
The root that holds the value and limits.
| Parameter | Type | Default |
|---|---|---|
value | Double | - |
onValueChange | (Double) -> Unit | - |
modifier | Modifier | Modifier |
min | Double | Double.NEGATIVE_INFINITY |
max | Double | Double.POSITIVE_INFINITY |
step | Double | 1.0 |
enabled | Boolean | true |
format | NumberFormat | remember { NumberFormat.getNumberInstance() } |
content | @Composable RowScope.() -> Unit | - |
StepperInput
The editable, right-aligned value.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
contentDescription | String? | null |
StepperGroup
The rounded field that holds the buttons.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable RowScope.() -> Unit | - |
StepperDecrement
The minus button.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
contentDescription | String | "Decrease" |
content | (@Composable () -> Unit)? | null |
StepperIncrement
The plus button.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
contentDescription | String | "Increase" |
content | (@Composable () -> Unit)? | null |
StepperSeparator
A thin divider between the buttons.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |