Skip to content
Slotted

Number Field

Takes a number in a text input that writes it in the reader's locale, with stepper buttons, bounds and keyboard stepping.

units

Chai is reordered when stock falls to this level.

Installation

The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { NumberField, NumberFieldDefaultsProvider, NumberFieldScrub } from '@slotted/react';

Sizes

size sets the height, the padding, the type and the stepper glyphs to one step of the control scale: sm for dense tables and toolbars, md for most forms, lg for sparse forms. Inside a field, a number field that states no size takes the field’s.

Variants

variant sets how much frame the control draws. outline, the default, draws a border on the ground it sits on. subtle fills the control with a sunken ground. plain draws no border and no ground at rest and keeps the same box, so a table cell that holds one does not change size under the pointer; it pairs with stepper="none" for an amount that is typed and never spun.

Steppers

stepper places the two buttons. stacked, the default, puts increment over decrement in one column at the inline end, which is what a form expects. split puts decrement before the value and increment after it, the arrangement of a quantity picker and the one whose targets are at least 24 pixels in both directions. none removes them. Held down, a button steps once, waits, and then repeats. Neither button is in the tab order: the arrow keys in the input do the same.

Bounds and step

min and max bound the value, and the button that would leave the range is disabled at the bound. Home and End move to min and max. step is what one press or one arrow key adds, 1 by default. With snapToStep, stepping lands on multiples of step counted from min, and a typed value goes to the nearest one when the field commits: type 20 into the second field and leave it.

A 53-foot trailer holds 26 pallets.

Sold in cases of 6. Type 20 and leave the field.

Keyboard and wheel

ArrowUp and ArrowDown move by step. Shift with an arrow, Page Up and Page Down move by largeStep, which defaults to ten steps. Alt with an arrow moves by smallStep, which defaults to a tenth of a step. wheel 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. Escape is not bound.

Arrows: 1 kg. Shift or Page keys: 10 kg. Alt: 0.1 kg.

With the field focused, the mouse wheel steps the value.

Out of range

clamp decides what happens to a typed value outside min and max. blur, the default, accepts any keystroke and returns the value to the range when the field commits, on blur or Enter. strict refuses the keystroke that would leave the range. none keeps the value and sets aria-invalid on the input and data-invalid on the frame while it is out of range, for a form that shows an error instead of changing the reader’s number.

%

Type 40 and leave the field: it returns to 25.

%

A keystroke that would pass 25 is refused.

%

40 stays, and the field is marked invalid.

Currency, percent and units

format takes Intl.NumberFormatOptions, so currency, percent and unit are ways of writing one number rather than separate components. The value stays a plain number: the credit limit below holds 25000 and the sales tax 0.0825. The input reads back what it writes, so a pasted $25,000.00 parses.

Decimals

Precision is set in format with minimumFractionDigits and maximumFractionDigits. The text is rounded and padded when the field commits, never while you type, and the committed number is the one the input shows. With maximumFractionDigits: 0 the input refuses the decimal separator. The on-screen keyboard follows the props: numeric for a whole number at or above zero, decimal when the format allows a fraction, and the full keyboard when min allows a negative.

Four decimals: 1.1 becomes 1.1000 on blur.

Whole cartons: the decimal separator is refused.

Locale

locale takes a BCP 47 tag and sets the decimal separator, the grouping and the digits the input writes and reads. Without it, the number field uses the locale of SlottedProvider in React and of provideSlotted in Angular, then en-US. Set the locale once for the application unless one amount is written differently from the rest.

When the text is formatted

formatOn="change", the default, regroups the text on every keystroke and keeps the caret on the character it was on, so a long number is readable while you type it. formatOn="commit" leaves your text as typed until blur, Enter, a stepper press or an arrow key, for an amount that is pasted and checked rather than typed. Under both, the number itself changes on every keystroke that parses.

Type 1250000: the grouping appears with each digit.

Your text stays as typed until Enter or blur.

Adornments

