coss.com Svelte

Number Field

A numeric input element with increment and decrement buttons, and a scrub area.

Loading p-number-field-1 preview…

Installation

Usage

<script lang="ts">
  import * as NumberField from "$lib/components/ui/number-field/index.js";
</script>

<NumberField.Root defaultValue={0}>
  <NumberField.ScrubArea label="Quantity" />
  <NumberField.Group>
    <NumberField.Decrement />
    <NumberField.Input />
    <NumberField.Increment />
  </NumberField.Group>
</NumberField.Root>

API Reference

NumberField.Root

Root component for the local Svelte number-field behavior.

PropTypeDefaultDescription
size"sm" \| "default" \| "lg""default"Controls the size of all child components through context

NumberField.Group

Container for the input and buttons.

NumberField.Input

Input element for the numeric value.

NumberField.Increment

Button that increments the value and displays the Hugeicons add glyph.

NumberField.Decrement

Button that decrements the value and displays the Hugeicons minus glyph.

NumberField.ScrubArea

Draggable area for scrubbing the value.

PropTypeDefaultDescription
labelstring-Label text displayed in the scrub area

Examples

For accessible labelling and validation, prefer using Field to wrap number fields. See Number field.

Small Size

Loading p-number-field-2 preview…

Large Size

Loading p-number-field-3 preview…

Disabled

Loading p-number-field-4 preview…

With External Label

Loading p-number-field-5 preview…

With Scrub

Loading p-number-field-6 preview…

With Range

Loading p-number-field-7 preview…

With Formatted Value

Loading p-number-field-8 preview…

With Step

Loading p-number-field-9 preview…

Form Integration

Set name on NumberField.Root and leave the surrounding Field.Root unnamed. This submits one canonical numeric value while preserving the visible input’s form owner and Enter submission. A named Field.Root currently gives both the visible input and the hidden numeric input the same name, producing duplicate form entries; formatted input can also submit locale-specific text.

<Field.Root>
  <NumberField.Root name="quantity" defaultValue={1} min={1} max={100}>
    <NumberField.ScrubArea label="Quantity" />
    <NumberField.Group>
      <NumberField.Decrement />
      <NumberField.Input />
      <NumberField.Increment />
    </NumberField.Group>
  </NumberField.Root>
</Field.Root>

An unnamed field does not associate server errors by field name. If your form uses server errors, handle their display and invalid state explicitly. The example below validates the submitted quantity and shows a success alert; it does not render Field.Error.

Loading p-number-field-10 preview…