# 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.

`number-input-demo.tsx`:

```tsx
import { Field as FieldPrimitive, FieldLabel as FieldLabelPrimitive } from "@ui-sdk/react/field";

import { Field, FieldLabel } from "@/components/ui/field";
import {
  NumberInput,
  NumberInputControl,
  NumberInputDecrement,
  NumberInputGroup,
  NumberInputIncrement,
} from "@/components/ui/number-input";

export default function NumberInputDemo() {
  return (
    <FieldPrimitive render={<Field className="w-48" />}>
      <FieldLabelPrimitive render={<FieldLabel />}>Quantity</FieldLabelPrimitive>
      <NumberInput defaultValue={4} min={0}>
        <NumberInputGroup>
          <NumberInputControl />
          <NumberInputDecrement />
          <NumberInputIncrement />
        </NumberInputGroup>
      </NumberInput>
    </FieldPrimitive>
  );
}
```

## Installation

```bash
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](https://shadcnuikit.com/docs/shadcn-cli). Number Input is free and installs without an API key.

### Manual installation

Install the dependencies and the shadcn/ui components:

```bash
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`:

`components/ui/number-input.tsx`:

```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

```tsx
import {
  NumberInput,
  NumberInputControl,
  NumberInputDecrement,
  NumberInputGroup,
  NumberInputIncrement,
} from "@/components/ui/number-input";
```

```tsx
<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

```tsx
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

```tsx
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.

`number-input-button-placement.tsx`:

```tsx
import {
  NumberInput,
  NumberInputControl,
  NumberInputDecrement,
  NumberInputGroup,
  NumberInputIncrement,
} from "@/components/ui/number-input";

export default function NumberInputButtonPlacement() {
  return (
    <NumberInput defaultValue={2} min={1} className="w-36">
      <NumberInputGroup>
        <NumberInputDecrement />
        <NumberInputControl aria-label="Guests" className="text-center" />
        <NumberInputIncrement />
      </NumberInputGroup>
    </NumberInput>
  );
}
```

### Stepper

`NumberInputStepper` stacks up and down chevrons at the end of the field.

`number-input-stepper.tsx`:

```tsx
import { NumberInput, NumberInputControl, NumberInputGroup, NumberInputStepper } from "@/components/ui/number-input";

export default function NumberInputStepperDemo() {
  return (
    <NumberInput defaultValue={16} min={8} max={72} className="w-36">
      <NumberInputGroup>
        <NumberInputControl aria-label="Font size" />
        <NumberInputStepper />
      </NumberInputGroup>
    </NumberInput>
  );
}
```

### 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.

`number-input-min-max-step.tsx`:

```tsx
import {
  NumberInput,
  NumberInputControl,
  NumberInputDecrement,
  NumberInputGroup,
  NumberInputIncrement,
} from "@/components/ui/number-input";

export default function NumberInputMinMaxStep() {
  return (
    <div className="flex w-48 flex-col gap-4">
      <NumberInput defaultValue={50} min={0} max={100} step={5}>
        <NumberInputGroup>
          <NumberInputControl aria-label="Volume" />
          <NumberInputDecrement />
          <NumberInputIncrement />
        </NumberInputGroup>
      </NumberInput>
      <NumberInput defaultValue={1.5} min={1} max={3} step={0.1}>
        <NumberInputGroup>
          <NumberInputControl aria-label="Line height" />
          <NumberInputDecrement />
          <NumberInputIncrement />
        </NumberInputGroup>
      </NumberInput>
    </div>
  );
}
```

### Format

`format` takes `Intl.NumberFormat` options, such as currency, percent and unit. Percents are fractions, so `0.15` shows as 15%.

`number-input-format.tsx`:

```tsx
import { NumberInput, NumberInputControl, NumberInputGroup, NumberInputStepper } from "@/components/ui/number-input";

const fields = [
  { label: "Price", value: 49.99, step: 0.01, format: { style: "currency", currency: "USD" } },
  { label: "Discount", value: 0.15, format: { style: "percent" } },
  { label: "Weight", value: 2.5, step: 0.1, format: { style: "unit", unit: "kilogram" } },
] satisfies { label: string; value: number; step?: number; format: Intl.NumberFormatOptions }[];

export default function NumberInputFormat() {
  return (
    <div className="flex w-48 flex-col gap-4">
      {fields.map(({ label, value, ...options }) => (
        <NumberInput key={label} defaultValue={value} min={0} {...options}>
          <NumberInputGroup>
            <NumberInputControl aria-label={label} />
            <NumberInputStepper />
          </NumberInputGroup>
        </NumberInput>
      ))}
    </div>
  );
}
```

### Mouse Wheel

`wheel` lets the mouse wheel step the value while the input has focus.

`number-input-wheel.tsx`:

```tsx
import { NumberInput, NumberInputControl, NumberInputGroup, NumberInputStepper } from "@/components/ui/number-input";

