<!-- generated by scripts/gen-docs.mjs from docs/system/number-field.md. Edit the source and rerun. -->
# NumberField

## Description
Takes a count or an amount that a person nudges more often than types, such as guests, nights or a size threshold, with a minus and a plus beside the figure.

`import { NumberField, numberFieldLabels } from "@/components/cobalt/ui/number-field"`, source `registry/cobalt/ui/number-field.tsx`, status `ready`.
Foundation: `Base UI NumberField`. Traps checked: `trap/field-label`, `trap/label-unbound`, `trap/field-error-link`, `trap/field-input-type`, `trap/field-affix-focus`, `trap/touch-input-zoom`, `trap/control-height`, `trap/focus-ring-shape`, `trap/hover-beats-focus`, `trap/disabled-still-hovers`, `trap/numbers-shift`, `trap/button-icon-name`.

Where each trap is answered: `trap/field-label` and `trap/label-unbound` through `rule/input-has-a-label`, Base UI's input registers with `Field`, so `FieldLabel` names it with no id written (measured on the showcase); `trap/field-error-link` through `rule/input-error-is-linked`, a `FieldError` sets `aria-invalid` and joins `aria-describedby`; `trap/field-input-type`, Base UI sets `inputMode` to numeric or decimal from the format; `trap/field-affix-focus`, the steppers sit inside the field's box and the box draws the focus edge; `trap/touch-input-zoom` in States, 16px text below 768px; `trap/control-height` in Tokens, 36px; `trap/focus-ring-shape`, the edge is a box-shadow on the box's 10px corner (D40); `trap/hover-beats-focus` and `trap/disabled-still-hovers`, the box has no hover look and the steppers drop their pointer events when disabled; `trap/numbers-shift`, the figure is tabular and centred; `trap/button-icon-name`, the steppers are named "Decrease" and "Increase" through `labels`.

### Foundation
| Given by the foundation | Added by the team |
|---|---|
| Base UI `NumberField`: the root, group, input, decrement and increment; a held press that repeats; ArrowUp and ArrowDown by `step`, Shift by `largeStep`, Alt by `smallStep`; Home and End to `min` and `max`; `min`, `max` and the steppers disabled at either end; parsing and formatting through `Intl` with `format` and `locale`; `Field` registration, so the label, the description, the error and `disabled` reach it; the steppers out of the tab order, named "Decrease" and "Increase" | The field's box from `fieldBoxClasses` (the fill in every state, the accent edge after Tab, the red wash when invalid, D40); the minus at the start and the plus at the end as ghost `icon-xs` buttons, 28px with a 6px corner 4px in from the 36px box's 10px corner so the shapes are concentric (D38), each reaching 44px under a coarse pointer; the figure centred in tabular figures and written left to right on a right-to-left page; `locale` defaulting to en-US like `registry/cobalt/lib/format.ts`; no scrub area |

## Examples
Guests and nights for a booking, inside `Field`s; the minus is disabled at the room's minimum.

Real uses, 0 call sites (`rg -n "<NumberField\b" app registry --glob '!registry/cobalt/ui/*'`): new in this run; booking forms and the screener's thresholds are where it goes.

### Example files
| File | Covers | Caption |
|---|---|---|
| `docs/system/examples/number-field/default.tsx` | default | Guests and nights for a booking, held to the room's limits |
| `docs/system/examples/number-field/format-money.tsx` | state:formatted | A minimum for gross assets in compact dollars, in steps of $100M |
| `docs/system/examples/number-field/locale.tsx` | state:localized | With locale set to German the rate reads 145,00 €, and a comma in a typed figure is the decimal mark |
| `docs/system/examples/number-field/state-invalid.tsx` | state:invalid | More guests than the room sleeps turns the field's fill to the error wash, with the reason under it |
| `docs/system/examples/number-field/state-disabled.tsx` | state:disabled | Nights fixed by the package rate |
| Not applicable: default.tsx starts at the minimum guests once stepped down | state:at-limit | The minus disabled at `min` |
| Not applicable: reached with Tab on any example | state:focus-visible | The field's accent edge and ring |
| `docs/system/examples/number-field/default.tsx` | composition:Field | The label names the input and the description describes it |