start and end hold content before and after the value in the reading direction: a currency mark, a unit or a code. They sit beside the number and are never selected, copied or parsed with it. A symbol that belongs inside the value, such as the $ a currency format writes, comes from format instead.

$USD
days

Scrubbing

NumberFieldScrub in React and slNumberFieldScrub in Angular, placed in start or end, moves the value when it is dragged sideways: one step per sensitivity pixels, 2 by default, largeStep with Shift and smallStep with Alt. Dragging towards the inline end steps up, so a right-to-left document steps up towards the left. A press moves focus to the input. The mark is hidden from assistive technology, because the arrow keys do the same.

mm
mm

States

A number field inside a field reads invalid, readOnly and disabled from it. invalid sets aria-invalid on the input and draws the frame in the danger tone. readOnly sets the native readonly: the value keeps full contrast, stays focusable and is still submitted, and both buttons are disabled. disabled sets the native disabled, which takes the input out of the tab order and out of form submission. A state written on the number field itself wins over the field’s.

Error:Only 39 units of Chai are in stock.

Counted from open purchase orders.

Released when the order ships.

Controlled value and empty

value with onValueChange in React, and [value] with (valueChange) in Angular, make the number yours. null is empty, and empty is not zero: an empty field sets no aria-valuenow, and the first press of a stepper starts from stepFrom, which defaults to min and then to 0. Remove the limit below, then press increment.

Orders up to $5,000.00

Stepping from your own code

handle in React reaches increment() and decrement(); in Angular, export the input as slNumberFieldInput and call the same methods from the template. Both move by one step, respect the bounds and report the reason programmatic. Use them for a shortcut or a button elsewhere on the page.

Forms and validation

name writes the raw number to a hidden input beside the visible one, so the form submits 12 rather than formatted text. Validate when the form is submitted, then set invalid on the field and write the error. The React example owns the value through value and onValueChange; the Angular example binds the input to a reactive form control, because slNumberFieldInput is a ControlValueAccessor that hands the form the number. Enter commits the value and submits the form. Submit the empty form, then a quantity.

39 units in stock.

Error:

Defaults for a section

NumberFieldDefaultsProvider in React and provideNumberFieldDefaults in Angular set size, variant, stepper, formatOn and clamp once for every number field under them. A number field that states a value of its own keeps it, and inside a field the field’s size wins over the provider’s.

Order lines

A quantity per line, with stepper="split" at size="sm", max set to the stock on hand and the line total computed from the controlled value. An emptied quantity counts as zero in the total.

Queso Cabrales$14.00 each · 22 in stock
$168.00
Singaporean Hokkien Fried Mee$9.80 each · 26 in stock
$98.00
Mozzarella di Giovanni$34.80 each · 14 in stock
$174.00
Order total$440.00

Price editor

Two controlled inputs in currency and percent, and a read-only result computed from them with stepper="none". The markup holds 0.5 and shows 50%; Shift with an arrow moves it by ten points through largeStep.

Unit cost plus markup.

Accessibility

KeyEffectFocus
ArrowUpstep—
ArrowDownstep—
Shift+ArrowUpstep—
Shift+ArrowDownstep—
Alt+ArrowUpstep—
Alt+ArrowDownstep—
PageUpstep—
PageDownstep—
Homemin—
Endmax—
Entercommit—
  • 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.

API

NumberField

Props

