<!-- index: /slotted/llms.txt -->
# number-field

`data-ui="number-field"` · Number input with steppers and formatting.

## Import

```tsx
import { NumberField, NumberFieldDefaultsProvider, NumberFieldScrub } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';
import { NumberField } from '@slotted/react/number-field';

export default function NumberFieldBasic() {
  return (
    <Field required>
      <FieldLabel>Reorder point</FieldLabel>
      <NumberField defaultValue={25} end="units" max={500} min={0} step={5} />
      <FieldDescription>Chai is reordered when stock falls to this level.</FieldDescription>
    </Field>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |
| `input` | input[type=text][role=spinbutton] | yes |
| `start` | span | no |
| `end` | span | no |
| `scrub` | span (NumberFieldScrub) | no |
| `stepper` | span | no |
| `decrement` | button[type=button][tabindex=-1] | no |
| `increment` | button[type=button][tabindex=-1] | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `value` | `number \| null` | — | NumberField | Makes the value controlled for the life of the component. null is empty, and empty is not zero: a required amount left blank and an amount of zero are different values. |
| `defaultValue` | `number \| null` | — | NumberField | The value an uncontrolled number field starts from. It is read once, and a native form reset returns to it. |
| `min` | `number` | — | NumberField | The smallest value. The decrement button is disabled at it, Home moves to it, and the snapToStep grid is counted from it. |
| `max` | `number` | — | NumberField | The largest value. The increment button is disabled at it, and End moves to it. |
| `step` | `number` | 1 | NumberField | What the arrow keys and the two buttons add or subtract. |
| `largeStep` | `number` | step × 10 | NumberField | What Shift with an arrow, Page Up and Page Down add or subtract, so a wide range is crossed without holding a key. |
| `smallStep` | `number` | step ÷ 10 | NumberField | What Alt with an arrow adds or subtracts. |
| `stepFrom` | `number` | min, or 0 | NumberField | The value the first step produces when the number field is empty. |
| `snapToStep` | `boolean` | false | NumberField | Steps to multiples of step counted from min, so with min 2 and step 3 the values are 2, 5, 8 and 11. A typed value moves to the nearest multiple when the number field commits. |
| `clamp` | `'blur' \| 'strict' \| 'none'` | blur | NumberField, NumberFieldDefaultsProvider | What happens to a value outside min and max. blur returns it to the range when the number field commits, strict refuses the keystroke that would leave the range, and none keeps the value and sets aria-invalid and data-invalid while it is out of range. |
| `format` | `Intl.NumberFormatOptions` | — | NumberField | How the value is written and read back. Currency, percent and unit are formats of one number, and precision is set with minimumFractionDigits and maximumFractionDigits. When the number field commits, the number is rounded to the precision the input shows. |
| `formatOn` | `'change' \| 'commit'` | change | NumberField, NumberFieldDefaultsProvider | When the text takes the format. change regroups it on every keystroke; commit keeps the typed text until blur, Enter, a stepper press or an arrow key. Under both, the number changes on every keystroke that parses. |
| `locale` | `string` | the locale of SlottedProvider, then en-US | NumberField | A BCP 47 tag. It sets the decimal separator, the grouping and the digits, for the text written and the text read back. |
| `stepper` | `'stacked' \| 'split' \| 'none'` | stacked | NumberField, NumberFieldDefaultsProvider | Where the two buttons sit. split places them at either end of the frame, and its targets are at least 24 CSS pixels in both directions; none removes them. |
| `wheel` | `boolean` | false | NumberField | Lets the mouse wheel step the value while the input has focus and the pointer is over it. It is off by default, so scrolling a form never changes an amount. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | NumberField, NumberFieldDefaultsProvider | The step of the control scale. When unset, the number field takes the size of its field, then of the provider. Height, padding, type and glyphs come from the control scale the theme sets per density. |
| `variant` | `'outline' \| 'subtle' \| 'plain'` | outline | NumberField, NumberFieldDefaultsProvider | How much frame the number field draws. plain draws no border at rest and keeps the same box, for a dense table cell. |
| `start` | `ReactNode` | — | NumberField | Content before the value in the reading direction. It is never selected, copied or parsed with the number. |
| `end` | `ReactNode` | — | NumberField | Content after the value in the reading direction. |
| `NumberFieldScrub sensitivity` | `number` | 2 | NumberFieldScrub | Pixels of horizontal drag per step. Use fewer pixels for a wide range and more for a fine one. |
| `disabled` | `boolean` | from the field, or false | NumberField | Unset rather than false, so the number field follows the field around it. When set, it wins over the field and writes the native disabled, which removes the input from the tab order and from form submission. |
| `readOnly` | `boolean` | from the field, or false | NumberField | Unset rather than false, so the number field follows the field around it. Writes the native readonly: the input stays focusable, selectable and submitted, and both buttons are disabled. |
| `invalid` | `boolean` | from the field, or false | NumberField | Unset rather than false, so the number field follows the field around it. Writes aria-invalid on the input and data-invalid on the frame. |
| `required` | `boolean` | — | NumberField | The element’s own attribute, passed through unchanged; NumberField never writes it. Inside a required field, the input receives aria-required instead, because the native attribute turns on the browser’s constraint validation. |
| `name / form` | `string` | — | NumberField | Reach a hidden input beside the visible one, which submits the raw number: 1480, not "$1,480.00". |
| `messages` | `Partial<NumberFieldMessages>` | — | NumberField | Replaces words of the active language, key by key: increment and decrement are the accessible names of the two buttons. |
| `render` | `(props: NumberFieldRootProps) => ReactNode` | — | NumberField | Renders another element in place of the frame and passes it every attribute. The props are typed as HTMLAttributes<HTMLElement>, so they spread onto any element. The input and the two buttons cannot be replaced. |
| `ref` | `Ref<HTMLInputElement>` | — | NumberField | Reaches the input element. focus(), select() and setSelectionRange() are the element’s own. |
| `handle` | `Ref<NumberFieldHandle>` | — | NumberField | Reaches increment() and decrement(), for a shortcut or a button elsewhere on the page. Both move by one step within the bounds and report the reason programmatic. |
| `className / style` | `string / CSSProperties` | — | NumberField | Reach the frame. Every other native prop reaches the input. |
| `NumberFieldDefaultsProvider` | `{ size, variant, stepper, formatOn, clamp }` | — | a subtree | Sets size, variant, stepper, formatOn and clamp once for every number field under it. A number field that states a value keeps it. The locale comes from SlottedProvider, so every amount in the application is written and read the same way. |
| `useField()` | `() => FieldContextValue \| undefined` | — | inside a Field | What the number field reads to wire itself: the id, the description and the four shared states. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onValueChange` | `(value: number \| null, request: ChangeRequest<number \| null>)` | Before the value changes. request.value is the new number and request.previous the number it replaces. request.reason is keyboard for typing, an arrow, a page key, Home, End or Enter; pointer for a stepper press, a wheel turn or a scrub; blur for the commit that formats, rounds and clamps on blur; programmatic for handle.increment() and handle.decrement(). request.source is the originating event, and request.cancel() refuses the change before it is applied. A request for the number NumberField already holds is not emitted. |
| `onChange, onBlur, onKeyDown, onFocus` | `the element’s own events` | Whenever the input fires them. NumberField composes your handler with its own instead of replacing it: yours runs first and always runs, and calling preventDefault() on a key stops NumberField from acting on it. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `variant` | `outline`, `subtle`, `plain` | `outline` |
| `stepper` | `stacked`, `split`, `none` | `stacked` |
| `clamp` | `blur`, `strict`, `none` | `blur` |
| `formatOn` | `change`, `commit` | `change` |