Example: Guests and nights for a booking, held to the room's limits (`docs/system/examples/number-field/default.tsx`)

```tsx
import { useState } from "react";

import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { NumberField } from "@/components/cobalt/ui/number-field";

export default function Example() {
  const [guests, setGuests] = useState<number | null>(2);
  return (
    // Two steppers fit side by side even on a phone, so they never stack into two wide bars.
    <div className="grid w-full max-w-sm grid-cols-2 gap-4">
      <Field>
        <FieldLabel>Guests</FieldLabel>
        <NumberField value={guests} onValueChange={setGuests} min={1} max={4} name="guests" />
        <FieldDescription>The Harbour suite sleeps 4.</FieldDescription>
      </Field>
      <Field>
        <FieldLabel>Nights</FieldLabel>
        <NumberField defaultValue={3} min={1} max={28} name="nights" />
      </Field>
    </div>
  );
}
```

Example: The label names the input and the description describes it (`docs/system/examples/number-field/default.tsx`)

```tsx
import { useState } from "react";

import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { NumberField } from "@/components/cobalt/ui/number-field";

export default function Example() {
  const [guests, setGuests] = useState<number | null>(2);
  return (
    // Two steppers fit side by side even on a phone, so they never stack into two wide bars.
    <div className="grid w-full max-w-sm grid-cols-2 gap-4">
      <Field>
        <FieldLabel>Guests</FieldLabel>
        <NumberField value={guests} onValueChange={setGuests} min={1} max={4} name="guests" />
        <FieldDescription>The Harbour suite sleeps 4.</FieldDescription>
      </Field>
      <Field>
        <FieldLabel>Nights</FieldLabel>
        <NumberField defaultValue={3} min={1} max={28} name="nights" />
      </Field>
    </div>
  );
}
```

## Variants
Not applicable: NumberField has one look. `format` and `locale` change how the figure is written, shown in format-money.tsx and locale.tsx.

## States
| State | Trigger | What the user can do | Shown by, besides color | Checked by |
|---|---|---|---|---|
| idle | a value or none | type, step with the buttons or the arrows | the figure, or the placeholder in the muted ink | test |
| at-limit | the value equals `min` or `max` | step the other way | the stepper toward the limit disabled at half opacity | by hand |
| formatted | `format` | type a plain number; the field writes it formatted on blur | "$1B" through `Intl` | test |
| focus-visible | Tab to the input | type, step | the 1px `--primary` edge and the 3px ring at 20 percent on the box; a click shows only the caret (D40) | screenshot |
| invalid | a `FieldError` in its `Field`, or `aria-invalid` | correct the value | the red wash on the box, `aria-invalid`, the error linked | by hand |
| disabled | `disabled`, or from `Field` | nothing | half opacity on the box, the steppers disabled | by hand |
| phone | below 768px | the same | 16px text so iOS does not zoom | screenshot |

Example: A minimum for gross assets in compact dollars, in steps of $100M (`docs/system/examples/number-field/format-money.tsx`)

```tsx
import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { NumberField } from "@/components/cobalt/ui/number-field";

export default function Example() {
  return (
    <Field className="w-full max-w-60">
      <FieldLabel>Gross assets, at least</FieldLabel>
      <NumberField
        defaultValue={1_000_000_000}
        min={0}
        step={100_000_000}
        largeStep={1_000_000_000}
        format={{ style: "currency", currency: "USD", notation: "compact", maximumFractionDigits: 1 }}
      />
      <FieldDescription>Shift with an arrow key steps by $1B.</FieldDescription>
    </Field>
  );
}
```

Example: With locale set to German the rate reads 145,00 €, and a comma in a typed figure is the decimal mark (`docs/system/examples/number-field/locale.tsx`)

```tsx
import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { NumberField } from "@/components/cobalt/ui/number-field";

export default function Example() {
  return (
    <Field className="w-full max-w-60">
      <FieldLabel>Rate per night</FieldLabel>
      <NumberField
        defaultValue={145}
        min={0}
        step={5}
        locale="de-DE"
        format={{ style: "currency", currency: "EUR" }}
      />
      <FieldDescription>Charged in euros at check-in.</FieldDescription>
    </Field>
  );
}
```

Example: More guests than the room sleeps turns the field's fill to the error wash, with the reason under it (`docs/system/examples/number-field/state-invalid.tsx`)

