Quantity
Forms
Open in Storybook: Quantity (opens in a new tab)
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
- Label
label forNames the input. In a list it includes the product.
- Decrease button
button type="button" tabindex="-1" aria-controlsPointer target outside the tab order. Disabled at the minimum.
- Input
input type="number" role="spinbutton"Holds the value and the range. The only tab stop.
- Increase button
button type="button" tabindex="-1" aria-controlsPointer target outside the tab order. Disabled at the maximum.
- Help text
small+aria-describedbyThe allowed range. Always present.
- Error message
small role="alert"+aria-describedbyRendered only while the value is invalid.
Usage
Props
| Prop | Does | Default |
|---|---|---|
label | Names the input. Always visible. In a list it includes the product | Required |
min · max · step | The range and the size of a step | 0 · 10 · 1 |
defaultValue | The value it starts with | min |
valueText | The value in words, (value) => string, for aria-valuetext | The number |
help | The range, said before the user errs | "0 to 10" |
error | What to fix, said while the value is invalid | "Must be between 0 and 10" |
decreaseLabel · increaseLabel | The names of the two buttons | "Decrease quantity" · "Increase quantity" |
name | The name of the field in a form | — |
onChange | The 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
| Part | Property | Value |
|---|---|---|
| Stack | Gap | 6px (0.375rem) |
| Label | Text | 13px (0.8125rem) / 1.45, 700 |
| Row | Gap · padding | 8px · 6px |
| Button | Size | 44 × 44px (2.75rem). WCAG 2.5.8 minimum 24 × 24px |
| Button | Icon | 20 × 20px (1.25rem) |
| Input | Height · width | 44px · max(4.5rem, 4ch + 1.5rem) |
| Input | Text | 18px (1.125rem) / 1.2, 700, centred. 16px minimum, below it iOS Safari zooms on focus |
| Button, input | Border · corner | 2px solid · 8px |
| Input | Focus | 2px gap, 3px ring |
| Input | Invalid border | 3px |
| Help | Text | 13px (0.8125rem) / 1.4 |
| Error | Text · icon | 13px (0.8125rem) / 1.4, 600 · 16px icon, 6px gap |
States
| State | Condition | Appearance | Exposed |
|---|---|---|---|
| Default | Value within the range | Both buttons enabled | aria-valuetext="3 in cart" |
| Focus | Keyboard focus on the input | 2px gap and 3px ring. Buttons get it on click | — |
| At min | Value equals min | Decrease dashed and muted | Decrease disabled |
| At max | Value equals max | Increase dashed and muted. Focus moves to the input | Increase disabled |
| Out of range | Typed value outside the range | 3px red edge, error text and icon. Value kept as typed | aria-invalid="true", aria-describedby="qty-help qty-error", error role="alert" |
| Empty | Field cleared | As out of range. valueAsNumber is NaN | As 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.
| Breakpoint | CSS width | Reached by | Layout |
|---|---|---|---|
| ≥ 48rem | ≥ 768px | Desktop at 100–150% zoom | One row: product · quantity + help · price |
| 30–48rem | 480–767px | 1280px at 200% (640 CSS px) · tablets · text at 200% | Product and price on one row. Quantity and help below |
| < 30rem | < 480px | 1280px at 400% (320 CSS px) · phones | Stacked: product, price, label, quantity, help. No sideways scroll |
Attributes
| Attribute | On | Purpose | Source |
|---|---|---|---|
type="number" | input | Arrow keys, swipe up and down, min / max / step, valueAsNumber | native |
role="spinbutton" | input | WebKit 26.4 exposes a number input as a textbox | you · measured |
inputmode="numeric" | input | Digit keyboard. No effect on role, keys or validation | you · measured |
min · max · step · value | input | The range. Chromium exposes valuemin and valuemax from them | native · measured |
aria-valuetext | input | The value in words. Without it VoiceOver and TalkBack read a percentage. Chromium ignores it on inputs | you · measured |
aria-describedby | input | The help id, plus the error id while invalid | you |
aria-invalid="true" | input | Only while the value is invalid | you |
autocomplete="off" | input | No autofill suggestions | you |
type="button" · aria-controls · name · tabindex="-1" | buttons | Named by action, linked to the input, outside the tab order | you |
aria-valuenow · aria-valuemin · aria-valuemax | none | aria-valuenow overrides the real value: 3 shown, 7 announced | measured |
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
| Part | Property | Token | Value |
|---|---|---|---|
| Button, input | Border width | bw | 2px |
| Button, input | Corner | radius | 8px |
| Button, input | Border colour | ink | #16161a |
| Button, input | Fill | white | #ffffff |
| Button | Icon colour | ink | #16161a |
| Button, disabled | Border and icon colour | — | #8c8c94 |
| Button, disabled | Fill | — | #f7f7fa |
| Input | Focus gap | white | #ffffff, 2px |
| Input | Focus ring | lilac | #5e548e, 3px |
| Input | Invalid border width | bw-thick | 3px |
| Input | Invalid border colour | danger | #82002a |
| Label | Text colour | ink | #16161a |
| Help | Text colour | ink-soft | #4b4b55 |
| Error | Text and icon colour | danger | #82002a |
Guidelines
Content
| Part | Format | Example |
|---|---|---|
| Label | What is counted. In a list it includes the product | "Add Product B to bag" |
| Value text | The count and what is counted. Words at zero | "3 in cart" · "2 adults" · "No children" |
| Help | The allowed range, visible before anything goes wrong | "0 to 10" · "1 to 4 per order" |
| Error | The allowed range, never "Invalid" | "Must be between 0 and 10" |
| Button name | The action, or what is counted | "Decrease quantity" · "Fewer adults" |
Behavior
| Event | Result |
|---|---|
| Any change of the value | aria-valuetext updated in the same tick |
| Button press | Value ± step, clamped to the range. The new value is announced |
| Typing within the range | aria-valuetext updated on input |
| Typing outside the range | Value kept as typed. On change: aria-invalid, error shown. Nothing is clamped |
| Empty field | valueAsNumber is NaN. Error shown, never coerced to 0 |
| Button reaches a limit | Button disabled. Focus moves to the input |
| Several rows | One tab stop per row, the input |
Alternatives
| Approach | WebKit role | Arrows · swipe | Wired by hand | Verdict |
|---|---|---|---|---|
<input type="number" role="spinbutton" inputmode="numeric"> + aria-valuetext | spinbutton | Native | Home / End, valuetext, validation | Used |
<input type="number"> without a role | textbox (measured) | Native | Same | VoiceOver cannot treat it as a spinbutton |
APG type="text" role="spinbutton" aria-valuenow | spinbutton | By hand | String ↔ number, every key, every ARIA value | Fights the role |
<input type="text" inputmode="numeric"> | textbox | None | — | Identifiers: phone, ZIP, card, PIN |
Accessibility
Keyboard
| Key | Action | Native |
|---|---|---|
| Up / Down Arrow | Value ± step | Yes |
| Home | Set to min | No |
| End | Set to max | No |
| Cmd + Left / Right | Set to min / max, for Mac keyboards without Home / End | No |
| Tab | Commit and leave | Yes |
Roles and properties
| Property | Exposed | Note |
|---|---|---|
| Role | spinbutton | Explicit, so WebKit matches |
| Name | "Add Product B to bag" | From the label |
| Value | "0 in cart" | VoiceOver and TalkBack. Chromium exposes 0 (measured) |
| Range | min 0 · max 10 | From the native attributes |
| Description | "0 to 10" | Plus the error while invalid |
| Tab stops | 1 | The 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.
| Action | Said by | Announced |
|---|---|---|
| Arrow Up | aria-valuetext | "4 in cart" |
| End | Home / End + aria-valuetext | "10 in cart" |
| Press + | ariaNotify() from the button | "Increase quantity, 5 in cart" |
| Type 99, Tab | role="alert" | "Must be between 0 and 10" |
| Clear field | role="alert" | "Must be between 0 and 10" |
| Fix to 6 | aria-valuetext | "6 in cart" · error removed |
Measured behavior
Chromium 148 and WebKit 26.4.
| Observation | Consequence |
|---|---|
| Home and End do nothing to the value | A keydown handler sets min and max |
An empty field has a valueAsNumber of NaN | The error is shown, the value is not turned into 0 |
Typing 99 with max="10" leaves 99, rangeOverflow is true, ArrowUp does not clamp | Validate on change. Clamp only in the buttons and in Home / End |
A plain type="number" is a textbox in WebKit | role="spinbutton" |
aria-valuenow="7" with the value 3 announces 7 | Never set |
Support
Source: the post "Accessible and Functional Quantity Spinbutton Pattern". Every combination worked with role="spinbutton".
| Browser | Platforms | Screen readers |
|---|---|---|
| Chrome 148 | macOS · iOS · Windows · Android | VoiceOver · NVDA · TalkBack |
| Firefox 150 | Windows · macOS | NVDA · VoiceOver |
| Safari 26.4 | macOS · iOS | VoiceOver |
WCAG criteria
| Criterion | Requirement 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)