valuenumber | null
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.
defaultValuenumber | null
The value an uncontrolled number field starts from. It is read once, and a native form reset returns to it.
minnumber
The smallest value. The decrement button is disabled at it, Home moves to it, and the snapToStep grid is counted from it.
maxnumber
The largest value. The increment button is disabled at it, and End moves to it.
stepnumberDefault 1
What the arrow keys and the two buttons add or subtract.
largeStepnumberDefault step × 10
What Shift with an arrow, Page Up and Page Down add or subtract, so a wide range is crossed without holding a key.
smallStepnumberDefault step ÷ 10
What Alt with an arrow adds or subtracts.
stepFromnumberDefault min, or 0
The value the first step produces when the number field is empty.
snapToStepbooleanDefault false
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'Default blur
On NumberField, NumberFieldDefaultsProviderWhat 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.
formatIntl.NumberFormatOptions
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'Default change
On NumberField, NumberFieldDefaultsProviderWhen 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.
localestringDefault the locale of SlottedProvider, then en-US
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'Default stacked
On NumberField, NumberFieldDefaultsProviderWhere 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.
wheelbooleanDefault false
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'Default md
On NumberField, NumberFieldDefaultsProviderThe 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'Default outline
On NumberField, NumberFieldDefaultsProviderHow much frame the number field draws. plain draws no border at rest and keeps the same box, for a dense table cell.
startReactNode
Content before the value in the reading direction. It is never selected, copied or parsed with the number.
endReactNode
Content after the value in the reading direction.
NumberFieldScrub sensitivitynumberDefault 2
On NumberFieldScrubPixels of horizontal drag per step. Use fewer pixels for a wide range and more for a fine one.
disabledbooleanDefault from the field, or false
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.
readOnlybooleanDefault from the field, or false
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.
invalidbooleanDefault from the field, or false
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.
requiredboolean
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 / formstring
Reach a hidden input beside the visible one, which submits the raw number: 1480, not "$1,480.00".
messagesPartial<NumberFieldMessages>
Replaces words of the active language, key by key: increment and decrement are the accessible names of the two buttons.

Events

NamePayloadWhen
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, onFocusthe element’s own eventsWhenever 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.

Composition

render(props: NumberFieldRootProps) => ReactNode
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.
refRef<HTMLInputElement>
Reaches the input element. focus(), select() and setSelectionRange() are the element’s own.
handleRef<NumberFieldHandle>
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 / stylestring / CSSProperties
Reach the frame. Every other native prop reaches the input.
NumberFieldDefaultsProvider{ size, variant, stepper, formatOn, clamp }
On a subtreeSets 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
On inside a FieldWhat the number field reads to wire itself: the id, the description and the four shared states.

Parts

PartElementRequiredPurpose
rootdiv, or the element render returnsYesThe frame. It carries data-ui="number-field", data-size, data-variant and data-stepper, and data-disabled, data-invalid, data-readonly and data-scrubbing while they apply. It has no role: a spinbutton may hold only non-interactive children, so the two buttons are siblings of the input inside a box that announces nothing. className and style reach this element.
inputinput[type=text][role=spinbutton]YesThe control. It holds the formatted text, the id, and aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext. It is type="text" because type="number" empties itself whenever the text is not a plain decimal number, so no grouping separator or currency symbol would survive it. ref reaches this element.
startspanNoContent before the value in the reading direction. It sits beside the number and is never selected, copied or parsed with it. A symbol that belongs inside the value comes from format instead.
endspanNoContent after the value in the reading direction, such as a unit or a currency code.
scrubspan (NumberFieldScrub)NoA mark in start or end that moves the value when it is dragged sideways: one step per sensitivity pixels towards the inline end, largeStep with Shift and smallStep with Alt. The mark and the frame carry data-scrubbing while it is dragged. It is hidden from assistive technology, because the arrow keys in the input make the same change.
stepperspanNoThe column that holds both buttons under stepper="stacked", inset from the frame and drawn without a border of its own; each button takes the inset back as hit area. It is not rendered under split, where the buttons are direct children of the frame.
decrementbutton[type=button][tabindex=-1]NoSteps down by step, by largeStep with Shift and by smallStep with Alt, and repeats while it is held. It is disabled at min, and it is out of the tab order because the arrow keys in the input make the same change.
incrementbutton[type=button][tabindex=-1]NoSteps up by the same amounts and is disabled at max. Both buttons carry a translatable accessible name and aria-controls pointing at the input.

Theming

Each token is a CSS custom property: set it in a theme, or on any element to retune every number field inside it.

