Skip to content

Number Field ​

A number input with an increment button, a decrement button and a scrub area.

vue
<script setup lang="ts">
import { h, useId } from 'vue';
import type { FunctionalComponent } from 'vue';
import { NumberField } from '@vase-ui/vue/number-field';
import styles from './index.module.css';

const id = useId();

const PlusIcon: FunctionalComponent = () =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linecap': 'square',
      'stroke-linejoin': 'round',
      style: { display: 'block' },
    },
    [h('path', { d: 'M1.5 8h13M8 14.5v-13' })],
  );

const MinusIcon: FunctionalComponent = () =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linecap': 'square',
      'stroke-linejoin': 'round',
      style: { display: 'block' },
    },
    [h('path', { d: 'M1.5 8h13' })],
  );
</script>

<template>
  <NumberField.Root :default-value="100" :class="styles.Field">
    <NumberField.ScrubArea :class="styles.ScrubArea">
      <label :for="id" :class="styles.Label">Amount</label>
    </NumberField.ScrubArea>

    <NumberField.Group :class="styles.Group">
      <NumberField.Decrement aria-label="Decrease" :class="styles.Decrement">
        <MinusIcon />
      </NumberField.Decrement>
      <NumberField.Input :id="id" :class="styles.Input" />
      <NumberField.Increment aria-label="Increase" :class="styles.Increment">
        <PlusIcon />
      </NumberField.Increment>
    </NumberField.Group>
  </NumberField.Root>
</template>
css
.Field {
  display: flex;
  flex-direction: column;
  align-items: start;
  gap: 0.25rem;
}

.ScrubArea {
  cursor: ew-resize;
  font-weight: 700;
  -webkit-user-select: none;
  user-select: none;
}

.Label {
  cursor: ew-resize;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 700;
  color: oklch(14.5% 0 0deg);

  @media (prefers-color-scheme: dark) {
    color: white;
  }
}

.Group {
  display: flex;
  height: 2rem;
}

.Input {
  box-sizing: border-box;
  margin: 0;
  padding: 0 0.5rem;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  width: 7ch;
  height: 100%;
  font-family: inherit;
  font-size: 0.875rem;
  line-height: 1.25rem;
  font-weight: 400;
  background-color: white;
  color: oklch(14.5% 0 0deg);

  text-align: left;
  font-variant-numeric: tabular-nums;

  @media (any-pointer: coarse) {
    font-size: 1rem;
    line-height: 1.5rem;
  }

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  &:focus {
    z-index: 1;
    outline: 2px solid oklch(14.5% 0 0deg);
    outline-offset: -1px;

    @media (prefers-color-scheme: dark) {
      outline-color: white;
    }
  }
}

.Decrement,
.Increment {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 100%;
  margin: 0;
  outline: 0;
  padding: 0;
  border: 1px solid oklch(14.5% 0 0deg);
  border-radius: 0;
  background-color: white;
  background-clip: padding-box;
  color: oklch(14.5% 0 0deg);
  -webkit-user-select: none;
  user-select: none;

  @media (prefers-color-scheme: dark) {
    border: 1px solid white;
    background-color: oklch(14.5% 0 0deg);
    color: white;
  }

  @media (hover: hover) {
    &:hover:not(:disabled) {
      background-color: oklch(97% 0 0deg);

      @media (prefers-color-scheme: dark) {
        background-color: oklch(26.9% 0 0deg);
      }
    }
  }

  &:active:not(:disabled) {
    background-color: oklch(92.2% 0 0deg);

    @media (prefers-color-scheme: dark) {
      background-color: oklch(37.1% 0 0deg);
    }
  }

  &:disabled {
    color: oklch(55.6% 0 0deg);
    border-color: oklch(55.6% 0 0deg);

    @media (prefers-color-scheme: dark) {
      color: oklch(70.8% 0 0deg);
      border-color: oklch(70.8% 0 0deg);
    }
  }
}

.Decrement {
  border-right: 0;
}

.Increment {
  border-left: 0;
}
vue
<script setup lang="ts">
import { h, useId } from 'vue';
import type { FunctionalComponent } from 'vue';
import { NumberField } from '@vase-ui/vue/number-field';

const id = useId();

const PlusIcon: FunctionalComponent = () =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linecap': 'square',
      'stroke-linejoin': 'round',
      style: { display: 'block' },
    },
    [h('path', { d: 'M1.5 8h13M8 14.5v-13' })],
  );

const MinusIcon: FunctionalComponent = () =>
  h(
    'svg',
    {
      width: '16',
      height: '16',
      viewBox: '0 0 16 16',
      fill: 'none',
      stroke: 'currentColor',
      'stroke-linecap': 'square',
      'stroke-linejoin': 'round',
      style: { display: 'block' },
    },
    [h('path', { d: 'M1.5 8h13' })],
  );

