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:
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.
import { NumberInput } from '@ark-ui/react'
export const MinMax = () => (
<NumberInput.Root min={0} max={10}>
<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 MinMax = () => (
<NumberInput.Root min={0} max={10}>
<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 :min="0" :max="10">
<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>
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
.
import { NumberInput } from '@ark-ui/react'
export const FractionDigits = () => (
<NumberInput.Root
formatOptions={{ minimumFractionDigits: 2, maximumFractionDigits: 3 }}
defaultValue="1.00"
>
<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 FractionDigits = () => (
<NumberInput.Root
formatOptions={{ minimumFractionDigits: 2, maximumFractionDigits: 4 }}
value="1.00"
>
<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="{ minimumFractionDigits: 2, maximumFractionDigits: 3 }"
model-value="1.00"
>
<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>
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.
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
.
import { NumberInput } from '@ark-ui/react'
export const MouseWheel = () => (
<NumberInput.Root allowMouseWheel>
<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 MouseWheel = () => (
<NumberInput.Root allowMouseWheel>
<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 allowMouseWheel>
<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>
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
.
import { NumberInput } from '@ark-ui/react'
export const NoClamp = () => (
<NumberInput.Root clampValueOnBlur={false}>
<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 NoClamp = () => (
<NumberInput.Root clampValueOnBlur={false}>
<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 :clampValueOnBlur="false">
<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>
Usage within forms
To use the number input within forms, set the name
prop.
import { NumberInput } from '@ark-ui/react'
export const FormUsage = () => (
<NumberInput.Root name="quantity">
<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 FormUsage = () => (
<NumberInput.Root name="quantity">
<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 name="quantity">
<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>
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
.
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>
Using the Field Component
The Field
component helps manage form-related state and accessibility attributes of a number input.
It includes handling ARIA labels, helper text, and error text to ensure proper accessibility.
import { Field, NumberInput } from '@ark-ui/react'
export const WithField = (props: Field.RootProps) => (
<Field.Root {...props}>
<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>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
)
import { Field, NumberInput } from '@ark-ui/solid'
export const WithField = (props: Field.RootProps) => (
<Field.Root {...props}>
<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>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
)
<script setup lang="ts">
import { Field, NumberInput } from '@ark-ui/vue'
</script>
<template>
<Field.Root>
<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>
<Field.HelperText>Additional Info</Field.HelperText>
<Field.ErrorText>Error Info</Field.ErrorText>
</Field.Root>
</template>
API Reference
Accessibility
Complies with the Spinbutton WAI-ARIA design pattern.
Keyboard Support
Key | Description |
---|---|
ArrowUp | Increments the value of the number input by a predefined step. |
ArrowDown | Decrements the value of the number input by a predefined step. |
PageUp | Increments the value of the number input by a larger predefined step. |
PageDown | Decrements the value of the number input by a larger predefined step. |
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. |