export default function NumberInputWheel() {
  return (
    <NumberInput defaultValue={24} min={0} wheel className="w-36">
      <NumberInputGroup>
        <NumberInputControl aria-label="Columns" />
        <NumberInputStepper />
      </NumberInputGroup>
    </NumberInput>
  );
}
```

### Scrub Area

Wrap a label in `NumberInputScrubArea` and drag it sideways to change the value. `NumberInputScrubAreaCursor` follows the pointer while scrubbing.

`number-input-scrub-area.tsx`:

```tsx
import { Field as FieldPrimitive, FieldLabel as FieldLabelPrimitive } from "@ui-sdk/react/field";

import { Field, FieldLabel } from "@/components/ui/field";
import {
  NumberInput,
  NumberInputControl,
  NumberInputGroup,
  NumberInputScrubArea,
  NumberInputScrubAreaCursor,
} from "@/components/ui/number-input";

export default function NumberInputScrubAreaDemo() {
  return (
    <FieldPrimitive render={<Field className="w-48" />}>
      <NumberInput defaultValue={120} min={0}>
        <NumberInputScrubArea>
          <FieldLabelPrimitive render={<FieldLabel />}>Width</FieldLabelPrimitive>
          <NumberInputScrubAreaCursor />
        </NumberInputScrubArea>
        <NumberInputGroup>
          <NumberInputControl />
        </NumberInputGroup>
      </NumberInput>
    </FieldPrimitive>
  );
}
```

### 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.

`number-input-disabled.tsx`:

```tsx
import {
  NumberInput,
  NumberInputControl,
  NumberInputDecrement,
  NumberInputGroup,
  NumberInputIncrement,
} from "@/components/ui/number-input";

export default function NumberInputDisabled() {
  return (
    <div className="flex w-48 flex-col gap-4">
      <NumberInput defaultValue={12} readOnly>
        <NumberInputGroup>
          <NumberInputControl aria-label="Read-only quantity" />
          <NumberInputDecrement />
          <NumberInputIncrement />
        </NumberInputGroup>
      </NumberInput>
      <NumberInput defaultValue={12} disabled>
        <NumberInputGroup>
          <NumberInputControl aria-label="Disabled quantity" />
          <NumberInputDecrement />
          <NumberInputIncrement />
        </NumberInputGroup>
      </NumberInput>
    </div>
  );
}
```

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](https://shadcnuikit.com/components/number-input) 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

```tsx
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 numeric `inputmode`, so phones show a number keyboard.
- The step buttons have `aria-controls` pointing at the input and are named "Increase" and "Decrease". Pass `aria-label` to translate them.
- A button at its bound has `aria-disabled`, so it is announced as unavailable.
- Name the control with a ui-sdk `FieldLabel` or `aria-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](https://shadcnuikit.com/components/slider) or a [Range Slider](https://shadcnuikit.com/components/range-slider).
- Codes made of digits, such as ZIP codes, phone numbers or card numbers. Use an [Input](https://shadcnuikit.com/components/input).
- Picking a time. Use a [Time Picker](https://shadcnuikit.com/components/time-picker).

**Agent notes**

- Import from `@/components/ui/number-input`. The exports are `NumberInput`, `NumberInputGroup`, `NumberInputControl`, `NumberInputIncrement`, `NumberInputDecrement`, `NumberInputStepper`, `NumberInputScrubArea`, `NumberInputScrubAreaCursor` and `createNumberInputController`.
- `onValueChange` receives `number | null`, never a string. Type state as `number | null`.
- Percents are fractions: store `0.15` for 15%. The form submits `0.15`.
- Put `min`, `max`, `step` and `format` on `NumberInput`, not on `NumberInputControl`.
- Do not add `onClick` handlers to the step buttons. They already step, repeat while held and respect the bounds.

**Resources**

- Markdown: [/docs/components/number-input.md](https://shadcnuikit.com/docs/components/number-input.md)
- Variants: [Number Input components](https://shadcnuikit.com/components/number-input), 15 free examples
- Install: `npx shadcn@latest add @shadcnuikit/number-input1`
- MCP: call `get_item_code` with `number-input1` from the [MCP Server](https://shadcnuikit.com/docs/mcp-server)
- Full index for agents: [llms.txt](https://shadcnuikit.com/llms.txt)

Source: https://shadcnuikit.com/docs/components/number-input
More from Shadcn UI Kit: [llms.txt](https://shadcnuikit.com/llms.txt)