Number Input

A field that allows user input of numeric values.

Loading...

Anatomy

<NumberInput.Root>
  <NumberInput.Label />
  <NumberInput.Scrubber />
  <NumberInput.Control>
    <NumberInput.Input />
    <NumberInput.IncrementTrigger />
    <NumberInput.DecrementTrigger />
  </NumberInput.Control>
</NumberInput.Root>

Examples

Min and Max

Set min and max to limit the value. The input clamps the value to that range.

To allow values outside the min/max range, set clampValueOnBlur to false.

Precision

To round to fixed decimals, pass Intl.NumberFormatOptions like maximumFractionDigits or minimumFractionDigits to formatOptions.

Scrubbing

Render NumberInput.Scrubber to change the value by dragging. It uses the Pointer Lock API to track movement and renders a virtual cursor.

Mouse Wheel

Set allowMouseWheel to increment or decrement with the mouse wheel.

Formatting

Pass Intl.NumberFormatOptions like style and currency to formatOptions.

Field

Field wires up the label, helper text, error text and their ARIA attributes.

Root Provider

Use RootProvider with the useNumberInput hook to access the state and methods from outside the component.

Guides

Scrubber

NumberInput.Scrubber is a <div> users drag to change the value. It shows a custom cursor while scrubbing and uses the Pointer Lock API.

Note: Browsers may show a notification when pointer lock activates. The scrubber is disabled in Safari to prevent layout shifts.

Controlled

Keep the controlled value as a string, not a number. This matters most with formatOptions for currency or locales.

const [value, setValue] = useState('0')

<NumberInput.Root value={value} onValueChange={(details) => setValue(details.value)}>
  {/* ... */}
</NumberInput.Root>

Converting to numbers breaks locale formats with different decimal and thousands separators (e.g., 1.523,30 vs 1,523.30). Strings keep the formatting intact.

To submit a numeric value, add a hidden input that reads value from NumberInput.Context:

<NumberInput.Root value={value} onValueChange={(details) => setValue(details.value)}>
  <NumberInput.Input />
  <NumberInput.Context>
    {(context) => <input type="hidden" name="amount" value={context.value ?? ''} />}
  </NumberInput.Context>
</NumberInput.Root>

API Reference

Props

Root

Defines the <div> element.

AttributeDefaultType
allowMouseWheel
boolean

Whether to allow mouse wheel to change the value

allowOutOfRangefalse
boolean

Whether a typed value can stay outside the min/max range instead of being clamped on blur or Enter. The value is then reported as invalid. Stepping always stays within the range.

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
defaultValue
number

The initial value when rendered. Use when you don't need to control the value.

disabled
boolean

Whether the number input is disabled.

focusInputOnChangetrue
boolean

Whether to focus input when the value changes

form
string

The associate form of the input element.

formatOptions
NumberFormatOptions

The options to pass to the `Intl.NumberFormat` constructor

id
string

The unique identifier of the machine.

ids
Partial<{ root: string label: string input: string incrementTrigger: string decrementTrigger: string scrubber: string scrubberCursor: string hiddenInput: string }>

The ids of the elements in the number input. Useful for composition.

inputMode
InputMode

Hints at the type of data that might be entered by the user. It also determines the type of keyboard shown to the user on mobile devices. Defaults to `"text"` on iPhone when negatives are allowed, otherwise `"decimal"`.

invalid
boolean

Whether the number input value is invalid.

largeStepstep * 10
number

Step value when Shift key is held (large increments).

locale'en-US'
string

The current locale. Based on the BCP 47 definition.

max
number

The maximum value of the number input

min
number

The minimum value of the number input

name
string

The name attribute of the number input. Useful for form submission.

onFocusChange
(details: FocusChangeDetails) => void

Function invoked when the number input is focused

onValueChange
(details: ValueChangeDetails) => void

Function invoked when the value changes

onValueCommit
(details: ValueChangeDetails) => void

Function invoked when the value settles: the input is blurred or `Enter` is pressed, a stepper or scrub gesture ends, or a key or wheel step completes.

onValueInvalid
(details: ValueInvalidDetails) => void

Function invoked when the value overflows or underflows the min/max range

pattern'-?[0-9]*(.[0-9]+)?'
string

The pattern used to check the <input> element's value against

readOnly
boolean

Whether the number input is readonly

render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<NumberInputRootState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

required
boolean

Whether the number input is required

scrubberDirection'horizontal'
'horizontal' | 'vertical'

The direction of scrubbing.

