Skip to content

    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.

    React

    Preview

    How many people are staying.

    Props

    • min
    • max
    • step
    • size
    • disabled
    • readOnly
    • invalid
    • label
    • defaultValue
    import { Counter } from "@/components/ui/counter"
    import { Field, FieldControl, FieldDescription, FieldLabel } from "@/components/ui/field"
    
    <Field className="w-full max-w-sm">
      <FieldLabel>Guests</FieldLabel>
      <FieldControl>
        <Counter min={0} max={8} step={1} defaultValue={2} />
      </FieldControl>
      <FieldDescription>How many people are staying.</FieldDescription>
    </Field>

    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.

    NameWebiOSAndroidNote
    valueYesYesYes
    defaultValueYesYesYes
    onValueChangeYesYesYes
    minYesYesYes
    maxYesYesYes
    stepYesYesYes
    sizeYesYesYes
    disabledYesYesYes
    readOnlyYesYesYes
    invalidYesYesYes
    requiredYesDeferredDeferredNot generated for iOS and Android.
    nameYesDeferredDeferredNot generated for iOS and Android.
    decrementLabelYesYesYes
    incrementLabelYesYesYes
    value of keyboard: ArrowUp / ArrowDownYesDeferredDeferred
    value of keyboard: Home / EndYesDeferredDeferred
    value of keyboard: EnterYesDeferredDeferred

    Variants

    size

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

    • How many people are staying.

      sm

    • How many people are staying.

      mddefault

    • How many people are staying.

      lg

    • How many people are staying.

      xl

    import { Counter } from "@/components/ui/counter"
    import { Field, FieldControl, FieldDescription, FieldLabel } from "@/components/ui/field"
    
    <Field className="w-full max-w-sm">
      <FieldLabel>Guests</FieldLabel>
      <FieldControl>
        <Counter size="sm" min={0} max={8} step={1} defaultValue={2} />
      </FieldControl>
      <FieldDescription>How many people are staying.</FieldDescription>
    </Field>
    <Field className="w-full max-w-sm">
      <FieldLabel>Guests</FieldLabel>
      <FieldControl>
        <Counter min={0} max={8} step={1} defaultValue={2} />
      </FieldControl>
      <FieldDescription>How many people are staying.</FieldDescription>
    </Field>
    <Field className="w-full max-w-sm">
      <FieldLabel>Guests</FieldLabel>
      <FieldControl>
        <Counter size="lg" min={0} max={8} step={1} defaultValue={2} />
      </FieldControl>
      <FieldDescription>How many people are staying.</FieldDescription>
    </Field>
    <Field className="w-full max-w-sm">
      <FieldLabel>Guests</FieldLabel>
      <FieldControl>
        <Counter size="xl" min={0} max={8} step={1} defaultValue={2} />
      </FieldControl>
      <FieldDescription>How many people are staying.</FieldDescription>
    </Field>

    Examples

    Guests

    How many people are staying.

    import { Counter } from "@/components/ui/counter"
    import { Field, FieldControl, FieldDescription, FieldLabel } from "@/components/ui/field"
    
    <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.

    import { Counter } from "@/components/ui/counter"
    
    <Counter defaultValue={1} min={0} max={99} decrementLabel="Remove one" incrementLabel="Add one" />

    Disabled

    import { Counter } from "@/components/ui/counter"
    import { Field, FieldControl, FieldLabel } from "@/components/ui/field"
    
    <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

    NameTypeValuesDefaultDescription
    valuenumber--The value when you control it.
    defaultValuenumber-0The 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.
    minnumber-0Lowest value. The minus button and ArrowDown stop here.
    maxnumber--Highest value. Must be greater than min. The plus button and ArrowUp stop here.
    stepnumber-1Amount per step. Must be greater than 0.
    sizeenumsm | md | lg | xlmdControl height from size-element-sm/md/lg/xl. Pick per region; md is the default.
    disabledboolean-falseNo typing, no buttons, disabled look.
    readOnlyboolean-falseThe value can be read and copied but not changed. The step buttons are hidden.
    invalidboolean-falseSets aria-invalid and the danger border.
    requiredboolean-falseNative required on the spinbutton. Not generated for iOS and Android.
    namestring--Form name. The submitted value is the number, on a hidden input. Not generated for iOS and Android.
    decrementLabelstring-DecreaseName of the minus button.
    incrementLabelstring-IncreaseName of the plus button.

    Anatomy

    PartDescription
    rootThe 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.
    decrementThe minus button at the start (Tabler minus, 16px, 20px at xl).
    inputThe text spinbutton in the middle, centered, with tabular numbers.
    incrementThe plus button at the end (Tabler plus, 16px, 20px at xl).
    decrementIconThe minus mark inside the minus button, in the icon color, 16px (20px at xl).
    incrementIconThe plus mark inside the plus button, in the icon color, 16px (20px at xl).

    Accessibility

    Role: spinbutton

    KeysAction
    ArrowUp / ArrowDownIncrease or decrease by step, clamped to min and max.
    Home / EndSet min, or max when max is set.
    EnterCommit the typed text: parse, clamp and round to step. Text that does not parse is dropped and the previous value stays.
    TabMoves 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 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 for requirements and the alternative registry setup.