Design System

Design System

Quantity

Forms

Open in Storybook: Quantity (opens in a new tab)

On this page

Overview

A compact control for a number the user steps through, like the items of a cart. It is one native input (type="number", role="spinbutton", inputmode="numeric") between two buttons that decrease and increase it.

The input holds the role, the name, the value and the range, so the buttons are a convenience and not tab stops.

Live example

0 to 10

Anatomy

  1. Label

    label for

    Names the input. In a list it includes the product.

  2. Decrease button

    button type="button" tabindex="-1" aria-controls

    Pointer target outside the tab order. Disabled at the minimum.

  3. Input

    input type="number" role="spinbutton"

    Holds the value and the range. The only tab stop.

  4. Increase button

    button type="button" tabindex="-1" aria-controls

    Pointer target outside the tab order. Disabled at the maximum.

  5. Help text

    small + aria-describedby

    The allowed range. Always present.

  6. Error message

    small role="alert" + aria-describedby

    Rendered only while the value is invalid.

Usage

Props

Props
PropDoesDefault
labelNames the input. Always visible. In a list it includes the productRequired
min · max · stepThe range and the size of a step0 · 10 · 1
defaultValueThe value it starts withmin
valueTextThe value in words, (value) => string, for aria-valuetextThe number
helpThe range, said before the user errs"0 to 10"
errorWhat to fix, said while the value is invalid"Must be between 0 and 10"
decreaseLabel · increaseLabelThe names of the two buttons"Decrease quantity" · "Increase quantity"
nameThe name of the field in a form—
onChangeThe value, or null while the field is empty—

Example

valueText is what the input says on every change. The value stays as typed: out of range or empty it is invalid, never clamped or turned into 0.

import { Quantity } from '@/components/Quantity'

<Quantity
  label="Add Product B to bag"
  defaultValue={3}
  valueText={(value) => `${value} in cart`}
/>

Specs

Measurements

Measurements
PartPropertyValue
StackGap6px (0.375rem)
LabelText13px (0.8125rem) / 1.45, 700
RowGap · padding8px · 6px
ButtonSize44 × 44px (2.75rem). WCAG 2.5.8 minimum 24 × 24px
ButtonIcon20 × 20px (1.25rem)
InputHeight · width44px · max(4.5rem, 4ch + 1.5rem)
InputText18px (1.125rem) / 1.2, 700, centred. 16px minimum, below it iOS Safari zooms on focus
Button, inputBorder · corner2px solid · 8px
InputFocus2px gap, 3px ring
InputInvalid border3px
HelpText13px (0.8125rem) / 1.4
ErrorText · icon13px (0.8125rem) / 1.4, 600 · 16px icon, 6px gap

States

States
StateConditionAppearanceExposed
DefaultValue within the rangeBoth buttons enabledaria-valuetext="3 in cart"
FocusKeyboard focus on the input2px gap and 3px ring. Buttons get it on click—
At minValue equals minDecrease dashed and mutedDecrease disabled
At maxValue equals maxIncrease dashed and muted. Focus moves to the inputIncrease disabled
Out of rangeTyped value outside the range3px red edge, error text and icon. Value kept as typedaria-invalid="true", aria-describedby="qty-help qty-error", error role="alert"
EmptyField clearedAs out of range. valueAsNumber is NaNAs out of range

Layout

The input is sized in ch and the breakpoints are in rem, so the layout follows zoom and text size. One DOM, only the CSS changes.

Layout
BreakpointCSS widthReached byLayout
≥ 48rem≥ 768pxDesktop at 100–150% zoomOne row: product · quantity + help · price
30–48rem480–767px1280px at 200% (640 CSS px) · tablets · text at 200%Product and price on one row. Quantity and help below
< 30rem< 480px1280px at 400% (320 CSS px) · phonesStacked: product, price, label, quantity, help. No sideways scroll

Attributes

Attributes
AttributeOnPurposeSource
type="number"inputArrow keys, swipe up and down, min / max / step, valueAsNumbernative
role="spinbutton"inputWebKit 26.4 exposes a number input as a textboxyou · measured
inputmode="numeric"inputDigit keyboard. No effect on role, keys or validationyou · measured
min · max · step · valueinputThe range. Chromium exposes valuemin and valuemax from themnative · measured
aria-valuetextinputThe value in words. Without it VoiceOver and TalkBack read a percentage. Chromium ignores it on inputsyou · measured
aria-describedbyinputThe help id, plus the error id while invalidyou
aria-invalid="true"inputOnly while the value is invalidyou
autocomplete="off"inputNo autofill suggestionsyou
type="button" · aria-controls · name · tabindex="-1"buttonsNamed by action, linked to the input, outside the tab orderyou
aria-valuenow · aria-valuemin · aria-valuemaxnonearia-valuenow overrides the real value: 3 shown, 7 announcedmeasured

Markup

<label for="qty">Add Product B to bag</label>
<button type="button" tabindex="-1" aria-controls="qty" aria-label="Decrease quantity" disabled>−</button>
<input id="qty" type="number" role="spinbutton" inputmode="numeric" min="0" max="10" value="0"
       aria-valuetext="0 in cart" aria-describedby="qty-help qty-error">
<button type="button" tabindex="-1" aria-controls="qty" aria-label="Increase quantity">+</button>
<small id="qty-help">0 to 10</small>
<small id="qty-error" role="alert"></small>

Tokens

Component tokens