scrubberPixelSensitivity2
number

How many pixels the pointer must move to change the value by one step. Higher values mean more precise control. Lower values mean more sensitivity.

scrubberTeleportDistance
number

Distance in pixels before the virtual cursor wraps around. When not set, the viewport bounds are used (default behavior).

smallStepstep * 0.1
number

Step value when Alt/Option key is held (fine control).

snapOnStepfalse
boolean

Whether values snap to step multiples when scrubbing.

spinOnPresstrue
boolean

Whether to spin the value when the increment/decrement button is pressed

step1
number

The amount to increment or decrement the value by

translations
IntlTranslations

Specifies the localized strings that identifies the accessibility elements and their states

value
number

The controlled value. Use `null` for an empty input.

AttributeDescription
[data-number-input-root]<uid>
[data-disabled]Present when disabled
[data-focus]Present when focused
[data-invalid]Present when invalid
[data-scrubbing]

Control

Defines the <div> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-control]<uid>
[data-focus]Present when focused
[data-disabled]Present when disabled
[data-invalid]Present when invalid
[data-scrubbing]

DecrementTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
type ONLY_FOR_FORMAT = ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<NumberInputDecrementTriggerState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-decrement-trigger]<uid>
[data-disabled]Present when disabled
[data-scrubbing]

IncrementTrigger

Defines the <button> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
type ONLY_FOR_FORMAT = ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<NumberInputIncrementTriggerState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-increment-trigger]<uid>
[data-disabled]Present when disabled
[data-scrubbing]

Input

Defines the <input> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<NumberInputInputState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-input]<uid>
[data-invalid]Present when invalid
[data-disabled]Present when disabled
[data-scrubbing]

Label

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-label]<uid>
[data-disabled]Present when disabled
[data-focus]Present when focused
[data-invalid]Present when invalid
[data-required]Present when required
[data-scrubbing]

RootProvider

Defines the <div> element.

AttributeDefaultType
value
UseNumberInputReturn

asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
type ONLY_FOR_FORMAT = ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<NumberInputRootProviderState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

Scrubber

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-scrubber]<uid>
[data-disabled]Present when disabled
[data-scrubbing]
CSS VariableDescription
--scrubber-xThe scrubber x value for the Scrubber
--scrubber-yThe scrubber y value for the Scrubber
--scrubber-scaleThe scrubber scale value for the Scrubber

ValueText

Defines the <span> element.

AttributeDefaultType
asChild
boolean

Use the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`.

For more details, read our Composition guide.
render
ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>

Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state.

AttributeDescription
[data-number-input-value-text]<uid>
[data-disabled]Present when disabled
[data-invalid]Present when invalid
[data-focus]Present when focused
[data-scrubbing]

Context

API

PropertyType
focused
boolean

Whether the input is focused.

scrubbing
boolean

Whether the input is being scrubbed.

invalid
boolean

Whether the input is invalid.

empty
boolean

Whether the input value is empty.

value
number | null

The number, or `null` when the input is empty.

formattedValue
string

The value as shown in the input.

setValue
(value: number) => void

Function to set the value of the input.

clearValue
VoidFunction

Function to clear the value of the input.

increment
VoidFunction

Function to increment the value of the input by the step.

decrement
VoidFunction

Function to decrement the value of the input by the step.

setToMax
VoidFunction

Function to set the value of the input to the max.

setToMin
VoidFunction

Function to set the value of the input to the min.

focus
VoidFunction

Function to focus the input.

getRootState
() => RootState

Returns the state of the root.

getInputState
() => InputState

Returns the state of the input.

getDecrementTriggerState
() => DecrementTriggerState

Returns the state of the decrement trigger.

getIncrementTriggerState
() => IncrementTriggerState

Returns the state of the increment trigger.

Accessibility

Complies with the Spinbutton WAI-ARIA design pattern.

Keyboard Support

KeyDescription
ArrowUp
Increments the value of the number input by a predefined step.
ArrowDown
Decrements the value of the number input by a predefined step.
Shift + ArrowUp
Increments the value of the number input by the `largeStep` amount.
Shift + ArrowDown
Decrements the value of the number input by the `largeStep` amount.
Alt + ArrowUp
Increments the value of the number input by the `smallStep` amount.
Alt + ArrowDown
Decrements the value of the number input by the `smallStep` amount.
Home
Sets the value of the number input to its minimum allowed value.
End
Sets the value of the number input to its maximum allowed value.
Enter
Submits the value entered in the number input.