```tsx
import { useState } from "react";

import { Field, FieldError, FieldLabel } from "@/components/cobalt/ui/field";
import { NumberField } from "@/components/cobalt/ui/number-field";

export default function Example() {
  // Typed values may pass the room's limit, so the field can say why the booking cannot go on.
  const [guests, setGuests] = useState<number | null>(6);
  return (
    <Field className="w-full max-w-xs">
      <FieldLabel>Guests</FieldLabel>
      <NumberField value={guests} onValueChange={setGuests} min={1} max={4} allowOutOfRange className="max-w-48" />
      <FieldError reserve>{guests !== null && guests > 4 ? "The Garden double sleeps 4. Book a second room." : ""}</FieldError>
    </Field>
  );
}
```

Example: Nights fixed by the package rate (`docs/system/examples/number-field/state-disabled.tsx`)

```tsx
import { Field, FieldDescription, FieldLabel } from "@/components/cobalt/ui/field";
import { NumberField } from "@/components/cobalt/ui/number-field";

export default function Example() {
  return (
    <Field disabled className="w-full max-w-48">
      <FieldLabel>Nights</FieldLabel>
      <NumberField defaultValue={3} min={1} />
      <FieldDescription>The weekend package is 3 nights.</FieldDescription>
    </Field>
  );
}
```

### State precedence
- Disabled and invalid: disabled wins. The wash fades with the box.
- Invalid and at-limit: both show. A typed value past `max` with `allowOutOfRange` keeps the wash and the plus stays disabled.
- Focus-visible and invalid: both show. The edge turns red.
- Formatted and focus-visible: both show. The figure keeps its format while focused.

### Motion
| Trigger | Kind | Preset | Properties | Reduced motion |
|---|---|---|---|---|
| Tab focus arrives | input | `duration-press` | box-shadow | the edge appears at once |
| a stepper pressed | input | `pressable`, `--t-press` | box-shadow, background-color | the same colour change |

## Props

<!-- Props generated from registry/cobalt/ui/number-field.tsx with TypeScript by scripts/props-table.mjs -->
| Prop | Type | Default | Purpose |
|---|---|---|---|
| `aria-label?` | `string` |  | Defines a string value that labels the current element. |
| `className?` | `string` |  |  |
| `labels?` | `Partial<{ decrease: string; increase: string; }>` |  |  |
| `placeholder?` | `string` |  |  |

Also accepts every native `<div>` attribute; from Base UI: `allowOutOfRange`, `allowWheelScrub`, `defaultValue`, `disabled`, `form`, `format`, `id`, `inputRef`, `largeStep`, `locale`, `max`, `min`, `name`, `onValueChange`, `onValueCommitted`, `readOnly`, `required`, `smallStep`, `snapOnStep`, `step`, `value`.

Every Base UI `NumberField.Root` prop passes through: `value`, `defaultValue` and `onValueChange` (a number or null), `min`, `max`, `step`, `largeStep`, `smallStep`, `format`, `locale` (en-US by default), `allowOutOfRange`, `name`, `required`, `disabled`, `readOnly`. `aria-label` and `placeholder` go to the input. `labels` renames the steppers. `className` is for placement.

## Usage

### When to use
- A small count a person changes by one or two: guests, rooms, nights, seats.
- A threshold with a natural step: gross assets of at least $1B in steps of $100M.

### When not to use
- A figure with no natural step, such as a price typed from an invoice. Use Input instead, with `inputMode="decimal"`.
- A choice of up to 6 fixed values, such as 1, 2 or 3 bedrooms. Use SegmentedControl instead.
- A range with two ends, such as 2 to 8 nights. Use two NumberFields in one Field row instead, or Select for set bands.

### Rules
- Follows `rule/input-has-a-label`.
- Follows `rule/input-error-is-linked`.
- `rule/number-field-bound-it`: When a count has a floor or a ceiling, pass `min` and `max` instead of checking after the change, because the steppers then stop at the limit and say so by turning off. Evidence: app 3/4 examples bound the value, `docs/system/examples/number-field/default.tsx:14`; principle heuristic: error prevention. Check: review.
  - Don't: `onValueChange={(n) => n !== null && n > 4 && setError("Too many guests")}`
  - Do: `<NumberField min={1} max={4} />`