Component tokens
PartPropertyTokenValue
Button, inputBorder widthbw2px
Button, inputCornerradius8px
Button, inputBorder colourink#16161a
Button, inputFillwhite#ffffff
ButtonIcon colourink#16161a
Button, disabledBorder and icon colour—#8c8c94
Button, disabledFill—#f7f7fa
InputFocus gapwhite#ffffff, 2px
InputFocus ringlilac#5e548e, 3px
InputInvalid border widthbw-thick3px
InputInvalid border colourdanger#82002a
LabelText colourink#16161a
HelpText colourink-soft#4b4b55
ErrorText and icon colourdanger#82002a

Guidelines

Content

Content
PartFormatExample
LabelWhat is counted. In a list it includes the product"Add Product B to bag"
Value textThe count and what is counted. Words at zero"3 in cart" · "2 adults" · "No children"
HelpThe allowed range, visible before anything goes wrong"0 to 10" · "1 to 4 per order"
ErrorThe allowed range, never "Invalid""Must be between 0 and 10"
Button nameThe action, or what is counted"Decrease quantity" · "Fewer adults"

Behavior

Behavior
EventResult
Any change of the valuearia-valuetext updated in the same tick
Button pressValue ± step, clamped to the range. The new value is announced
Typing within the rangearia-valuetext updated on input
Typing outside the rangeValue kept as typed. On change: aria-invalid, error shown. Nothing is clamped
Empty fieldvalueAsNumber is NaN. Error shown, never coerced to 0
Button reaches a limitButton disabled. Focus moves to the input
Several rowsOne tab stop per row, the input

Alternatives

Alternatives
ApproachWebKit roleArrows · swipeWired by handVerdict
<input type="number" role="spinbutton" inputmode="numeric"> + aria-valuetextspinbuttonNativeHome / End, valuetext, validationUsed
<input type="number"> without a roletextbox (measured)NativeSameVoiceOver cannot treat it as a spinbutton
APG type="text" role="spinbutton" aria-valuenowspinbuttonBy handString ↔ number, every key, every ARIA valueFights the role
<input type="text" inputmode="numeric">textboxNone—Identifiers: phone, ZIP, card, PIN

Accessibility

Keyboard

Keyboard
KeyActionNative
Up / Down ArrowValue ± stepYes
HomeSet to minNo
EndSet to maxNo
Cmd + Left / RightSet to min / max, for Mac keyboards without Home / EndNo
TabCommit and leaveYes

Roles and properties

Roles and properties
PropertyExposedNote
RolespinbuttonExplicit, so WebKit matches
Name"Add Product B to bag"From the label
Value"0 in cart"VoiceOver and TalkBack. Chromium exposes 0 (measured)
Rangemin 0 · max 10From the native attributes
Description"0 to 10"Plus the error while invalid
Tab stops1The input. The buttons stay in the tree for touch and screen readers

Announcements

The input announces its own value while it has focus. A change from elsewhere, or a rejected value, is said separately. The component says a button press once with ariaNotify(), from the pressed button; where it is missing nothing is said and nothing replaces it. At a limit focus moves to the input, which says its own value, so nothing is announced. The calls are tested, not the speech: nothing here was heard in a screen reader yet.

Announcements
ActionSaid byAnnounced
Arrow Uparia-valuetext"4 in cart"
EndHome / End + aria-valuetext"10 in cart"
Press +ariaNotify() from the button"Increase quantity, 5 in cart"
Type 99, Tabrole="alert""Must be between 0 and 10"
Clear fieldrole="alert""Must be between 0 and 10"
Fix to 6aria-valuetext"6 in cart" · error removed

Measured behavior

Chromium 148 and WebKit 26.4.

Measured behavior
ObservationConsequence
Home and End do nothing to the valueA keydown handler sets min and max
An empty field has a valueAsNumber of NaNThe error is shown, the value is not turned into 0
Typing 99 with max="10" leaves 99, rangeOverflow is true, ArrowUp does not clampValidate on change. Clamp only in the buttons and in Home / End
A plain type="number" is a textbox in WebKitrole="spinbutton"
aria-valuenow="7" with the value 3 announces 7Never set

Support

Source: the post "Accessible and Functional Quantity Spinbutton Pattern". Every combination worked with role="spinbutton".

Support
BrowserPlatformsScreen readers
Chrome 148macOS · iOS · Windows · AndroidVoiceOver · NVDA · TalkBack
Firefox 150Windows · macOSNVDA · VoiceOver
Safari 26.4macOS · iOSVoiceOver

WCAG criteria

WCAG criteria
CriterionRequirement here
1.3.1 Info and Relationships (A)<label for>; help and error tied with aria-describedby
1.4.1 Use of Color (A)The invalid state is text and an icon as well as colour
1.4.3 Contrast (Minimum) (AA)Label, value, help and error text at 4.5:1
1.4.4 Resize Text · 1.4.10 Reflow (AA)No clipped value, no sideways scroll at 200% and 400%
1.4.11 Non-text Contrast (AA)Input border, button icons and focus ring at 3:1
2.1.1 Keyboard (A)Arrows step; Home and End jump; Cmd + arrows on Mac keyboards
2.4.7 Focus Visible (AA)A visible ring on the input and on the buttons
2.5.3 Label in Name (A)Any visible text on a button is inside its accessible name
2.5.8 Target Size (Minimum) (AA)The tabindex="-1" buttons are pointer targets of at least 24 × 24 CSS px
3.3.1 Error Identification (A)The field in error is named and the error is text
3.3.2 Labels or Instructions (A)The range ("0 to 10") is stated before the user errs
3.3.3 Error Suggestion (AA)The error says how to fix it
4.1.2 Name, Role, Value (A)Explicit role="spinbutton", named buttons, aria-valuetext
4.1.3 Status Messages (AA)role="alert" on the error; the new value is announced after a button press

Related

This section is still in progress.

References

Read the post: Accessible and Functional Quantity Spinbutton Pattern (opens in a new tab)