const stepperClasses =
  'flex h-full w-8 items-center justify-center border border-neutral-950 bg-white bg-clip-padding text-neutral-950 outline-0 select-none dark:border-white dark:bg-neutral-950 dark:text-white hover:not-disabled:bg-neutral-100 dark:hover:not-disabled:bg-neutral-800 active:not-disabled:bg-neutral-200 dark:active:not-disabled:bg-neutral-700 disabled:border-neutral-500 disabled:text-neutral-500 dark:disabled:border-neutral-400 dark:disabled:text-neutral-400';
</script>

<template>
  <NumberField.Root :default-value="100" class="flex flex-col items-start gap-1">
    <NumberField.ScrubArea class="cursor-ew-resize font-bold select-none">
      <label :for="id" class="cursor-ew-resize text-sm font-bold text-neutral-950 dark:text-white"
        >Amount</label
      >
    </NumberField.ScrubArea>

    <NumberField.Group class="flex h-8">
      <NumberField.Decrement aria-label="Decrease" :class="[stepperClasses, 'border-r-0']">
        <MinusIcon />
      </NumberField.Decrement>
      <NumberField.Input
        :id="id"
        class="h-full w-[7ch] border border-neutral-950 bg-white px-2 text-left text-sm font-normal text-neutral-950 tabular-nums any-pointer-coarse:text-base dark:border-white dark:bg-neutral-950 dark:text-white focus:z-1 focus:outline-2 focus:-outline-offset-1 focus:outline-neutral-950 dark:focus:outline-white"
      />
      <NumberField.Increment aria-label="Increase" :class="[stepperClasses, 'border-l-0']">
        <PlusIcon />
      </NumberField.Increment>
    </NumberField.Group>
  </NumberField.Root>
</template>

Usage guidelines ​

  • Form controls must have an accessible name: Give the input a <label> element, or put the number field in a Field. Give each button a text or an aria-label. See the forms guide.

Anatomy ​

Import the component and assemble its parts:

vue
<script setup lang="ts">
import { NumberField } from '@vase-ui/vue/number-field';
</script>

<template>
  <NumberField.Root>
    <NumberField.ScrubArea />
    <NumberField.Group>
      <NumberField.Decrement aria-label="Decrease" />
      <NumberField.Input />
      <NumberField.Increment aria-label="Increase" />
    </NumberField.Group>
  </NumberField.Root>
</template>

API reference ​

NumberField.Root ​

Groups all parts of the number field and manages its state. Renders a <div> element with a hidden form <input> element in it.

Props

PropTypeDefaultDescription
allowOutOfRangeboolean | undefinedfalseA typed, pasted or filled number stays out of the range, so the form reports it. A step always stays in the range.
allowWheelScrubboolean | undefinedfalseThe wheel steps the value while the input has the focus.
defaultValuenumber | undefinedThe value of a field with no modelValue, at the mount and at a native reset of the form.
disabledboolean | undefinedfalseThe user cannot change the value, and the form data has no value.
formstring | undefinedThe id of the form that owns the inputs.
formatIntl.NumberFormatOptions | undefinedThe options of Intl.NumberFormat for the text. Typed text stays as the user typed it while its number is the value, and the blur gives it the format. A rounding option rounds the typed number at the blur.
largeStepnumber | undefined10The amount of one step with the Shift key.
localeIntl.LocalesArgumentthe locale of the nearest LocaleProvider, else 'en-US'The locale of the text.
maxnumber | undefinedThe greatest value. The End key sets it.
minnumber | undefinedThe least value. The Home key sets it.
modelValuenumber | null | undefinedThe value. null is an empty field, and NaN counts as null.
namestring | undefinedThe name of the value in the form data. The form gets the number, not the text of the input. The name of a Field.Root wins.
readonlyboolean | undefinedfalseThe user cannot change the value. The input stays in the tab order.
requiredboolean | undefinedfalseThe user must give a value before a submit.
smallStepnumber | undefined0.1The amount of one step with the Alt key.
snapOnStepboolean | undefinedfalseA step gives a multiple of its amount, counted from min, or from 0 with no min.
stepnumber | "any" | undefinedThe amount of one step. A number is also the step of the hidden input, so the form accepts only its multiples from min, and each number with no min. With none, or 'any', a step is 1 and the form accepts each number.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: NumberFieldRootState
The content of the part.

Events

EventArgumentsDescription
valueChange[value: number | null, details: NumberFieldRootChangeEventDetails]The user changes the value. details.cancel() keeps the value as it was.
update:modelValue[value: number | null]Comes after the change event with the new value, unless a listener cancels the change. v-model uses it.
valueCommitted[value: number | null, details: NumberFieldRootCommitEventDetails]The user ends a change: at the blur after typed edits, a key, a wheel step, a release or a fill.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.
inputElementHTMLInputElement | nullThe hidden input that gives the number to a form.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-readonly]The field is read-only.
[data-required]The field is required.
[data-scrubbing]The user drags in a scrub area of the field.
[data-valid]The value is valid, in a Field.Root.
[data-invalid]The value is invalid, in a Field.Root.
[data-touched]The focus left the field one time or more, in a Field.Root.
[data-dirty]The value differs from the first value, in a Field.Root.
[data-filled]The field has a value, in a Field.Root.

