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.
Preview
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.
Platform support
What each platform has, from the spec. A selected platform is highlighted.
| Name | Web | iOS | Android | Note |
|---|---|---|---|---|
| value | Yes | Yes | Yes | |
| defaultValue | Yes | Yes | Yes | |
| onValueChange | Yes | Yes | Yes | |
| min | Yes | Yes | Yes | |
| max | Yes | Yes | Yes | |
| step | Yes | Yes | Yes | |
| size | Yes | Yes | Yes | |
| disabled | Yes | Yes | Yes | |
| readOnly | Yes | Yes | Yes | |
| invalid | Yes | Yes | Yes | |
| required | Yes | Deferred | Deferred | Not generated for iOS and Android. |
| name | Yes | Deferred | Deferred | Not generated for iOS and Android. |
| decrementLabel | Yes | Yes | Yes | |
| incrementLabel | Yes | Yes | Yes | |
| value of keyboard: ArrowUp / ArrowDown | Yes | Deferred | Deferred | |
| value of keyboard: Home / End | Yes | Deferred | Deferred | |
| value of keyboard: Enter | Yes | Deferred | Deferred |
Variants
size
Control height from size-element-sm/md/lg/xl. Pick per region; md is the default.
Examples
Guests
Named buttons
The buttons are skipped by Tab. Their names are what a screen reader says when they are pressed.
Disabled
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
npx werewolf-ui add counterRun 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 for requirements and the alternative registry setup.