# Number Input

A number field with buttons to step the value down and up.

A number field with a minus and a plus button inside its end. The field is the Input (the shared control's sizes and looks: its fill, hairline, focus and invalid rings, 16px text on touch) and the buttons are ghost Buttons laid over the room kept at its end. Arrow keys step, Shift + arrow ten steps, Home / End go to the bounds; a button held down repeats. The value can be formatted (currency, percent) with Intl options. Behaviour by Ark UI's number-input (Zag). Controlled or uncontrolled.

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `number-input` machine, through Ark UI

## Add

```bash
npx fndn add number-input
```

It writes the folder `number-input/` into the project's ui folder (`index.ts`, `number-input.tsx`, `NumberInput.vue`, `NumberInput.svelte`, `number-input.recipe.ts`), and what it is built from: [Input](/components/input.md), [Button](/components/button.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { NumberInput } from "@/components/ui/number-input";
```

```vue
import { NumberInput } from "@/components/ui/number-input";
```

```svelte
import { NumberInput } from "$lib/components/ui/number-input";
```

## Parts

In the DOM the root carries `data-slot="number-input"` and each part `data-slot="number-input-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `number-input` |  | Ark's number-input root: a column, the label above the control. As wide as its container (give it a width). |
| `label` | `label` | `number-input-label` | optional | Visible name (the label prop): text.sm, medium, ink, as the Field's label. It wraps, never truncates. |
| `control` | `div` | `number-input-control` |  | Holds the field and the two buttons laid over its end. |
| `input` | `input` | `number-input-input` |  | The Input (data-number-input="input"): role=spinbutton, tabular figures, its end padded for the buttons (xs 42px, sm 48px, md 56px, lg 60px). |
| `decrement-trigger` | `button` | `number-input-decrement-trigger` |  | The Button (data-number-input="decrement-trigger"): ghost, muted, rounded, square, with a minus; xs 18px, sm 20px, md and lg 24px. Out of the tab order; disabled at the minimum. Held down it repeats. On a coarse pointer its hit area is 40px high. |
| `increment-trigger` | `button` | `number-input-increment-trigger` |  | The Button (data-number-input="increment-trigger") with a plus, at the very end: as the decrement trigger; disabled at the maximum. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` |  | Bindable (React: `defaultValue` and `onValueChange`; Vue: `v-model:value`; Svelte: `bind:value`). The text of the field ("12"; "" when empty). onValueChange also gives the number. |
| `min` | `number` |  | Lower bound: the minus button is disabled there, and a typed value below it is clamped on blur. |
| `max` | `number` |  | Upper bound. |
| `step` | `number` | `1` |  |
| `size` | `"xs" \| "sm" \| "md" \| "lg"` | `"md"` | The Input's sizes (`lib/control.ts`): xs 24px field and 18px buttons, sm 28px and 20px, md 32px and 24px, lg 36px and 24px. |
| `variant` | `"outline" \| "subtle" \| "ghost"` | `"outline"` | The shared control's looks (`lib/control.ts`), at every size. outline: the form field (field colour, hairline). subtle: the inspector (inset grey, no edge). ghost: nothing until touched. Focused, open and invalid look the same in every variant. |
| `label` | `string` |  | Visible label above the field. |
| `placeholder` | `string` |  |  |
| `formatOptions` | `object` |  | Intl.NumberFormat options for the shown value (style currency / percent, grouping, fraction digits). |
| `allowMouseWheel` | `boolean` | `false` | The wheel steps the value while the field has focus. |
| `decrementLabel` | `string` | `"Decrease"` | Accessible name of the minus button. |
| `incrementLabel` | `string` | `"Increase"` | Accessible name of the plus button. |
| `invalid` | `boolean` | `false` | The Input's red ring (aria-invalid). |
| `disabled` | `boolean` | `false` |  |
| `readOnly` | `boolean` | `false` |  |
| `name` | `string` |  | Form field name. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Events

| Event | Payload |
| --- | --- |
| `valueChange` | (value: string, valueAsNumber: number) on every change |
| `valueCommit` | (value: string, valueAsNumber: number) after Enter, a blur or a step |

## States

`hover`, `focus`, `invalid`, `disabled`, `at-min`, `at-max`

## Keyboard and accessibility

Role: spinbutton.

Keyboard:

- Arrow Up / Arrow Down: one step up / down; with Shift ten steps
- Home / End: the minimum / the maximum (when set)
- Enter: settles the typed value (formatted and clamped)
- The buttons are out of the tab order: the arrow keys do what they do

Notes:

- The field is role=spinbutton with aria-valuemin, aria-valuemax, aria-valuenow and aria-roledescription "numberfield".
- Name it with label (visible) or aria-label; the buttons are named by decrementLabel / incrementLabel and point at the field (aria-controls).
- The text is 16px on touch devices (the Input's rule), so iOS does not zoom.
- To set it from outside, control `value`: React value / onValueChange.

## Motion

- the Input's: rings over duration.base, fill over duration.fast (pointer hover only)
- the Buttons': press scales to motion.press-scale; hover fill over duration.fast (pointer only)
- the value changes in place, instantly

## Tokens

`color.ink`, `text.sm`

## Dependencies

- [Input](/components/input.md)
- [Button](/components/button.md)

## Examples

### Seats

React · `seats.tsx`

```tsx
import { NumberInput } from "@/components/ui/number-input";

/* min and max bound the steppers, the arrow keys and what can be typed */
export default function Seats() {
  return <NumberInput label="Seats" defaultValue="12" min={1} max={500} className="w-40" />;
}
```

Vue · `seats.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { NumberInput } from "@/components/ui/number-input";

const seats = ref("12");
</script>

<!-- min and max bound the steppers, the arrow keys and what can be typed -->
<template>
  <NumberInput v-model:value="seats" label="Seats" :min="1" :max="500" class="w-40" />
</template>
```

Svelte · `seats.svelte`

```svelte
<script lang="ts">
  import { NumberInput } from "$lib/components/ui/number-input";

  let seats = $state("12");
</script>

<!-- min and max bound the steppers, the arrow keys and what can be typed -->
<NumberInput bind:value={seats} label="Seats" min={1} max={500} class="w-40" />
```

### Monthly spend limit

React · `spend-limit.tsx`

```tsx
import { NumberInput } from "@/components/ui/number-input";

/* formatOptions shows the value as money; the steppers move by $50 */
export default function SpendLimit() {
  return (
    <NumberInput
      label="Monthly spend limit"
      defaultValue="500"
      min={0}
      step={50}
      formatOptions={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }}
      className="w-48"
    />
  );
}
```

Vue · `spend-limit.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { NumberInput } from "@/components/ui/number-input";

