Number Input

A field that allows user input of numeric values.

Anatomy

To set up the number input correctly, you’ll need to understand its anatomy and how we name its parts.

Each part includes a data-part attribute to help identify them in the DOM.

Examples

Learn how to use the NumberInput component in your project. Let’s take a look at the most basic example:

Setting a minimum and maximum value

Pass the min prop or max prop to set an upper and lower limit for the input. By default, the input will restrict the value to stay within the specified range.

Adjusting the precision of the value

In some cases, you might need the value to be rounded to specific decimal points. Set the formatOptions and provide Intl.NumberFormatOptions such as maximumFractionDigits or minimumFractionDigits.

Scrubbing the input value

The NumberInput supports the scrubber interaction pattern. To use this pattern, render the NumberInput.Scrubber component. It uses the Pointer lock API and tracks the pointer movement. It also renders a virtual cursor which mimics the real cursor’s pointer.

Using the mouse wheel to change value

The NumberInput exposes a way to increment/decrement the value using the mouse wheel event. To activate this, set the allowMouseWheel prop to true.

Clamp value when user blurs the input

In most cases, users can type custom values in the input field. If the typed value is greater than the max, the value is reset to max when the user blur out of the input.

To disable this behavior, pass clampValueOnBlur and set to false.

Usage within forms

To use the number input within forms, set the name prop.

Format and parse value

To apply custom formatting to the input’s value, set the formatOptions and provide Intl.NumberFormatOptions such as style and currency.

API Reference

Root

PropTypeDefault
allowMouseWheel
boolean
allowOverflow
booleantrue
asChild
boolean
clampValueOnBlur
booleantrue
defaultValue
string
disabled
boolean
focusInputOnChange
booleantrue
form
string
formatOptions
NumberFormatOptions
id
string
ids
Partial<{ root: string label: string input: string incrementTrigger: string decrementTrigger: string scrubber: string }>
inputMode
InputMode"decimal"
invalid
boolean
locale
string"en-US"
max
number
min
number
name
string
onFocusChange
(details: FocusChangeDetails) => void
onValueChange
(details: ValueChangeDetails) => void
onValueInvalid
(details: ValueInvalidDetails) => void
pattern
string"[0-9]*(.[0-9]+)?"
readOnly
boolean
spinOnPress
boolean
step
number
translations
IntlTranslations
value
string

Control

PropTypeDefault
asChild
boolean

DecrementTrigger

PropTypeDefault
asChild
boolean

IncrementTrigger

PropTypeDefault
asChild
boolean

Input

PropTypeDefault
asChild
boolean

Label

PropTypeDefault
asChild
boolean

Scrubber

PropTypeDefault
asChild
boolean

Previous

Menu