NumberField.ScrubArea ​

An interactive area where the user can click and drag to change the field value. Renders a <span> element.

Props

PropTypeDefaultDescription
orientation"horizontal" | "vertical" | undefined'horizontal'The axis of the pointer that steps the value. A move up, or a move to the end of the line, steps up.
pixelSensitivitynumber | undefined2The distance of the pointer, in px, for one step.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: NumberFieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: NumberFieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-readonly]The field is read-only.
[data-required]The field is required.
[data-scrubbing]The user drags in a scrub area of the field.
[data-valid]The value is valid, in a Field.Root.
[data-invalid]The value is invalid, in a Field.Root.
[data-touched]The focus left the field one time or more, in a Field.Root.
[data-dirty]The value differs from the first value, in a Field.Root.
[data-filled]The field has a value, in a Field.Root.

NumberField.Group ​

Groups the input with the increment and decrement buttons. Renders a <div> element.

Props

PropTypeDefaultDescription
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: NumberFieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: NumberFieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
[data-disabled]The field is disabled.
[data-readonly]The field is read-only.
[data-required]The field is required.
[data-scrubbing]The user drags in a scrub area of the field.
[data-valid]The value is valid, in a Field.Root.
[data-invalid]The value is invalid, in a Field.Root.
[data-touched]The focus left the field one time or more, in a Field.Root.
[data-dirty]The value differs from the first value, in a Field.Root.
[data-filled]The field has a value, in a Field.Root.

NumberField.Decrement ​

A stepper button that decreases the field value when the user clicks it. Renders a <button> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe button cannot step the value.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: NumberFieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: NumberFieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
:disabledThe button cannot step the value: the button or the field is disabled, the field is read-only, or the value is at its bound.
[aria-disabled="true"]The button cannot step the value, and it is not a native button.
[data-readonly]The field is read-only.
[data-required]The field is required.
[data-scrubbing]The user drags in a scrub area of the field.
[data-valid]The value is valid, in a Field.Root.
[data-invalid]The value is invalid, in a Field.Root.
[data-touched]The focus left the field one time or more, in a Field.Root.
[data-dirty]The value differs from the first value, in a Field.Root.
[data-filled]The field has a value, in a Field.Root.

NumberField.Input ​

The native input control in the number field. Renders an <input> element with the role spinbutton.

Props

PropTypeDefaultDescription
idstring | undefinedThe id of the input. With none, the input has the id of its Field.Root.

Slots

SlotSlot propsDescription
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: NumberFieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLInputElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
:disabledThe field is disabled.
:read-onlyThe field is read-only or disabled.
:requiredThe field is required.
[aria-invalid="true"]The value is out of the range, or it is invalid in a Field.Root.
[data-scrubbing]The user drags in a scrub area of the field.
[data-valid]The value is valid, in a Field.Root.
[data-touched]The focus left the field one time or more, in a Field.Root.
[data-dirty]The value differs from the first value, in a Field.Root.
[data-filled]The field has a value, in a Field.Root.

NumberField.Increment ​

A stepper button that increases the field value when the user clicks it. Renders a <button> element.

Props

PropTypeDefaultDescription
disabledboolean | undefinedfalseThe button cannot step the value.
askeyof HTMLElementTagNameMap | Component | GenericComponent | undefinedA tag or a component that the part renders in place of its own element, with the same attributes and content. The element slot wins over it.

Slots

SlotSlot propsDescription
default
state: NumberFieldRootState
The content of the part.
element
props: Record<string, unknown>
elementRef: (target: unknown) => void
state: NumberFieldRootState
Renders your element in place of the element of the part. Bind props with v-bind and elementRef with :ref.

Exposed

MemberTypeDescription
elementHTMLElement | nullThe element of the part, or null while it is not mounted.

Selectors

SelectorDescription
:disabledThe button cannot step the value: the button or the field is disabled, the field is read-only, or the value is at its bound.
[aria-disabled="true"]The button cannot step the value, and it is not a native button.
[data-readonly]The field is read-only.
[data-required]The field is required.
[data-scrubbing]The user drags in a scrub area of the field.
[data-valid]The value is valid, in a Field.Root.
[data-invalid]The value is invalid, in a Field.Root.
[data-touched]The focus left the field one time or more, in a Field.Root.
[data-dirty]The value differs from the first value, in a Field.Root.
[data-filled]The field has a value, in a Field.Root.