const limit = ref("500");
</script>

<!-- formatOptions shows the value as money; the steppers move by $50 -->
<template>
  <NumberInput
    v-model:value="limit"
    label="Monthly spend limit"
    :min="0"
    :step="50"
    :format-options="{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }"
    class="w-48"
  />
</template>
```

Svelte · `spend-limit.svelte`

```svelte
<script lang="ts">
  import { NumberInput } from "$lib/components/ui/number-input";

  let limit = $state("500");
</script>

<!-- formatOptions shows the value as money; the steppers move by $50 -->
<NumberInput
  bind:value={limit}
  label="Monthly spend limit"
  min={0}
  step={50}
  formatOptions={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }}
  class="w-48"
/>
```

### Retries in a settings row

React · `webhook-retries.tsx`

```tsx
import { Label } from "@/components/ui/label";
import { NumberInput } from "@/components/ui/number-input";

/* sm in a settings row: the Label points at the input by id */
export default function WebhookRetries() {
  return (
    <div className="flex w-full max-w-80 items-center justify-between gap-4">
      <div className="flex min-w-0 flex-col gap-0.5">
        <Label htmlFor="webhook-retries">Retries</Label>
        <span className="text-xs text-ink-2">Before a delivery is marked failed.</span>
      </div>
      <NumberInput id="webhook-retries" size="sm" defaultValue="5" min={0} max={10} className="w-24 shrink-0" />
    </div>
  );
}
```

Vue · `webhook-retries.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Label } from "@/components/ui/label";
import { NumberInput } from "@/components/ui/number-input";

const retries = ref("5");
</script>

<!-- sm in a settings row: the Label points at the input by id -->
<template>
  <div class="flex w-full max-w-80 items-center justify-between gap-4">
    <div class="flex min-w-0 flex-col gap-0.5">
      <Label for="webhook-retries">Retries</Label>
      <span class="text-xs text-ink-2">Before a delivery is marked failed.</span>
    </div>
    <NumberInput id="webhook-retries" v-model:value="retries" size="sm" :min="0" :max="10" class="w-24 shrink-0" />
  </div>
</template>
```

Svelte · `webhook-retries.svelte`

```svelte
<script lang="ts">
  import { Label } from "$lib/components/ui/label";
  import { NumberInput } from "$lib/components/ui/number-input";

  let retries = $state("5");
</script>

