Number input
A field for one number that people nudge as often as they type: seats, a budget, a threshold. Minus and plus buttons step it, limits hold it in range, and Intl options print it as a currency, a percentage or a unit. For a code or an id use the input.
Install
npx shadcn@latest add https://thrumui.dev/r/number-input.jsonAdds components/ui/number-input.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { NumberInput } from "@/components/ui/number-input";Automatically installs icons minus and plus from the icon set.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | number | null | – | The number, when the parent holds it. null is an empty field. Use it with onValueChange. |
defaultValue | number | – | The number shown at first, when the field holds it itself. Without it the field starts empty. |
onValueChange | (value: number | null) => void | – | Called on every step and on every typed change that reads as a number. null means the field was cleared. |
onValueCommitted | (value: number | null) => void | – | Called once the change is final: on blur after typing, on key or wheel steps, and when a held button is released. |
min | number | – | The lowest allowed value. Steps stop there and a typed value below it is pulled up on blur. |
max | number | – | The highest allowed value. Steps stop there and a typed value above it is pulled down on blur. |
step | number | "any" | Default 1 | How far a button press or an arrow key moves the value. |
smallStep | number | Default 0.1 | The step while Alt is held. |
largeStep | number | Default 10 | The step while Shift is held. |
snapOnStep | boolean | Default false | A step lands on the nearest multiple of step instead of adding to the current value. |
allowOutOfRange | boolean | Default false | Keep a typed value outside min and max as it is, so the form's own validation can report it. |
allowWheelScrub | boolean | Default false | Let the mouse wheel step the value while the field has focus and the pointer is over it. |
format | Intl.NumberFormatOptions | – | How the number is printed: currency, percent, unit, fraction digits. A percentage holds 0.25 and prints 25%. |
locale | string | Default "en-US" | The locale that prints the number and reads what is typed, including its decimal and group separators. |
size | "sm" | "md" | "lg" | Default "md" | sm is 28 pixels tall, md 32, lg 40. The buttons are squares of the same height. |
placeholder | string | – | Shown in the muted text colour while the field is empty. |
id | string | – | The id of the input element, for a label's htmlFor. |
name | string | – | The field name when it sits in a form. The plain number is submitted, not the formatted text. |
disabled | boolean | Default false | The field and both buttons are dimmed and cannot be used. |
readOnly | boolean | Default false | The value can be focused and copied but not changed. |
required | boolean | Default false | The field must hold a number before its form submits. |
aria-invalid | boolean | – | Marks the value as wrong: the border takes the destructive colour. Pair it with a message linked by aria-describedby. |
incrementLabel | string | Default "Increase" | The plus button's name for screen readers. |
decrementLabel | string | Default "Decrease" | The minus button's name for screen readers. |
sound | boolean | Default true | Play a tick for each step from a button, a key or the wheel, if the visitor has sound on. Typing is silent. |
className | string | – | Extra classes for the root, merged last. The field fills its container; set a width here. |
Motion and sound
Motion
- A button's background and icon colour change over 150 ms on hover.
- The border colour changes over 150 ms when the field becomes invalid or valid again.
- The number itself changes at once: a held button repeats too fast for a roll to help.
- Reduced motion: colours change at once.
Sound
- A tick for each step from a button, an arrow key or the wheel. A held button ticks as it repeats, rate limited so it never buzzes.
- Typing a number is silent.
Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works
Accessibility
- The field is a native text input with a numeric keyboard on phones, announced as a number field. A hidden input carries the plain number in a form.
- Its name comes from a label element whose htmlFor is the id, or from an aria-label.
- The buttons are real buttons named Increase and Decrease. They are 28, 32 or 40 pixels square, and are disabled at the limit they would cross.
- The focus ring is 2 pixels of the theme's ring colour around the whole field.
- An invalid field is marked with aria-invalid and its border changes colour. The message next to it carries the meaning.
- Text is 16 pixels on phones, so the page does not zoom when the field takes focus.
- At 160 pixels wide the number keeps the space between the buttons and scrolls inside it when it is longer.
| Keys | Action |
|---|---|
| Tab | Focus the field. The two buttons are not tab stops: the arrow keys do their work. |
| ↑ / ↓ | Add or take away one step. |
| Shift + ↑ / ↓ | Step by largeStep, 10 by default. |
| Alt + ↑ / ↓ | Step by smallStep, 0.1 by default. |
| Home / End | Jump to min or max, when they are set. |
| 0 to 9, separators, minus | Type a number. It is formatted, and pulled into range, when the field loses focus. |
Source
"use client";
import { NumberField } from "@base-ui/react/number-field";
import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { MinusIcon } from "@/components/icons/minus";
import { PlusIcon } from "@/components/icons/plus";
export type NumberInputSize = "sm" | "md" | "lg";
export type NumberInputProps = Omit<NumberField.Root.Props, "children" | "locale"> & {
/** `sm` is 28 pixels tall, `md` 32, `lg` 40. */
size?: NumberInputSize;
/** The locale that formats and reads the number. Fixed by default, so the server and the browser print the same text. */
locale?: string;
/** Shown while the field is empty. */
placeholder?: string;
/** The name a screen reader gives the plus button. */
incrementLabel?: string;
/** The name a screen reader gives the minus button. */
decrementLabel?: string;
/** Play a tick for each step from a button, an arrow key or the wheel, if the visitor has sound on. Typing is silent. */
sound?: boolean;
};
const SIZE: Record<NumberInputSize, { field: string; step: string }> = {
sm: { field: "h-7", step: "w-7" },
md: { field: "h-8", step: "w-8" },
lg: { field: "h-10", step: "w-10" },
};
const STEP =
"flex shrink-0 cursor-pointer items-center justify-center text-muted-foreground transition-[background-color,color] duration-150 ease-out motion-reduce:transition-none " +
"hover:bg-accent hover:text-accent-foreground data-disabled:cursor-not-allowed data-disabled:opacity-50 data-disabled:hover:bg-transparent data-disabled:hover:text-muted-foreground";
/** The reasons a value changed by a step, as opposed to being typed. */
const STEPPED = new Set(["increment-press", "decrement-press", "keyboard", "wheel", "scrub"]);
/**
* A field for a number, with a minus and a plus button. It keeps the value inside `min` and `max`, steps by `step`,
* and prints it with `format`. Hold a button to repeat. Name it with a `<label>` whose `htmlFor` is the `id`, or an `aria-label`.
*/
export function NumberInput({
size = "md",
locale = "en-US",
placeholder,
incrementLabel = "Increase",
decrementLabel = "Decrease",
sound = true,
className,
onValueChange,
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledBy,
"aria-describedby": ariaDescribedBy,
"aria-invalid": ariaInvalid,
...props
}: NumberInputProps) {
return (
<NumberField.Root
data-slot="number-input"
locale={locale}
onValueChange={(value, details) => {
if (sound && STEPPED.has(details.reason)) play("tick");
onValueChange?.(value, details);
}}
className={cn("w-full min-w-0", className)}
{...props}
>
<NumberField.Group
data-slot="number-input-group"
data-surface="control"
className={cn(
"flex w-full overflow-hidden rounded-md border border-input bg-background text-foreground",
"transition-[border-color] duration-150 ease-out motion-reduce:transition-none",
"has-[input:focus-visible]:outline-2 has-[input:focus-visible]:-outline-offset-1 has-[input:focus-visible]:outline-ring",
"has-[input[aria-invalid=true]]:border-destructive data-disabled:opacity-50",
SIZE[size].field,
)}
>
<NumberField.Decrement data-slot="number-input-decrement" aria-label={decrementLabel} className={cn(STEP, "border-e border-input", SIZE[size].step)}>
<MinusIcon size={16} />
</NumberField.Decrement>
<NumberField.Input
data-slot="number-input-field"
placeholder={placeholder}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledBy}
aria-describedby={ariaDescribedBy}
aria-invalid={ariaInvalid}
// 16px text on phones: anything smaller makes iOS zoom the page when the field takes focus.
className="min-w-0 flex-1 bg-transparent px-2 text-center text-base tabular-nums outline-none placeholder:text-muted-foreground disabled:cursor-not-allowed md:text-sm"
/>
<NumberField.Increment data-slot="number-input-increment" aria-label={incrementLabel} className={cn(STEP, "border-s border-input", SIZE[size].step)}>
<PlusIcon size={16} />
</NumberField.Increment>
</NumberField.Group>
</NumberField.Root>
);
}Files and dependencies
- Adds
components/ui/number-input.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Sound engine
- Icons
minusplus
Changelog
- 1.0.0 · 2026-12-03First release.