Number Field
A number typed in a box, or stepped with a minus, a plus and the arrow keys.
Installation
pnpm dlx @human-kit/design add number-fieldnpx @human-kit/design add number-fieldyarn dlx @human-kit/design add number-fieldbunx @human-kit/design add number-fieldThe command copies the files to src/lib/components/ui/number-field. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as NumberField from '$lib/components/ui/number-field';
let cycles = $state<number | null>(3);
</script>
<NumberField.Root aria-label="Cycles" bind:value={cycles} min={0} max={12} /> Examples
In a field
Spread a Field.Root's control on it, as on an input: the label names it, the hint is read with it, and min and max keep it in range.
One or two.
Currency and percent
formatOptions takes the options of Intl.NumberFormat. The format only draws the number: the value stays 350000, and a percent stays 10 for 10 %, not 0.1.
Guidelines
- Use it for a count or a quantity that is stepped: embryos, cycles, doses. For a code that only looks like a number, a document or a phone, use an
Input.Root. - Set
minandmaxwhenever the number has a range. A value typed past them is brought back to the edge when the field is left. - Turn off
stepperswhere the number is mostly typed and large, a price or an amount, and the buttons would only step it by one. - Use the same variant and size as the inputs beside it: the two families share their boxes, so a form lines up.
- Write the unit in
formatOptions(style: 'percent',style: 'unit') rather than in the label, so it stays next to the number.
Accessibility
- The input is a
role="spinbutton"witharia-valuenow,aria-valueminandaria-valuemax, andaria-valuetextwith the formatted number. - Up and Down step it, Shift with them by
largeStep, Ctrl or Cmd bysmallStep. Page Up and Page Down step bylargeStep, Home and End jump tominandmax. - The minus and the plus take no tab stop, since the arrows do the same. They are named "Decrement" and "Increment" in the locale, or by
decrementAriaLabelandincrementAriaLabel. - It needs a name: a
<label for>with the sameid, aField.Root, oraria-label. - A value that cannot be read, or one out of range, marks the input invalid, so a form does not submit it.
API reference
| Prop | Type | Default |
|---|---|---|
variant | 'default' | 'filled' | 'ghost' | 'underline' The box around the number, the same as the input's. | 'default' |
size | 'sm' | 'default' | 'lg' The height. The number keeps the body size at | 'default' |
value | number | null The number, | — |
defaultValue | number | null The number at the start, when you give no | null |
onChange | (value: number | null) => void Called when the number changes. | — |
min | number The smallest number it takes. | — |
max | number The largest number it takes. | — |
step | number | 'any' How much a press of a stepper or an arrow adds. | 1 |
smallStep | number The step with Ctrl or Cmd held. | 0.1 |
largeStep | number The step with Shift held, and with Page Up and Page Down. | 10 |
formatOptions | Intl.NumberFormatOptions How the number is drawn: decimals, currency, percent, unit. | — |
steppers | boolean The minus and the plus. Leave them out where the number is mostly typed. | true |
snapOnStep | boolean Rounds a stepped number to a multiple of | false |
allowOutOfRange | boolean Takes a number past | false |
allowWheelScrub | boolean The mouse wheel steps the number while the field has the focus and the pointer is on it. | false |
invalid | boolean Marks the number as wrong: a red ring and | false |
required | boolean The field has to be filled. | false |
readonly | boolean Shows the number in a grey box, without steppers. It can be focused, not changed. | false |
disabled | boolean Turns the field off. | false |
name | string Submits the raw number in a form, through a hidden input. | — |
id | string Goes on the input, so a | — |
placeholder | string The text while empty. | — |
incrementAriaLabel | string The name of the plus. Localized by default. | — |
decrementAriaLabel | string The name of the minus. Localized by default. | — |
Plus every attribute of a <div>, on the root. class goes on the box.