Prototyper UI

NumberField

A number input with increment/decrement controls built on Base UI

Loading...

Installation

pnpm dlx shadcn@latest add https://prototyper-ui.com/r/numberfield.json
npx shadcn@latest add https://prototyper-ui.com/r/numberfield.json
yarn dlx shadcn@latest add https://prototyper-ui.com/r/numberfield.json
bunx --bun shadcn@latest add https://prototyper-ui.com/r/numberfield.json

This will add the following files to your project:

  • components/ui/numberfield.tsx

Note: This component depends on Field. It will be installed automatically.

Usage

import {
  NumberField,
  NumberFieldGroup,
  NumberFieldInput,
  NumberFieldSteppers,
} from "@/components/ui/numberfield"

<NumberField>
  <NumberFieldGroup>
    <NumberFieldInput />
    <NumberFieldSteppers />
  </NumberFieldGroup>
</NumberField>

Anatomy

<NumberField>
  <FieldLabel />
  <NumberFieldGroup>
    <NumberFieldInput />
    <NumberFieldSteppers />
  </NumberFieldGroup>
  <FieldDescription />
  <FieldError />
</NumberField>

Or with individual step buttons:

<NumberField>
  <NumberFieldGroup>
    <NumberFieldDecrement />
    <NumberFieldInput />
    <NumberFieldIncrement />
  </NumberFieldGroup>
</NumberField>
Sub-componentdata-slotPurposeRequired
NumberFieldnumber-fieldRoot provider, manages value stateYes
NumberFieldGroupnumber-field-groupVisual container for input and steppersYes
NumberFieldInputnumber-field-inputThe numeric input elementYes
NumberFieldIncrementnumber-field-incrementButton to increase the valueNo
NumberFieldDecrementnumber-field-decrementButton to decrease the valueNo
NumberFieldSteppersnumber-field-steppersConvenience wrapper with increment/decrementNo
NumberFieldScrubAreanumber-field-scrub-areaDrag-to-scrub area for value adjustmentNo
NumberFieldScrubAreaCursornumber-field-scrub-area-cursorCustom cursor for the scrub areaNo

Examples

Currency

Loading...

Description

Loading...

Disabled

Loading...

Formatting

Loading...

Percentages

Loading...

Read Only

Loading...

Reusable

Loading...

Step Values

Loading...

Units

Loading...

Validation

Loading...

Validation Error

Loading...

Styling

Data Slots

Use data-slot attributes to target specific parts of the number field:

Slot nameElement
number-fieldRoot wrapper
number-field-groupVisual container for input and controls
number-field-inputThe <input> element
number-field-incrementIncrement button
number-field-decrementDecrement button
number-field-steppersSteppers container (increment + decrement)
number-field-scrub-areaDrag-to-scrub interaction area
number-field-scrub-area-cursorCustom cursor for scrub area

Customization Examples

/* Make the number field group larger */
[data-slot="number-field-group"] {
  @apply h-12;
}

/* Style the stepper buttons */
[data-slot="number-field-increment"],
[data-slot="number-field-decrement"] {
  @apply px-2 text-primary;
}
{/* Override size via className on the group */}
<NumberFieldGroup size="lg" className="rounded-lg">
  <NumberFieldInput />
  <NumberFieldSteppers />
</NumberFieldGroup>

API Reference

NumberField

Root component that manages the number field state. Built on Base UI NumberField.Root.

Prop

Type

All Base UI NumberField props are forwarded via ...props.

NumberFieldGroup

Visual container that wraps the input and stepper controls.

Prop

Type

All Base UI NumberField.Group props are forwarded via ...props.

NumberFieldInput

The numeric input element.

Prop

Type

All Base UI NumberField.Input props are forwarded via ...props.

NumberFieldIncrement

Button that increments the value. Renders a chevron-up icon by default.

Prop

Type

All Base UI NumberField.Increment props are forwarded via ...props.

NumberFieldDecrement

Button that decrements the value. Renders a chevron-down icon by default.

Prop

Type

All Base UI NumberField.Decrement props are forwarded via ...props.

NumberFieldSteppers

Convenience wrapper that renders increment and decrement buttons in a vertical stack.

Prop

Type

All standard div props are forwarded via ...props.

NumberFieldScrubArea

A drag-to-scrub interaction area for adjusting the value by dragging.

Prop

Type

All Base UI NumberField.ScrubArea props are forwarded via ...props.

NumberFieldScrubAreaCursor

Custom cursor element rendered within the scrub area.

Prop

Type

All Base UI NumberField.ScrubAreaCursor props are forwarded via ...props.

numberFieldGroupVariants

A cva helper exported for applying number field group styles outside the component.

import { numberFieldGroupVariants } from "@/components/ui/numberfield"

<div className={numberFieldGroupVariants({ size: "lg" })}>...</div>

Accessibility

Keyboard Interactions

KeyAction
ArrowUpIncrements the value by one step
ArrowDownDecrements the value by one step
HomeSets the value to the minimum (if min is set)
EndSets the value to the maximum (if max is set)
TabMoves focus into or out of the number field

ARIA Attributes

  • The input renders with role="spinbutton" via Base UI.
  • aria-valuenow, aria-valuemin, and aria-valuemax are set automatically based on the current value and constraints.
  • aria-invalid is set when validation fails.
  • data-disabled is set on the group and controls when the field is disabled.
  • Increment and decrement buttons are labeled for screen readers.

On this page