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-partattribute 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:
import { NumberInput } from '@ark-ui/react'
export const Basic = () => (
<NumberInput.Root>
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
)
import { NumberInput } from '@ark-ui/solid'
export const Basic = () => (
<NumberInput.Root>
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
)
<script setup lang="ts">
import { NumberInput } from '@ark-ui/vue'
</script>
<template>
<NumberInput.Root>
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
</template>
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.
Example not foundExample not foundExample not foundAdjusting 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.
Example not foundExample not foundExample not foundScrubbing 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.
import { NumberInput } from '@ark-ui/react'
export const Scrubber = () => (
<NumberInput.Root>
<NumberInput.Scrubber />
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
)
import { NumberInput } from '@ark-ui/solid'
export const Scrubber = () => (
<NumberInput.Root>
<NumberInput.Scrubber />
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
)
<script setup lang="ts">
import { NumberInput } from '@ark-ui/vue'
</script>
<template>
<NumberInput.Root>
<NumberInput.Scrubber />
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
</template>
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.
Example not foundExample not foundExample not foundClamp 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.
Example not foundExample not foundExample not foundUsage within forms
To use the number input within forms, set the name prop.
Example not foundExample not foundExample not foundFormat and parse value
To apply custom formatting to the input's value, set the formatOptions and
provide Intl.NumberFormatOptions such as style and currency.
import { NumberInput } from '@ark-ui/react'
export const Formatted = () => (
<NumberInput.Root
formatOptions={{
style: 'currency',
currency: 'USD',
}}
>
<NumberInput.Scrubber />
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
)
import { NumberInput } from '@ark-ui/solid'
export const Formatted = () => (
<NumberInput.Root
formatOptions={{
style: 'currency',
currency: 'USD',
}}
>
<NumberInput.Scrubber />
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
)
<script setup lang="ts">
import { NumberInput } from '@ark-ui/vue'
</script>
<template>
<NumberInput.Root :formatOptions="{ style: 'currency', currency: 'USD' }">
<NumberInput.Scrubber />
<NumberInput.Label>Label</NumberInput.Label>
<NumberInput.Input />
<NumberInput.Control>
<NumberInput.DecrementTrigger>-1</NumberInput.DecrementTrigger>
<NumberInput.IncrementTrigger>+1</NumberInput.IncrementTrigger>
</NumberInput.Control>
</NumberInput.Root>
</template>
API Reference
Root
| Prop | Default | Type |
|---|---|---|
allowMouseWheel | booleanWhether to allow mouse wheel to change the value | |
allowOverflow | true | booleanWhether to allow the value overflow the min/max range |
asChild | booleanRender as a different element type. | |
clampValueOnBlur | true | booleanWhether to clamp the value when the input loses focus (blur) |
defaultValue | stringThe initial value of the number input when it is first rendered. Use when you do not need to control the state of the number input. | |
disabled | booleanWhether the number input is disabled. | |
focusInputOnChange | true | booleanWhether to focus input when the value changes |
form | stringThe associate form of the input element. | |
formatOptions | NumberFormatOptionsThe options to pass to the `Intl.NumberFormat` constructor | |
id | stringThe unique identifier of the machine. | |
ids | Partial<{
root: string
label: string
input: string
incrementTrigger: string
decrementTrigger: string
scrubber: string
}>The ids of the elements in the number input. Useful for composition. | |
inputMode | 'decimal' | InputModeHints 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 |
invalid | booleanWhether the number input value is invalid. | |
locale | 'en-US' | stringThe current locale. Based on the BCP 47 definition. |
max | numberThe maximum value of the number input | |
min | numberThe minimum value of the number input | |
name | stringThe name attribute of the number input. Useful for form submission. | |
onFocusChange | (details: FocusChangeDetails) => voidFunction invoked when the number input is focused | |
onValueChange | (details: ValueChangeDetails) => voidFunction invoked when the value changes | |
onValueInvalid | (details: ValueInvalidDetails) => voidFunction invoked when the value overflows or underflows the min/max range | |
pattern | '[0-9]*(.[0-9]+)?' | stringThe pattern used to check the <input> element's value against |
readOnly | booleanWhether the number input is readonly | |
spinOnPress | booleanWhether to spin the value when the increment/decrement button is pressed | |
step | numberThe amount to increment or decrement the value by | |
translations | IntlTranslationsSpecifies the localized strings that identifies the accessibility elements and their states | |
value | stringThe value of the input |
Control
| Prop | Default | Type |
|---|---|---|
asChild | booleanRender as a different element type. |
DecrementTrigger
| Prop | Default | Type |
|---|---|---|
asChild | booleanRender as a different element type. |
IncrementTrigger
| Prop | Default | Type |
|---|---|---|
asChild | booleanRender as a different element type. |
Input
| Prop | Default | Type |
|---|---|---|
asChild | booleanRender as a different element type. |
Label
| Prop | Default | Type |
|---|---|---|
asChild | booleanRender as a different element type. |
Scrubber
| Prop | Default | Type |
|---|---|---|
asChild | booleanRender as a different element type. |