- `rule/number-field-format-not-units-in-the-label`: When the value is an amount, pass `format` with the currency or unit instead of writing "($)" in the label or typing the symbol, because `Intl` places the symbol by locale and the field parses it back. Evidence: app 2/2 amounts in the examples pass `format` (`docs/system/examples/number-field/format-money.tsx:15`, `docs/system/examples/number-field/locale.tsx:15`); person D10. Check: review.
  - Don't: `<FieldLabel>Gross assets ($)</FieldLabel><NumberField />`
  - Do: `<NumberField format={{ style: "currency", currency: "USD", notation: "compact" }} />`

### Content
- Follows `rule/writing-field-label-noun`.

### Anti-slop
- `rule/number-field-not-input-number`: When a field needs steppers, use `NumberField` instead of `<Input type="number">`, because the native spinner is a pair of 10px arrows drawn by the browser, it ignores the tokens, and it scrolls the value under a wheel. Evidence: principle platform: the native number spinner is browser chrome; measured the system steppers at 28px, `.design-system/evidence/number-field/measure.json`. Check: review of every `type="number"`.
  - Don't: `<Input type="number" min={1} max={4} />`
  - Do: `<NumberField min={1} max={4} />`

### Limits
- `rule/number-field-width`: When the figure can pass 12 characters, such as €1.284.000.000.000, give the field at least 240px, `max-w-60`, instead of a narrower box, because the steppers and the box's padding take 64px and a 36px field at 160px leaves 96px, under the figure's width at 14px. Evidence: measured the input at 238px in a 310px box, `.design-system/evidence/number-field/measure.json`. Check: probe /stress/number-field?data=worst.
  - Don't: `<div className="w-32"><NumberField format={{ style: "currency", currency: "EUR" }} /></div>`
  - Do: `<Field className="w-full max-w-60"><NumberField format={{ style: "currency", currency: "EUR" }} /></Field>`

## Accessibility
Rests on Base UI `NumberField`; the input is a native `<input>` with `role` none of its own and `inputMode` numeric.

### Keyboard
| Key | Where focus is | Effect | Focus after | Checked by |
|---|---|---|---|---|
| Tab | before the field | focuses the input; the steppers are skipped | the input | by hand |
| ArrowUp or ArrowDown | the input | adds or takes one `step`; Shift a `largeStep`, Alt a `smallStep` | the input | by hand |
| Home or End | the input | goes to `min` or `max` | the input | by hand |
| digits | the input | types a value, formatted on blur | the input | by hand |

Measured at 1440 and 390: three ArrowUp presses took Guests from 1 to 4 and disabled the plus; Home went back to 1 and disabled the minus.

### ARIA
| Part | Role | Accessible name from | States and properties | Announced | Checked by |
|---|---|---|---|---|---|
| group | group | none | none | nothing | test |
| input | textbox | the `FieldLabel`, or `aria-label` | `aria-describedby` from the description and the error, `aria-invalid`, `disabled` | the name, the value and the description | test |
| decrease | button | "Decrease" from `labels` | `disabled` at `min`, `aria-controls` the input, out of the tab order | "Decrease" | test |
| increase | button | "Increase" from `labels` | `disabled` at `max`, `aria-controls` the input | "Increase" | test |

## Tokens
| Part | State | Token |
|---|---|---|
| box | all | `--control`, `--r-lg`, `--input` at 50 percent |
| box | focus-visible | `--primary` 1px inset and 3px at 20 percent, `--t-press` |
| box | invalid | `--destructive` at 10 percent |
| stepper | rest | `--muted-foreground`, `--r-sm` |
| stepper | hover | `--muted`, `--foreground` |

Measured at 390 (`.design-system/evidence/number-field/measure.json`): the box 36px tall with a 10px corner, each stepper 28px with a 6px corner, 4px in from either end, the figure in 16px text.

## Related
- Input: a figure with no step, or a field with a glyph or a button at one end.
- Field: the label, the description and the error around it.
- SegmentedControl: one of a few fixed values.
- InlineEdit: a value edited where it is read, saved on Enter or when the field is left.
- Slider: a value or a range picked along a track, with its bounds typed if needed.
