Skip to content

SpinButton

SpinButton

A numeric input with decrement/increment buttons (APG spinbutton pattern).

- **Keyboard** on the input: ArrowUp/ArrowDown step by step, PageUp/PageDown by largeStep (default ten steps), Home/End jump to min/max when they are finite (Shift+Home and Shift+End keep their native text-selection instead). The −/+ buttons are not tab stops and never take focus, so focus stays on the spinbutton and screen readers announce the new value.
- **Pointer** on the −/+ buttons (mouse, touch or pen): a short press steps once, on release (its click), never on the press alone, so a finger that starts a scroll on a button changes nothing. Holding a button repeats the step: the first after 300ms, then faster, every 80ms after about a second (Fluent's timing). A hold stops when the button is released, the pointer leaves it or the press turns into a scroll, at the bound, when the window loses focus, and when the spin button becomes disabled or read-only. Only the primary button of a primary pointer holds: a second finger, a secondary button or a Ctrl+press with a mouse (the secondary click on macOS) neither starts a hold nor ends one. A click without a press (a screen reader, element.click()) steps once. The buttons open no context menu (a long press on touch would), show no iOS callout, and allow no text selection or double-tap zoom.
- **Typing** edits a draft: the value is parsed, clamped and committed on blur or Enter (Escape reverts). Text that is not a number is flagged with aria-invalid and reverted on blur. Steps (keys and buttons) start from the typed number, and the −/+ buttons are disabled when that number is at min/max. Steps are rounded to the precision of step and of the value, so 0.1 + 0.2 is 0.3; precision rounds every committed value, typed or stepped, to a fixed number of decimals instead, without padding it (pad with displayValue).
- **Routing**: ref, id, ARIA, native input attributes and focus/keyboard handlers go to the <input role="spinbutton">; className, style, data-* and other handlers stay on the root (slotProps.root.ref reaches it). Inside a Field it picks up the label, hint, error and required state automatically. It has no built-in name: pass aria-label/aria-labelledby or use a label (development warning otherwise).
- **Forms**: with name the committed value is submitted; required makes the input natively required; a form reset restores defaultValue.
- **Empty values**: allowEmpty lets the value become null (an empty field). Clearing the text and committing it (blur or Enter) sets the value to null, uncontrolled too; stepping up or down from an empty value starts at 0 and clamps to the nearest bound. required (its own, or a required Field's) still blocks submitting an empty value: the check runs on the HiddenInput that carries the committed value, not on the shown text, so it keeps working even when the shown text is not empty.
- **Display text**: displayValue shows formatted text ($1.00) for the value while the field is not being edited, and as its aria-valuetext; while the field has focus and can be edited it shows the plain number instead, so typing still edits the number (a consumer aria-valuetext always wins). It applies only while value is controlled: an uncontrolled spin button ignores it and warns once in development.
- **Size and appearance**: size resolves from its own prop, then the surrounding Field's size, then WaveProvider inputDefaults.size, else 'medium'; appearance from its own prop, then WaveProvider inputDefaults.appearance, else 'outline'; both render as data-size and data-appearance on the root. SpinButton takes every size the other fields do, and unlike 0.7 its root is now exactly as tall as theirs, with its step buttons and input taking up that height; sizing the root wider widens the input instead of leaving empty space inside it. The step buttons keep a 1px separator from the input only in outline; the other appearances have none.
- The −/+ buttons are named "Decrement"/"Increment"; labels localizes the names.

Examples

Default

export const Default: Story = {};

Min Max

export const MinMax: Story = {
  args: {
    'aria-label': 'Guests',
    defaultValue: 5,
    min: 0,
    max: 10,
  },
};

Decimal Step

export const DecimalStep: Story = {
  args: {
    'aria-label': 'Weight in kilograms',
    defaultValue: 1.5,
    min: 0,
    step: 0.1,
  },
};

Disabled

export const Disabled: Story = {
  args: {
    'aria-label': 'Seats',
    defaultValue: 3,
    disabled: true,
  },
};

Invalid

You can book at most 10 tickets.
export const Invalid: Story = {
  args: {
    'aria-label': 'Tickets',
    defaultValue: 12,
    'aria-invalid': true,
    'aria-describedby': 'spin-button-invalid-error',
  },
  render: (args) => (
    <div className="flex flex-col gap-1">
      <SpinButton {...args} />
      <span id="spin-button-invalid-error" className="text-wave-caption-1 text-wave-error">
        You can book at most 10 tickets.
      </span>
    </div>
  ),
};

Controlled

Value: 40

export const Controlled: Story = {
  args: {
    'aria-label': 'Volume',
    min: 0,
    max: 100,
    step: 5,
  },
  render: function ControlledStory(args) {
    const [value, setValue] = useState(40);
    return (
      <div className="flex flex-col gap-2">
        <SpinButton
          {...args}
          value={value}
          onValueChange={(next: number | null) => {
            if (next === null) return;
            setValue(next);
            args.onValueChange?.(next);
          }}
        />
        <p className="text-wave-body-1 text-wave-foreground">Value: {value}</p>
      </div>
    );
  },
};

Display Value

export const DisplayValue: Story = {
  args: { 'aria-label': 'Price' },
  render: (args) => <CurrencySpinButton {...args} />,
};

Allow Empty

export const AllowEmpty: Story = {
  render: () => <SpinButton aria-label="Guests (optional)" allowEmpty min={1} max={12} />,
};

Precision

export const Precision: Story = {
  args: { 'aria-label': 'Weight in kg', precision: 2, step: 0.25, defaultValue: 1.5 },
};

Full Width

export const FullWidth: Story = {
  args: { 'aria-label': 'Quantity', className: 'w-full' },
};

Sizes And Appearances

export const SizesAndAppearances: Story = {
  render: (args) => (
    <SizeAppearanceGrid
      render={(size, appearance) => (
        <SpinButton
          {...args}
          size={size}
          appearance={appearance}
          aria-label={`${size} ${appearance}`}
        />
      )}
    />
  ),
};

Props

NameTypeRequiredDefaultDescription
allowEmptybooleanNofalse

Allows an empty value (null). Takes the literal true: a boolean variable fits neither member of the props, so branch the JSX or spread { allowEmpty: true, value }.

valuenumber | nullNo

Controlled numeric value.

defaultValuenumber | nullNo0

Initial value for uncontrolled usage (also what a form reset restores).

onValueChange((value: number) => void) | ((value: number | null) => void)No

Called with the new value when it changes: a step (buttons, arrow keys, PageUp/PageDown, Home/End) or a typed value committed on blur or Enter. Not called while the user is typing, nor when the value stays the same.

minnumberNo-Infinity

Minimum allowed value.

maxnumberNoInfinity

Maximum allowed value.

stepnumberNo1

Step of the increment/decrement buttons and of ArrowUp/ArrowDown.

largeStepnumberNostep * 10

Step of PageUp/PageDown.

precisionnumberNo

Decimals every committed value is rounded to, typed or stepped (0–20); the number is not padded (use displayValue for 1.00). Default: steps round to the decimals of step and the value, typed values are kept as typed.

disabledbooleanNo

Whether the spin button is disabled and non-interactive.

namestringNo

Form field name. With a name, the committed value is submitted with the form.

formstringNo

Id of the form the spin button belongs to, when it is rendered outside that form.

labelsSpinButtonLabelsNo

Names of the −/+ step buttons (not tab stops, but in the accessibility tree), for localization. Unset members keep their English defaults.

displayValuestringNo

Text shown for the value while the field is not being edited, and its aria-valuetext (a $1.00 for 1). While the field has focus and can be edited it shows the plain number, so typing edits the number. Applies only while value is controlled (update it with the value); Fluent's displayValue.

size'small' | 'medium' | 'large'No

Size of the field: small (24px tall), medium (32px) or large (40px). Default: the surrounding Field's size, else WaveProvider inputDefaults.size, else 'medium'.

SpinButton is 24, 32 or 40px tall like every field; its buttons are square-ish, 24, 32 or 40px wide.

appearance'outline' | 'underline' | 'filled-darker' | 'filled-lighter'No

Look of the field: outline (a full border), underline (a bottom stroke only), filled-darker or filled-lighter (a fill without a visible stroke: give the field a visible label). Default: WaveProvider inputDefaults.appearance, else 'outline'.

The step buttons keep a 1px separator from the input only in outline; the other appearances have none.

refReact.Ref<HTMLInputElement>No

Ref to the <input role="spinbutton">, the focusable control (Fluent UI's ref, on its primary element). The root <div> is slotProps.root.ref.

slotProps{ root?: SlotPart<'div'>; decrementButton?: SlotPart<'button'>; input?: SlotPart<'input'>; incrementButton?: SlotPart<'button'> }No

Props for the elements the SpinButton draws, by part, merged into each part's own (Fluent UI's root, decrementButton, input and incrementButton slot props): your classes after the part's (yours win a conflict), your styles over its styles, your handlers before its handlers (preventDefault() in yours skips the SpinButton's handling of the event, its own on… prop included, but never the work that ends a hold or tracks focus), your ref beside its ref, and your ids added to its aria-labelledby and aria-describedby. Every attribute the SpinButton sets on a part is locked and keeps the SpinButton's value (set it through its own props); you add the attributes it does not set. A part takes no children.

Any other prop is passed to the element it renders (HTMLDivElement).

Parts

  • root
  • decrementButton
  • input
  • incrementButton

Stable class names

PartClass
rootwave-spin-button
decrementButtonwave-spin-button__decrement-button
inputwave-spin-button__input
incrementButtonwave-spin-button__increment-button