How it works
A native type="text" input with role="spinbutton", not type="number", which changes value on wheel and hides partial input. Non-numeric characters cannot be typed, full-width digits are converted, and onChange fires only for valid values or null. Blur or Enter clamps to min / max and pads to precision. Holding a button waits 400ms, then repeats from 120ms down to 32ms. On touch screens the keys switch to round minus left, plus right, with a 44px hit area.
Use it for
- Numbers with limits: price, quantity, duration, tip, shipping fee.
- Small counts that usually change by one, such as cart quantities: use controls="sides" for a stepper.
- For money use precision={2} and put units in prefix / suffix, not in the placeholder.
Look elsewhere when
- Phone numbers, verification codes, order numbers: they are digit strings that cannot be stepped; use a text input with inputMode="numeric".
- Picking a rough value in a range: use a slider.
- A few fixed choices (1 / 3 / 6 / 12): use radio buttons.
Usage
import { useState } from 'react'
import { NumberInput } from './NumberInput.jsx'
export function PriceField() {
const [price, setPrice] = useState(6)
return (
<>
<label htmlFor="price">Price per bag</label>
<NumberInput
id="price"
value={price}
onChange={setPrice}
min={0.5}
max={99}
step={0.5}
precision={2}
prefix="$"
suffix="per bag"
/>
</>
)
}
// Quantity stepper: minus on the left, plus on the right, number centered
// <NumberInput controls="sides" defaultValue={1} min={1} max={24} aria-label="Quantity" />Props
| Prop | Type | Default | What it does |
|---|---|---|---|
value | number | null | — | Controlled value; null means empty. Omit for uncontrolled. |
defaultValue | number | null | — | Initial value when uncontrolled. |
onChange | (value: number | null) => void | — | Always receives a valid value (in range, within precision) or null when cleared. |
min | number | — | Minimum; the decrease button greys out there. Use a negative min to allow negatives. |
max | number | — | Maximum; the increase button greys out there. |
step | number | 1 | Change per click or Up / Down press; PageUp / PageDown use 10 times this. |
precision | number | — | Decimal places: extra digits cannot be typed and blur pads zeros (18 becomes 18.00 at 2). When omitted, the decimals of step apply and nothing is padded. |
controls | 'right' | 'sides' | 'none' | 'right' | Step keys: two small rounded keys stacked on the right, round minus and plus keys inside the field at both ends (the value is then bold and centred), or hidden. On touch screens right becomes sides. |
size | 'sm' | 'md' | 'lg' | 'md' | Height 36 / 44 / 52px, radius 10 / 12 / 14px, text 14 / 15 / 16px. Use lg on mobile. |
prefix | ReactNode | — | Unit or symbol inside the field on the left, such as “$”. |
suffix | ReactNode | — | Unit inside the field on the right, such as “kg” or “per bag”. |
invalid | boolean | false | Error state: danger hairline and a faint danger tint, plus aria-invalid="true". |
disabled | boolean | false | Disabled: the fill stays, the hairline goes, text and keys are muted. |
inputMode | string | chosen from precision | Mobile keyboard: numeric without decimals, decimal with them, text when min is negative (numeric keypads have no minus sign). |
labels | object | — | Replaces built-in text (increase, decrease). Shallow-merged over the English defaults. |
ref | Ref<HTMLInputElement> | — | Forwarded to the native <input>. |
...rest | InputHTMLAttributes | — | className and style go on the outer element; other native attributes (id, name, placeholder, readOnly, onBlur) go to the <input>. |
Accessibility
- The input has role="spinbutton" with aria-valuenow, aria-valuemin and aria-valuemax; link a <label htmlFor> or add aria-label.
- Up / Down change one step, PageUp / PageDown ten steps, Enter normalizes the value; holding the arrow keys repeats.
- Step buttons are outside the Tab order, named “Increase” and “Decrease”, and natively disabled at the limits.
- The mouse wheel never changes the value, so scrolling the page past it is safe.
- On touch screens the step keys have a hit area of at least 44px, and tapping them does not open the keyboard. With reduced motion the value does not roll and the keys do not spring.
Motion
On focus the fill turns white in 160ms and the accent line and 4px halo grow in over 220ms (ease-out). A stepped value rolls in 7px from the direction it moved, with a fade, in 220ms. Keys press to 0.88 in 100ms and spring back in 360ms. Holding repeats after 400ms, from every 120ms down to every 32ms.
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
NumberInput.jsxnumber-input.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