Differences from Base UI ​

Vase UI ports Number Field from Base UI 1.9.0.

API ​

Base UIVase UI
NumberField.Rootv-model and valueChange replace value and onValueChange, and valueCommitted replaces onValueCommitted. readonly replaces readOnly, also in the state. With no step, a step is 1 and the hidden input has step="any", so the form accepts each number. :step="1" gives the constraint that Base UI has by default. With no locale, the text has the locale of the nearest LocaleProvider, else en-US. inputElement on the template ref replaces inputRef. The id prop of NumberField.Input names the element that has the role. The hidden input stays the last child of the root, so the root has no element slot. CSS selects :focus-within, so the state has no focused. The family stops no event and has no trigger, so the change details have no allowPropagation, isPropagationAllowed and trigger.
NumberField.ScrubAreaorientation replaces direction: on the web, direction is the direction of text, and Slider and Toolbar name this value set orientation. The native cursor stays at the pointer, and CSS cursor on the area and on html:has([data-scrubbing]) styles it in each browser. So the area asks for no pointer lock and has no teleportDistance: MDN marks requestPointerLock() "Limited availability", and a behavior of a part rests only on Baseline Widely Available features.
NumberField.ScrubAreaCursorThe native cursor stays at the pointer, and CSS cursor on the scrub area and on html:has([data-scrubbing]) styles it in each browser. So the family has no cursor part, and the scrub area asks for no pointer lock: MDN marks requestPointerLock() "Limited availability", and a behavior of a part rests only on Baseline Widely Available features.
NumberField.GroupCSS selects :focus-within, so the group has no data-focused.
NumberField.DecrementThe button reads the element that it renders, so it has no nativeButton. The developer gives the button a text or an aria-label, so it has no default aria-label. CSS selects :disabled, or [aria-disabled="true"] on an element that is not a button, and :focus-within on the root, so the button has no data-disabled and no data-focused.
NumberField.InputThe input is the native form control, so it has no as. The role spinbutton says what the input is in the language of the screen reader, so the input has no default aria-roledescription. An aria-roledescription of the developer is a plain attribute. CSS selects :disabled, :read-only, :required, [aria-invalid="true"] and :focus, so the input has no data-disabled, data-readonly, data-required, data-invalid and data-focused.
NumberField.IncrementThe button reads the element that it renders, so it has no nativeButton. The developer gives the button a text or an aria-label, so it has no default aria-label. CSS selects :disabled, or [aria-disabled="true"] on an element that is not a button, and :focus-within on the root, so the button has no data-disabled and no data-focused.

Behavior ​

Vase UIEvidence
A Number Field submits the number that it shows. Text with two dots or more is a number only when each dot before the last one can be a group separator, as in 1.234.567.89. A run of dots counts as one dot, as in Base UI: 1..5 in en-US and 1,,5 in de-DE are 1.5. Other text, such as 1.2.3, does not change the value. The rule also applies to the decimal separator of the locale: 1,2,3 in de-DE does not change the value. Base UI reads 1.2.3 as 12.3, so the field shows 1.2.3 and submits 12.3.
A repeat button stops after one step when that step releases the press. Base UI starts its repeat timer after the first step returns, even if a pointer release ended the press during that step.
With no locale prop, Meter.Root, Progress.Root, Slider.Root and NumberField.Root format their text in the locale of the nearest LocaleProvider, else in en-US. The server and the browser use the same locale, so the text does not change after the hydration. Base UI formats the text of Meter, Progress and Slider in en-US until the hydration, and then in the locale of the browser. It formats the text of Number Field in the default locale of each runtime, so the server and the browser can differ.
With no step prop, the buttons and the keys change the value by 1, and the hidden input has step="any", so the form accepts each number. A step prop of the developer is also the constraint of the form: with :min="0", :step="1" makes the form refuse 0.11. Base UI checks a step of 1 when the developer gives no step, so with min={0} its form refuses 0.11.
A control in a field keeps the id of the developer in the server HTML when no Field.Label of the field renders before it, and a label that renders after the control has that id in for. When a label renders before the control, the control has the generated id of the field until the hydration, because that label names the generated id. Then the control takes its id, and the label follows. In each order, the label names the control in the server HTML. Base UI gives each control in a field the generated id until the hydration, also where no label needs it.
A button part knows the element of a component in as or of the element slot only at the mount, and it needs no prop that says what the element is. Until the mount, the element has the attributes that suit a <button> and each other element: type="button", role="button", a tab stop, and the disabled state of both. So in the server HTML, Tab reaches a <div> before a lazy hydration, and a <button> does not submit its form. At the mount, the part keeps the attributes of its element. Base UI needs nativeButton set to false for an element that is not a <button>.

The handbook gives the differences that all components share: Styling, Animation, Composition, Customization, Forms and TypeScript.