Number Field tokens

TokenDefault
--slotted-number-field-action-sizemin(calc(var(--_icon-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size))
--slotted-number-field-adornment-colorvar(--slotted-text-muted, GrayText)
--slotted-number-field-backgroundvar(--slotted-control-surface, Field)
--slotted-number-field-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-number-field-colorvar(--slotted-text, CanvasText)
--slotted-number-field-disabled-backgroundvar(--slotted-disabled-background)
--slotted-number-field-disabled-border-colorvar(--slotted-disabled-border)
--slotted-number-field-disabled-colorvar(--slotted-disabled-foreground)
--slotted-number-field-hover-border-colorvar(--slotted-tone-accent-border)
--slotted-number-field-icon-size-min14px
--slotted-number-field-invalid-border-colorvar(--slotted-tone-danger-border)
--slotted-number-field-invalid-edge-widthvar(--slotted-control-invalid-edge-width, 1px)
--slotted-number-field-invalid-focus-ring-colorvar(--slotted-focus-ring-color-invalid, Highlight)
--slotted-number-field-placeholder-colorvar(--slotted-text-muted, GrayText)
--slotted-number-field-plain-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-number-field-radiusvar(--slotted-control-radius, 4px)
--slotted-number-field-readonly-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-number-field-readonly-border-colorvar(--slotted-border-subtle, ButtonBorder)
--slotted-number-field-scrub-active-colorvar(--slotted-text, CanvasText)
--slotted-number-field-shadowvar(--slotted-control-shadow, none)
--slotted-number-field-stepper-active-backgroundvar(--slotted-tone-secondary-subtle-active)
--slotted-number-field-stepper-colorvar(--slotted-text-muted, GrayText)
--slotted-number-field-stepper-disabled-backgroundvar(--slotted-disabled-background)
--slotted-number-field-stepper-disabled-colorvar(--slotted-disabled-foreground)
--slotted-number-field-stepper-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)
--slotted-number-field-stepper-sizemax( var(--slotted-target-min, 24px), min(calc(var(--_icon-size) + 2 * var(--slotted-space-2, 0.25rem)), var(--_inner-size)) )
--slotted-number-field-subtle-backgroundvar(--slotted-surface-sunken, Canvas)
--slotted-number-field-subtle-border-colorvar(--slotted-border-subtle, ButtonBorder)
--slotted-number-field-subtle-hover-backgroundvar(--slotted-tone-secondary-subtle-hover)

Theme tokens it reads

TokenDefault
--slotted-border-subtleSet per scheme by the theme
--slotted-control-border-colorSet per scheme by the theme
--slotted-control-border-widthvar(--slotted-border-width-thin)
--slotted-control-focus-ring-offsetvar(--slotted-focus-ring-offset)
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-gap-lgcomfortable 8px · compact 6px · dense 6px
--slotted-control-gap-mdcomfortable 8px · compact 6px · dense 4px
--slotted-control-gap-smcomfortable 6px · compact 4px · dense 4px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-inner-size-lgcomfortable 28px · compact 24px · dense 22px
--slotted-control-inner-size-mdcomfortable 24px · compact 20px · dense 18px
--slotted-control-inner-size-smcomfortable 22px · compact 18px · dense 16px
--slotted-control-invalid-edge-widthvar(--slotted-border-width-thin)
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-shadowSet per scheme by the theme
--slotted-control-shadow-focus-geometry0 1px 4px -1px
--slotted-control-surfaceSet per scheme by the theme
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-color-invalidSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-radius-none0px
--slotted-radius-xs2px
--slotted-space-10.125rem
--slotted-space-20.25rem
--slotted-surface-sunkenSet per scheme by the theme
--slotted-target-comfortable44px
--slotted-target-min24px
--slotted-textSet per scheme by the theme
--slotted-text-mutedSet per scheme by the theme
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-danger-borderSet per scheme by the theme
--slotted-tone-secondary-subtle-activeSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme