Skip to the content
human-kit design
09 — Form

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-field
npx @human-kit/design add number-field
yarn dlx @human-kit/design add number-field
bunx @human-kit/design add number-field

The 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 min and max whenever the number has a range. A value typed past them is brought back to the edge when the field is left.
  • Turn off steppers where 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" with aria-valuenow, aria-valuemin and aria-valuemax, and aria-valuetext with the formatted number.
  • Up and Down step it, Shift with them by largeStep, Ctrl or Cmd by smallStep. Page Up and Page Down step by largeStep, Home and End jump to min and max.
  • 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 decrementAriaLabel and incrementAriaLabel.
  • It needs a name: a <label for> with the same id, a Field.Root, or aria-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

PropTypeDefault
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 lg.

'default'
valuenumber | null

The number, null while empty. Bindable.

—
defaultValuenumber | null

The number at the start, when you give no value.

null
onChange(value: number | null) => void

Called when the number changes.

—
minnumber

The smallest number it takes.

—
maxnumber

The largest number it takes.

—
stepnumber | 'any'

How much a press of a stepper or an arrow adds.

1
smallStepnumber

The step with Ctrl or Cmd held.

0.1
largeStepnumber

The step with Shift held, and with Page Up and Page Down.

10
formatOptionsIntl.NumberFormatOptions

How the number is drawn: decimals, currency, percent, unit.

—
steppersboolean

The minus and the plus. Leave them out where the number is mostly typed.

true
snapOnStepboolean

Rounds a stepped number to a multiple of step.

false
allowOutOfRangeboolean

Takes a number past min or max as it is, instead of bringing it back to the edge.

false
allowWheelScrubboolean

The mouse wheel steps the number while the field has the focus and the pointer is on it.

false
invalidboolean

Marks the number as wrong: a red ring and aria-invalid.

false
requiredboolean

The field has to be filled.

false
readonlyboolean

Shows the number in a grey box, without steppers. It can be focused, not changed.

false
disabledboolean

Turns the field off.

false
namestring

Submits the raw number in a form, through a hidden input.

—
idstring

Goes on the input, so a <label for> or a Field.Root names it.

—
placeholderstring

The text while empty.

—
incrementAriaLabelstring

The name of the plus. Localized by default.

—
decrementAriaLabelstring

The name of the minus. Localized by default.

—

Plus every attribute of a <div>, on the root. class goes on the box.