<!-- sm in a settings row: the Label points at the input by id -->
<div class="flex w-full max-w-80 items-center justify-between gap-4">
  <div class="flex min-w-0 flex-col gap-0.5">
    <Label for="webhook-retries">Retries</Label>
    <span class="text-xs text-ink-2">Before a delivery is marked failed.</span>
  </div>
  <NumberInput id="webhook-retries" bind:value={retries} size="sm" min={0} max={10} class="w-24 shrink-0" />
</div>
```

### Seats with a live total

React · `seats-total.tsx`

```tsx
import { useState } from "react";
import { NumberInput } from "@/components/ui/number-input";

const price = 20;

/* controlled: the total follows the value as it changes */
export default function SeatsTotal() {
  const [seats, setSeats] = useState("8");
  const count = Number(seats) || 0;
  return (
    <div className="flex items-end gap-3">
      <NumberInput label="Seats on Team" value={seats} onValueChange={setSeats} min={1} max={500} className="w-36" />
      <span className="pb-1.5 text-sm text-ink-2">${(count * price).toLocaleString("en-US")} a month</span>
    </div>
  );
}
```

Vue · `seats-total.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { NumberInput } from "@/components/ui/number-input";

const price = 20;
const seats = ref("8");
const count = computed(() => Number(seats.value) || 0);
</script>

<!-- controlled: the total follows the value as it changes -->
<template>
  <div class="flex items-end gap-3">
    <NumberInput v-model:value="seats" label="Seats on Team" :min="1" :max="500" class="w-36" />
    <span class="pb-1.5 text-sm text-ink-2">${{ (count * price).toLocaleString("en-US") }} a month</span>
  </div>
</template>
```

Svelte · `seats-total.svelte`

```svelte
<script lang="ts">
  import { NumberInput } from "$lib/components/ui/number-input";

  const price = 20;
  let seats = $state("8");
  const count = $derived(Number(seats) || 0);
</script>

<!-- controlled: the total follows the value as it changes -->
<div class="flex items-end gap-3">
  <NumberInput bind:value={seats} label="Seats on Team" min={1} max={500} class="w-36" />
  <span class="pb-1.5 text-sm text-ink-2">${(count * price).toLocaleString("en-US")} a month</span>
</div>
```

## Recipe

`number-input.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* number-input — a number field with a minus and a plus button inside its end (Ark UI number-input).
 * The field is the real Input, so its sizes and looks are the shared control's (lib/control.ts):
 *   xs  24px field · 18px buttons, 3px from the edge
 *   sm  28px field · 20px buttons, 4px from the edge
 *   md  32px field (default) · 24px buttons, 4px from the edge
 *   lg  36px field · 24px buttons, 6px from the edge
 * The field is the real Input (its fill, hairline, rings, invalid and 16px text on touch) with room
 * kept at its end for the two buttons. The buttons are real Buttons (ghost, muted, rounded: the
 * chip radius, concentric with the field's control radius), laid over that room; the engine keeps
 * them out of the tab order (the arrow keys step) and disables the one at a bound. Figures are
 * tabular, so stepping never makes the value jitter in width.
 *
 * Touch: each button's hit area reaches 8px above and below on a coarse pointer (40px high);
 * side by side they stay 24px wide each. */
export const numberInput = tv({
  slots: {
    base: "flex flex-col gap-1.5 w-full min-w-0",
    label: "text-sm font-medium text-ink [overflow-wrap:anywhere] [unicode-bidi:plaintext]",
    control: "relative w-full min-w-0",
    /* the Input */
    input: "tabular-nums",
    /* the Buttons */
    decrementTrigger: "absolute top-1/2 -translate-y-1/2 pointer-coarse:after:content-[''] pointer-coarse:after:absolute pointer-coarse:after:inset-x-0 pointer-coarse:after:-inset-y-2",
    incrementTrigger: "absolute top-1/2 -translate-y-1/2 pointer-coarse:after:content-[''] pointer-coarse:after:absolute pointer-coarse:after:inset-x-0 pointer-coarse:after:-inset-y-2",
  },
  variants: {
    size: {
      xs: { input: "pe-[42px]", decrementTrigger: "size-[18px] min-h-[18px] end-[21px]", incrementTrigger: "size-[18px] min-h-[18px] end-[3px]" },
      sm: { input: "pe-12", decrementTrigger: "size-5 min-h-5 end-6", incrementTrigger: "size-5 min-h-5 end-1" },
      md: { input: "pe-14", decrementTrigger: "end-7", incrementTrigger: "end-1" },
      lg: { input: "pe-15", decrementTrigger: "end-[30px]", incrementTrigger: "end-1.5" },
    },
  },
  defaultVariants: { size: "md" },
});

export type NumberInputRecipeProps = VariantProps<typeof numberInput>;
```