## Rules

- NumberField follows the WAI-ARIA APG Spinbutton pattern: role="spinbutton" on the input with aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext, ArrowUp and ArrowDown to step, Home and End to reach the bounds, and Page Up and Page Down for the larger step.
- aria-valuetext carries the formatted text, so a screen reader announces "$1,480.00" rather than 1480. When the number field is empty, neither aria-valuenow nor aria-valuetext is set: no value is not the value zero.
- The frame has no role. WAI-ARIA requires the children of a spinbutton to be non-interactive, so the two buttons are siblings of the input inside a box that announces nothing.
- The buttons are out of the tab order, as the APG pattern recommends, and stay in the accessibility tree with translatable names and aria-controls pointing at the input. A form of twenty amounts keeps twenty tab stops instead of sixty. A button at a bound is disabled, so its name is still announced and its appearance shows why it does nothing.
- Inside a required field the input receives aria-required, never the native required, which would turn on the browser’s constraint validation. disabled and readonly are the native attributes.
- The input is type="text", and its inputMode comes from the props: numeric for a whole number at or above zero, decimal when the format allows a fraction, and text when min allows a negative, because neither numeric keypad offers a minus sign on every platform.
- The mouse wheel is not bound unless wheel is set, and Escape is not bound at all, so scrolling a form never changes an amount.
- The invalid state is never conveyed by color alone: aria-invalid announces it and the field’s error states it in words. In the high-contrast scheme the border does not carry it.

## Tokens

`--slotted-border-subtle`, `--slotted-control-border-color`, `--slotted-control-border-width`, `--slotted-control-focus-ring-offset`, `--slotted-control-font-family`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-lg`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-inner-size-lg`, `--slotted-control-inner-size-md`, `--slotted-control-inner-size-sm`, `--slotted-control-invalid-edge-width`, `--slotted-control-padding-inline-lg`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-radius`, `--slotted-control-shadow`, `--slotted-control-shadow-focus-geometry`, `--slotted-control-surface`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-background`, `--slotted-disabled-border`, `--slotted-disabled-foreground`, `--slotted-focus-ring-color`, `--slotted-focus-ring-color-invalid`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-number-field-action-size`, `--slotted-number-field-adornment-color`, `--slotted-number-field-background`, `--slotted-number-field-border-color`, `--slotted-number-field-color`, `--slotted-number-field-disabled-background`, `--slotted-number-field-disabled-border-color`, `--slotted-number-field-disabled-color`, `--slotted-number-field-hover-border-color`, `--slotted-number-field-icon-size-min`, `--slotted-number-field-invalid-border-color`, `--slotted-number-field-invalid-edge-width`, `--slotted-number-field-invalid-focus-ring-color`, `--slotted-number-field-placeholder-color`, `--slotted-number-field-plain-hover-background`, `--slotted-number-field-radius`, `--slotted-number-field-readonly-background`, `--slotted-number-field-readonly-border-color`, `--slotted-number-field-scrub-active-color`, `--slotted-number-field-shadow`, `--slotted-number-field-stepper-active-background`, `--slotted-number-field-stepper-color`, `--slotted-number-field-stepper-disabled-background`, `--slotted-number-field-stepper-disabled-color`, `--slotted-number-field-stepper-hover-background`, `--slotted-number-field-stepper-size`, `--slotted-number-field-subtle-background`, `--slotted-number-field-subtle-border-color`, `--slotted-number-field-subtle-hover-background`, `--slotted-radius-none`, `--slotted-radius-xs`, `--slotted-space-1`, `--slotted-space-2`, `--slotted-surface-sunken`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-border`, `--slotted-tone-danger-border`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`

## Examples

See [number-field.examples.md](./number-field.examples.md).
