Shadcn Number Input
A numeric field with increment and decrement buttons. It clamps to min and max, snaps to step, and formats with Intl.NumberFormat, so currency, percent and units show as people expect while the form submits the plain number. Buttons repeat while held, the arrow keys step with Shift for big steps and Alt for fine ones, and a scrub area changes the value as you drag.
Installation
npx shadcn@latest add @shadcnuikit/number-input1
This adds components/ui/number-input.tsx, the button, field and input-group components it depends on, and a demo file. Set up the registry once as described in Shadcn CLI. Number Input is free and installs without an API key.
Manual installation
Install the dependencies and the shadcn/ui components:
npm install @ui-sdk/react@beta lucide-react
npx shadcn@latest add button field input-group
Copy the source into components/ui/number-input.tsx:
"use client";
import * as React from "react";import { NumberInput as NumberInputPrimitive, NumberInputControl as NumberInputControlPrimitive, NumberInputDecrement as NumberInputDecrementPrimitive, NumberInputGroup as NumberInputGroupPrimitive, NumberInputIncrement as NumberInputIncrementPrimitive, NumberInputScrubArea as NumberInputScrubAreaPrimitive, NumberInputScrubAreaCursor as NumberInputScrubAreaCursorPrimitive, createNumberInputController,} from "@ui-sdk/react/number-input";import { ChevronDownIcon, ChevronUpIcon, MinusIcon, MoveHorizontalIcon, PlusIcon } from "lucide-react";
import { Button } from "@/components/ui/button";import { InputGroup, InputGroupInput } from "@/components/ui/input-group";import { cn } from "@/lib/utils";
type WithClassName<T> = Omit<T, "className"> & { className?: string };
function NumberInput({ className, ...props}: WithClassName<React.ComponentProps<typeof NumberInputPrimitive>>) { return ( <NumberInputPrimitive className={cn("group/number-input flex w-full flex-col gap-2", className)} {...props} /> );}
function NumberInputGroup({ className, ...props}: WithClassName<React.ComponentProps<typeof NumberInputGroupPrimitive>>) { return ( <NumberInputGroupPrimitive render={<InputGroup />} className={cn( "overflow-hidden bg-transparent", "has-[[data-slot=number-input-control]:focus-visible]:border-ring has-[[data-slot=number-input-control]:focus-visible]:ring-3 has-[[data-slot=number-input-control]:focus-visible]:ring-ring/50", "has-[[data-slot=number-input-control][aria-invalid=true]]:border-destructive has-[[data-slot=number-input-control][aria-invalid=true]]:ring-3 has-[[data-slot=number-input-control][aria-invalid=true]]:ring-destructive/20 dark:has-[[data-slot=number-input-control][aria-invalid=true]]:ring-destructive/40", // A step button at its bound is disabled, which would trip InputGroup's has-disabled look. "has-disabled:bg-transparent has-disabled:opacity-100 dark:has-disabled:bg-input/30", "data-disabled:bg-input/50 data-disabled:opacity-50 dark:data-disabled:bg-input/80", "data-readonly:bg-muted/50", "[&>[data-slot=number-input-decrement]:first-child]:border-e [&>[data-slot=number-input-increment]:not(:first-child)]:border-s [&>[data-slot=number-input-decrement]:not(:first-child)]:border-s", className, )} {...props} /> );}
function NumberInputControl({ className, ...props}: WithClassName<React.ComponentProps<typeof NumberInputControlPrimitive>>) { return ( <NumberInputControlPrimitive render={<InputGroupInput />} className={cn("h-full tabular-nums read-only:cursor-default", className)} {...props} /> );}
const stepButton = "h-full w-8 shrink-0 rounded-none border-0 border-input text-muted-foreground shadow-none hover:text-foreground active:not-aria-[haspopup]:translate-y-0 data-disabled:pointer-events-none data-disabled:opacity-50 [&_svg:not([class*='size-'])]:size-3.5";
function NumberInputIncrement({ className, children, ...props}: WithClassName<React.ComponentProps<typeof NumberInputIncrementPrimitive>>) { return ( <NumberInputIncrementPrimitive render={<Button type="button" variant="ghost" size="icon" />} className={cn(stepButton, className)} aria-label="Increase" {...props} > {children ?? <PlusIcon aria-hidden="true" />} </NumberInputIncrementPrimitive> );}
function NumberInputDecrement({ className, children, ...props}: WithClassName<React.ComponentProps<typeof NumberInputDecrementPrimitive>>) { return ( <NumberInputDecrementPrimitive render={<Button type="button" variant="ghost" size="icon" />} className={cn(stepButton, className)} aria-label="Decrease" {...props} > {children ?? <MinusIcon aria-hidden="true" />} </NumberInputDecrementPrimitive> );}
function NumberInputStepper({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="number-input-stepper" className={cn( "flex h-full shrink-0 flex-col border-s border-input", "*:h-1/2 *:w-6 [&>*+*]:border-t", className, )} {...props} > <NumberInputIncrement> <ChevronUpIcon aria-hidden="true" /> </NumberInputIncrement> <NumberInputDecrement> <ChevronDownIcon aria-hidden="true" /> </NumberInputDecrement> </div> );}
function NumberInputScrubArea({ className, ...props}: WithClassName<React.ComponentProps<typeof NumberInputScrubAreaPrimitive>>) { return ( <NumberInputScrubAreaPrimitive className={cn( "inline-flex w-fit cursor-ew-resize touch-none items-center select-none", "data-[orientation=vertical]:cursor-ns-resize data-disabled:cursor-default data-readonly:cursor-default", "[&_label]:cursor-[inherit]", className, )} {...props} /> );}
function NumberInputScrubAreaCursor({ className, children, ...props}: WithClassName<React.ComponentProps<typeof NumberInputScrubAreaCursorPrimitive>>) { return ( <NumberInputScrubAreaCursorPrimitive className={cn( "pointer-events-none fixed top-0 left-0 z-50 -mt-2.5 -ml-2.5 text-foreground drop-shadow-sm [&_svg:not([class*='size-'])]:size-5", className, )} {...props} > {children ?? <MoveHorizontalIcon aria-hidden="true" />} </NumberInputScrubAreaCursorPrimitive> );}
export { NumberInput, NumberInputControl, NumberInputDecrement, NumberInputGroup, NumberInputIncrement, NumberInputScrubArea, NumberInputScrubAreaCursor, NumberInputStepper, createNumberInputController,};
Usage
import {
NumberInput,
NumberInputControl,
NumberInputDecrement,
NumberInputGroup,
NumberInputIncrement,
} from "@/components/ui/number-input";
<NumberInput defaultValue={4} min={0}>
<NumberInputGroup>
<NumberInputControl aria-label="Quantity" />
<NumberInputDecrement />
<NumberInputIncrement />
</NumberInputGroup>
</NumberInput>
The order of the parts inside NumberInputGroup decides the layout. Borders between the buttons and the input follow that order.
With a label
import { Field as FieldPrimitive, FieldLabel as FieldLabelPrimitive } from "@ui-sdk/react/field";
import { Field, FieldLabel } from "@/components/ui/field";
<FieldPrimitive render={<Field />}>
<FieldLabelPrimitive render={<FieldLabel />}>Quantity</FieldLabelPrimitive>
<NumberInput defaultValue={4} min={0}>
<NumberInputGroup>
<NumberInputControl />
</NumberInputGroup>
</NumberInput>
</FieldPrimitive>;
The ui-sdk Field connects the label to the input. Without it, put aria-label on NumberInputControl.
Controlled
const [value, setValue] = React.useState<number | null>(4);
<NumberInput value={value} onValueChange={setValue}>
<NumberInputGroup>
<NumberInputControl aria-label="Quantity" />
</NumberInputGroup>
</NumberInput>;
The value is a number, or null while the field is empty. Use onValueCommit to save once typing, a held key or a button press ends.
Examples
Button Placement
Put NumberInputDecrement before the control and NumberInputIncrement after it to place buttons on both sides.
Stepper
NumberInputStepper stacks up and down chevrons at the end of the field.
Min, Max and Step
min and max bound the value, and step sets how far each step moves. Typed values are clamped and snapped when committed.
Format
format takes Intl.NumberFormat options, such as currency, percent and unit. Percents are fractions, so 0.15 shows as 15%.
Mouse Wheel
wheel lets the mouse wheel step the value while the input has focus.
Scrub Area
Wrap a label in NumberInputScrubArea and drag it sideways to change the value. NumberInputScrubAreaCursor follows the pointer while scrubbing.
Disabled and Read Only
readOnly keeps the field focusable and submitted but stops changes. disabled turns it off and leaves it out of the form.
More layouts built with Number Input, such as long press repeat, a cart quantity and right to left text, are on the Number Input components page.
API Reference
NumberInput
The root, plus the hidden input that submits the number.
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | null | The value, or null for an empty field. Pass it to control the input. | |
defaultValue | number | null | null | The value at first, when uncontrolled. A form reset returns to it. |
onValueChange | (value, details) => void | Runs as a typed number becomes valid and on every step. details.reason says why, details.cancel() stops it. | |
onValueCommit | (value, details) => void | Runs once an interaction ends: typing is committed, a key or button is released, a scrub ends. Use it for saving. | |
min | number | The lowest value. Typed values below it are raised to it on commit. | |
max | number | The highest value. Typed values above it are lowered to it on commit. | |
step | number | 1 | How far one step moves. Values snap to steps from min, or from 0. With a percent format, it defaults to the smallest percent shown, like 0.01. |
largeStep | number | 10 steps | How far PageUp, PageDown, and Shift with an arrow, a button, the wheel or a scrub move. |
smallStep | number | a tenth of a step | How far Alt with an arrow, the wheel or a scrub moves. |
format | Intl.NumberFormatOptions | How the value shows, like { style: "currency", currency: "USD" }. Typing accepts the same format. | |
locale | string | the page's | The locale for formatting and reading typed numbers. |
wheel | boolean | false | Lets the mouse wheel step the value while the input has focus. |
disabled | boolean | false | Can't be focused or changed, and isn't submitted. |
readOnly | boolean | false | Stays focusable and submitted, but stops changes. |
required | boolean | false | Blocks form submission while empty, with the browser's own message. |
name | string | The name it submits under. The number is submitted as is, like 1234.5, never formatted. | |
form | string | The id of a form to belong to, when the input is outside it. | |
controller | NumberInputController | Steps the input from anywhere. Create one with createNumberInputController(). | |
className | string | Classes for the root. |
The root has data-disabled, data-readonly, data-required and data-scrubbing attributes for styling.
NumberInputGroup
An InputGroup that holds the control and its buttons. It shows the focus ring and the invalid border of the control inside it.
NumberInputControl
The text field that shows the formatted number and takes typing. It takes every input prop except value, defaultValue and readOnly, which come from the root.
NumberInputIncrement and NumberInputDecrement
Ghost icon buttons that step once, then repeat while held. Shift steps by largeStep. They render PlusIcon and MinusIcon unless you pass children.
| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | false | Turns off this button alone. |
aria-label | string | "Increase", "Decrease" | The button name. Pass it to translate. |
children | ReactNode | PlusIcon, MinusIcon | The button content. |
className | string | Classes for the button. |
A button at the bound it steps toward gets data-at-bound and data-disabled.
NumberInputStepper
Up and down chevron buttons stacked in one column. It takes every div prop.
NumberInputScrubArea
Drag across it to change the value. Wrap a label or an icon in it.
| Prop | Type | Default | Description |
|---|---|---|---|
orientation | "horizontal", "vertical" | "horizontal" | Which way to drag. Right and up increase. |
pixelsPerStep | number | 2 | How far the pointer moves for one step, in CSS pixels. |
controller | NumberInputController | Connects a scrub area outside the number input, like an icon in a toolbar. | |
className | string | Classes for the area. |
NumberInputScrubAreaCursor
The cursor that follows the pointer while scrubbing. Put it inside NumberInputScrubArea. It renders MoveHorizontalIcon unless you pass children.
createNumberInputController
const [controller] = React.useState(() => createNumberInputController());
<NumberInput controller={controller} />;
controller.setValue(10);
controller.increment();
controller.decrement({ large: true });
controller.getValue();
controller.focus();
Keyboard
| Key | Action |
|---|---|
ArrowUp, ArrowDown | Steps the value up or down by step. |
Shift + ArrowUp, ArrowDown | Steps by largeStep. |
Alt + ArrowUp, ArrowDown | Steps by smallStep. |
PageUp, PageDown | Steps by largeStep. |
Home, End | Sets the value to min or max, when they are set. |
Enter | Commits the typed text: parses, clamps, snaps and formats it. |
The step buttons are not in the tab order, since the arrow keys do the same from the input.
Accessibility
- The root is a
group, and the control is a text input with a numericinputmode, so phones show a number keyboard. - The step buttons have
aria-controlspointing at the input and are named "Increase" and "Decrease". Passaria-labelto translate them. - A button at its bound has
aria-disabled, so it is announced as unavailable. - Name the control with a ui-sdk
FieldLabeloraria-label.
For AI Agents
When to use
- Exact numeric values: quantities, prices, sizes, percentages, durations in minutes.
- Values with units or currency that should show formatted but submit as plain numbers.
- Design tool fields where dragging a label to scrub is faster than typing.
When not to use
- Picking an approximate value on a scale. Use a Slider or a Range Slider.
- Codes made of digits, such as ZIP codes, phone numbers or card numbers. Use an Input.
- Picking a time. Use a Time Picker.
Agent notes
- Import from
@/components/ui/number-input. The exports areNumberInput,NumberInputGroup,NumberInputControl,NumberInputIncrement,NumberInputDecrement,NumberInputStepper,NumberInputScrubArea,NumberInputScrubAreaCursorandcreateNumberInputController. onValueChangereceivesnumber | null, never a string. Type state asnumber | null.- Percents are fractions: store
0.15for 15%. The form submits0.15. - Put
min,max,stepandformatonNumberInput, not onNumberInputControl. - Do not add
onClickhandlers to the step buttons. They already step, repeat while held and respect the bounds.
Resources
- Markdown: /docs/components/number-input.md
- Variants: Number Input components, 15 free examples
- Install:
npx shadcn@latest add @shadcnuikit/number-input1 - MCP: call
get_item_codewithnumber-input1from the MCP Server - Full index for agents: llms.txt