# Counter

A compact whole-number count: a minus button, the value, and a plus button. It is only as wide as the count, for guests, seats and items. The value is a text spinbutton, so it can be typed. Use NumberInput for a formatted amount, such as a price.

**Dependencies:** npm: 2 direct, 3 total; registry: 2 direct, 2 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Adjust a small whole-number count, such as guests, seats or items, with a minus and a plus.
- Show the count in a row, only as wide as the count itself.

### When not to use

- Enter a formatted amount, such as a price or a weight. Use NumberInput.
- Enter an identifier such as a phone or card number. Use Input.
- Pick a position where the range matters more than the number. Use Slider.

### Native

**Guests**

```text
WerewolfCounter(value: guests, onValueChange: { guests = $0 }, min: 1, max: 8)
```

```text
WerewolfCounter(value = guests, onValueChange = { guests = it }, min = 1f, max = 8f)
```

**Uncontrolled**

```text
WerewolfCounter(defaultValue: 2, min: 1, max: 8)
```

```text
WerewolfCounter(defaultValue = 2.0, min = 1f, max = 8f)
```

**Half steps**

```text
WerewolfCounter(defaultValue: 1, step: 0.5)
```

```text
WerewolfCounter(defaultValue = 1f, step = 0.5f)
```

**Named buttons**

```text
WerewolfCounter(defaultValue: 1, decrementLabel: "Remove one", incrementLabel: "Add one")
```

```text
WerewolfCounter(defaultValue = 1f, decrementLabel = "Remove one", incrementLabel = "Add one")
```

## Variants

### size

Control height from size-element-sm/md/lg/xl. Pick per region; md is the default.

Values: `sm`, `md`, `lg`, `xl`. Default: `md`.

## Examples

### Guests

```tsx
<Field className="w-full max-w-sm">
  <FieldLabel>Guests</FieldLabel>
  <FieldControl>
    <Counter defaultValue={2} min={1} max={8} />
  </FieldControl>
  <FieldDescription>How many people are staying.</FieldDescription>
</Field>
```

### Named buttons

The buttons are skipped by Tab. Their names are what a screen reader says when they are pressed.

```tsx
<Counter defaultValue={1} min={0} max={99} decrementLabel="Remove one" incrementLabel="Add one" />
```

### Disabled

```tsx
<Field className="w-full max-w-sm" disabled>
  <FieldLabel>Seats</FieldLabel>
  <FieldControl>
    <Counter defaultValue={4} />
  </FieldControl>
</Field>
```

## Do and don't

### Do

- Use Counter for a small whole-number count, such as guests, seats or items.
- Set min and max whenever the domain has them.
- Use it inside Field so the label and the error are linked.
- Use lg or xl on a touch-first screen so the buttons are easy to hit.

### Don't

- Do not use Counter for a price or any formatted amount. Use NumberInput.
- Do not use it for an identifier such as a phone or a card number. Use Input.
- Do not use it where the position on a range matters more than the number. Use Slider.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `value` | `number` |  |  | The value when you control it. |
| `defaultValue` | `number` |  | `0` | The value on first render when you do not control it. |
| `onValueChange` | `(value: number) => void` |  |  | Called with the next number after a step key, a button press, Enter or blur. Not called for text that does not parse, or on every keystroke. |
| `min` | `number` |  | `0` | Lowest value. The minus button and ArrowDown stop here. |
| `max` | `number` |  |  | Highest value. Must be greater than min. The plus button and ArrowUp stop here. |
| `step` | `number` |  | `1` | Amount per step. Must be greater than 0. |
| `size` | `enum` | `sm`, `md`, `lg`, `xl` | `md` | Control height from size-element-sm/md/lg/xl. Pick per region; md is the default. |
| `disabled` | `boolean` |  | `false` | No typing, no buttons, disabled look. |
| `readOnly` | `boolean` |  | `false` | The value can be read and copied but not changed. The step buttons are hidden. |
| `invalid` | `boolean` |  | `false` | Sets aria-invalid and the danger border. |
| `required` | `boolean` |  | `false` | Native required on the spinbutton. Not generated for iOS and Android. |
| `name` | `string` |  |  | Form name. The submitted value is the number, on a hidden input. Not generated for iOS and Android. |
| `decrementLabel` | `string` |  | `Decrease` | Name of the minus button. |
| `incrementLabel` | `string` |  | `Increase` | Name of the plus button. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The group that draws the field box, only as wide as the count. The minus button, the value and the plus button sit in one row. |
| `decrement` | The minus button at the start (Tabler minus, 16px, 20px at xl). |
| `input` | The text spinbutton in the middle, centered, with tabular numbers. |
| `increment` | The plus button at the end (Tabler plus, 16px, 20px at xl). |
| `decrementIcon` | The minus mark inside the minus button, in the icon color, 16px (20px at xl). |
| `incrementIcon` | The plus mark inside the plus button, in the icon color, 16px (20px at xl). |

## Accessibility

Role: `spinbutton`

| Keys | Action |
| --- | --- |
| `ArrowUp / ArrowDown` | Increase or decrease by step, clamped to min and max. |
| `Home / End` | Set min, or max when max is set. |
| `Enter` | Commit the typed text: parse, clamp and round to step. Text that does not parse is dropped and the previous value stays. |
| `Tab` | Moves to the next field. The step buttons are skipped (tabIndex -1); the keys above cover them. |

- The value is a text input with role spinbutton, inputmode numeric when step is a whole number and min is at least 0, otherwise decimal. It exposes aria-valuenow, aria-valuemin, aria-valuemax and aria-valuetext.
- The step buttons are real buttons named by decrementLabel and incrementLabel. They are disabled at min and max, and skipped by Tab.
- Clearing the field, or typing text that does not parse, restores the previous value on blur. The value is always a number.
- name submits the number on a hidden input. The visible field has no name, so a half-typed draft is not submitted.
- invalid sets aria-invalid. Field passes its error with aria-describedby.

## Install

```bash
npx werewolf-ui add counter
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add counter --platform ios
npx werewolf-ui add counter --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
