Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Slider

One approximate number in a range, such as a search distance or a volume. Lets someone choose an approximate number by dragging a handle or with the keyboard. It is the browser’s own range control, so the arrow keys, Home, End, Page Up, Page Down, dragging and sending a form work without extra code.

Package
@kvirn-ui/react
Pattern
APG Slider (single thumb)
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use a slider for an approximate value over a small range with a coarse step, where seeing the position helps: a search distance, a volume.
  • Put it in a Field, after the label, with the ends of the range in the help text. Give valueText the unit, so a screen reader (software that reads the screen aloud) says “15 km”, not “15”.
  • Not for an exact value, such as an amount or an age: use a NumberInput. A slider is never the only way to enter a number that matters: put a NumberInput beside it.

Example#

From 1 to 20 km.

Use cases#

A search distance in a plain form

Use it when the form needs only the number. Without value the browser keeps the number, and sending the form posts it under the slider’s name. A form reset puts back defaultValue.

From 1 to 20 km.

Props in this example:

A search distance with an exact box beside it

Dragging is hard for some people and imprecise for everyone, so give an exact way in: the slider for a rough choice, the number box for the exact distance. In the example the two stay in step.

From 1 to 20 km. You can also type a number.

Props in this example:

Information: Implement like this

Let the NumberInput own the Field. A click on the label then moves focus to the number box, and the error belongs to it. Name and describe the slider with aria-labelledby and aria-describedby, pointing at a span with an id inside the label and the help text. Give only the NumberInput a name, so the form sends the value once.

Recycling points that update as the distance changes

A list that changes while the thumb moves is a status message. The slider announces only its own value, so say the new count yourself, once, after the person stops. The example does this half a second after the last change, politely: the screen reader finishes what it is saying first.

From 1 to 20 km.

2 recycling points within 5 km

  • Harbour recycling centre, 2 km
  • Hill Road collection point, 4 km

Props in this example:

Warning: Don't forget

Announce the result with the Announcer, once and after a pause, never on every step (4.1.3 Status Messages, level AA). Don’t move focus or leave the page when the value changes (3.2.2 On Input, level A).

A distance that is off for now

When another choice makes the slider meaningless, disable it and say why in the help text. Here disabled is set on the Field, which disables the slider too, and Tab skips it.

From 1 to 20 km.

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: Slider, single thumb. The native form: <input type="range">, whose implicit role is slider and whose aria-valuemin, aria-valuemax and aria-valuenow are native.
  • Deviations: none from APG.
  • Native elements used: <input type="range">, named by a <label for> (Field.Label) or by aria-labelledby.

A Slider is for an approximate value (a search radius, a volume). It is never the only way to enter a number that matters: an exact value is a NumberInput, and a slider beside one gives a resident both. It is a native input, so the browser supplies every key, the drag, a press on the track, form submission, form.reset() and disabled. Slider adds the Field's wiring, aria-valuetext, the part class and data-* state. It holds no form state: pass value and onValueChange, or defaultValue and name for a plain form.

PartElement / roleARIA / stateNotes
Slider<input type="range">, implicit sliderNative value, min and max. aria-valuetext always. id, aria-describedby (help text, then the error) and aria-invalid="true" from the Field. Native disabled. data-invalid, data-disabled, data-focus-visibleClass kv-slider. Props: value, defaultValue, min (0), max (100), step (1), name, disabled, onValueChange(value, { reason: 'input', event }), valueText, aria-labelledby, native input props except type, role, required
Field.Label<label for>, the slider's nameThe name never changes with the valueA Slider that owns the Field, or a plain Field. A Slider beside a NumberInput: see the rules below
useSliderthe same attributes, for your own elementsinputProps, isInvalid, isDisabled, isFocusVisible, value, valueTextSpread inputProps on your <input>

Rules, each with an automated test:

  • A native range. type="range", class kv-slider, no role and no aria-valuenow written by the component, no aria-required.
  • aria-valuetext is always set. It is valueText(value), or the number formatted with the provider's locale. It follows the value, controlled or not. Say the unit in valueText (15 km).
  • Named by its label. In a Field the label's for matches the input's id. The help text describes it.
  • A Slider beside a NumberInput. An explicit aria-labelledby opts the Slider out of the Field: it gets no id, no aria-describedby, no aria-invalid and no data-invalid from it. The NumberInput owns the Field's id, the label's for and the error. The Field's label and help text take no id, so wrap their text in a <span id> and point aria-labelledby and aria-describedby at it. Pass aria-describedby yourself for the help text. A disabled Field still disables the Slider.
  • Controlled and uncontrolled. value is a number; onValueChange gets Number(event.currentTarget.value). A form submit sends name=value as a string; form.reset() restores defaultValue.
  • Disabled. A Field's disabled or the prop gives native disabled and data-disabled.
  • Dev warnings (once): a Slider in a Field with no Field.Label; a Slider outside a Field with no accessible name (aria-label or aria-labelledby count); an id inside a Field; a min that is not below max; a value or defaultValue outside the range.

It announces nothing.

What you need to do#

  • Name the quantity and its unit in the label ("Distance in kilometres"); never a label that changes with the value.
  • Say the unit in valueText ("15 km", "40 percent"): a bare number is right only when it has no unit.
  • State the ends of the range in the help text. Never "Drag the slider" as the only instruction.
  • Never the only way in for an exact number: pair it with a NumberInput beside it, where the NumberInput owns the Field and the Slider has aria-labelledby. A value with legal effect is a NumberInput alone. This is a KvirnUI rule for precision and for touch users without keys, not a WCAG requirement: dragging a native range is excepted from 2.5.7 Dragging Movements (AA), and its keys meet 2.1.1 Keyboard (A).
  • Results that update as the value moves are announced once, debounced, through the Announcer (4.1.3 Status Messages, AA), never per step. This is your code: the Slider announces nothing itself.
  • Updating results on input is fine, but never navigate or move focus on change (3.2.2 On Input, A).
  • A not-available slider is disabled, with the reason in the help text.

Warning: Don't forget

Name the quantity and its unit in the label, and give valueText the unit. Without a name a screen reader user hears only “slider” and can’t tell what it is for (4.1.2 Name, Role, Value). Without the unit they hear a bare number.

Visual and modes#

  • Focus indicator: a ring around the input box with at least 3:1 contrast against the colours next to it (2.4.7 Focus Visible, AA; 2.4.13 Focus Appearance, AAA).
  • Target size: the box is tall enough to press, and the thumb is at least 24px (2.5.8 Target Size (Minimum), AA). On a coarse pointer, such as a finger, the thumb's hit area is larger.
  • Colour: the thumb and the track edge each have at least 3:1 contrast against their background (1.4.11 Non-text Contrast, AA). An invalid slider has a heavier error-coloured edge plus the message. A disabled slider has a dashed edge and a muted thumb.
  • Forced colours (the system's own colour scheme): the track uses the system field colour with a button-border edge, the thumb uses the system highlight colour, a disabled slider is grey, and an invalid one has a heavier edge in the system text colour. The forced-colours sweep runs every story and checks the boundaries and the focus ring; the thumb and track colours themselves are checked by eye.
  • Reduced motion: the track's border colour changes gradually only when the person has not asked for reduced motion. The thumb never animates.
  • Right-to-left (RTL) pages: the browser reverses the range, and nothing in the theme depends on direction.

Warning: Don't forget

If you restyle the slider, keep the thumb and the track edge at least 3:1 against their background (1.4.11 Non-text Contrast). Keep the thumb at least 24 pixels across (2.5.8 Target Size (Minimum)).

Focus management#

  • Initial focus: not moved.
  • Trap: no.
  • Restore to: not applicable.
  • Never obscured by: the focus ring is drawn on the input's box, outside the track, and nothing around clips it (2.4.11 Focus Not Obscured (Minimum), AA; 2.4.13 Focus Appearance, AAA). Consumers with a sticky header set scroll-padding.

WCAG success criteria#

  • 1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A): native range, label for, aria-valuetext.
  • 1.4.11 Non-text Contrast (AA): the track edge, the thumb and the focus ring.
  • 1.4.10 Reflow (AA), 1.4.12 Text Spacing (AA): full width, the label wraps.
  • 2.1.1 Keyboard (A), 2.4.3 Focus Order (A): native Tab and the range keys.
  • 2.4.7 Focus Visible (AA), 2.4.11 Focus Not Obscured (Minimum) (AA), 2.4.13 Focus Appearance (AAA, not counted toward the AA bar).
  • 2.5.7 Dragging Movements (AA): dragging a native range is the user agent's own, unmodified behaviour, which the criterion excepts. The alternative is the keys, and, with a NumberInput beside it, that box.
  • 2.5.8 Target Size (Minimum) (AA): the box and the thumb.
  • 3.2.2 On Input (A): a slider never changes context.
  • 3.3.1 Error Identification (A), 3.3.2 Labels or Instructions (A): the Field's error and help text.

Keyboard#

Focus
native
Selection follows focus
n/a
Arrows wrap
no
Shortcuts
none

Each slider is one Tab stop. Nothing is intercepted: every key keeps its native behaviour.

KeyWhereWhat happens
Tabbefore the sliderMoves focus to the slider. One Tab stop
Test: slider.test.tsx › Tab moves to the slider, one stop
Shift+Tabon the sliderMoves focus to the previous focusable element
Test: slider.test.tsx › Shift+Tab moves to the previous focusable element
Tabdisabled sliderSkips it (native disabled leaves the Tab sequence)
Test: slider.test.tsx › Tab skips a disabled slider
ArrowRight / ArrowUpon the sliderIncreases by step
Test: slider.test.tsx › ArrowRight and ArrowUp increase by one step
ArrowLeft / ArrowDownon the sliderDecreases by step
Test: slider.test.tsx › ArrowLeft and ArrowDown decrease by one step
Homeon the sliderSets min
Test: slider.test.tsx › Home sets the minimum
Endon the sliderSets max
Test: slider.test.tsx › End sets the maximum
PageUpon the sliderIncreases by a larger step (a step the browser sets)
Test: slider.test.tsx › PageUp and PageDown move by a larger step
PageDownon the sliderDecreases by a larger step (a step the browser sets)
Test: slider.test.tsx › PageUp and PageDown move by a larger step
ArrowLeft / ArrowRightright-to-left pageThe browser reverses them: Left increases, Right decreases
Test: slider.test.tsx › in a right-to-left page the arrows are reversed

Arrows stop at min and max. The value is announced natively; there is no live region.

API reference#

import { Field, Slider, useSlider } from '@kvirn-ui/react'

Slider

Renders <input type="range">. It takes every attribute of an <input> except type, role, required and readOnly (a range always has a value), and passes ref to it. Put it in a Field.Root, after the Field.Label. The label, help text and error are the Field’s parts, and the slider has no strings of its own. The default theme is described on Theming.

PropTypeDefaultDescription
valuenumber–Controlled: the number from your form logic. Pair it with onValueChange. The slider never copies it into state of its own.
defaultValuenumberhalfwayUncontrolled: the browser keeps the number, and a form submit sends it.
minnumber0The lowest value.
maxnumber100The highest value.
stepnumber1The distance between values.
namestring–The name a form submit uses.
disabledbooleanfalseNative disabled: skipped by Tab. A disabled Field disables the slider too.
valueText(value: number) => stringthe number in the provider’s localeWhat aria-valuetext says. Give it the unit, such as “15 km”, or a screen reader says only the number.
onValueChange(value: number, details: SliderChangeDetails) => void–Called on every change with the new number and { reason: "input", event }. It only reports. onChange still works too.
aria-labelledbystring–Names the slider by another element, and opts it out of its Field: no id, description or invalid state from it. For a slider beside a NumberInput that owns the Field.
Class or attributeValuesMeaning
kv-slideralwaysThe part class. The default theme styles it.
aria-valuetextalwaysFrom valueText, or the number in the provider’s locale.
aria-describedbyidsFrom the Field: the help text, then the error, then your own ids. With aria-labelledby the Field adds none, and only your own ids are used.
data-invalidpresent or absentThe Field is invalid.
data-disabledpresent or absentThe Field or the disabled prop disables it.
data-focus-visiblepresent or absentThe slider has keyboard focus (:focus-visible).
aria-invalid"true" or absentSet when its Field is invalid.

useSlider

Options

PropTypeDefaultDescription
valuenumber–Controlled: the number from your logic.
defaultValuenumberhalfwayUncontrolled: the browser keeps the number.
minnumber0The lowest value.
maxnumber100The highest value.
stepnumber1The distance between values.
namestring–The name a form submit uses.
disabledbooleanfalseNative disabled.
onValueChange(value: number, details: SliderChangeDetails) => void–Called with the new number on every change. It only reports.
valueText(value: number) => stringthe number in the provider’s localeWhat aria-valuetext says.
aria-labelledbystring–Names the slider by another element and opts it out of its Field.

Result

PropTypeDefaultDescription
inputPropsSliderPartProps–Spread on an <input>: class, type, range, Field wiring, aria-valuetext, state attributes and handlers.
isInvalidboolean–Whether the Field is invalid.
isDisabledboolean–Whether the slider is disabled.
isFocusVisibleboolean–Whether the slider has keyboard (:focus-visible) focus.
valuenumber–The current number.
valueTextstring–What aria-valuetext says.

Known issues#

  • The thumb and track colours are checked by eye. They are parts the browser draws itself, styled separately for each browser engine, so the automated forced-colours check covers the boundaries and the focus ring but not these. Check them in Windows Contrast Themes yourself.
  • A value between steps. The browser snaps the thumb to the nearest step; beside a NumberInput, the NumberInput keeps the typed number.
  • form.reset() and aria-valuetext. For an uncontrolled slider the text follows the native value after each change and after a form reset.
  • A controlled value off the step or outside the range. The browser snaps the thumb and aria-valuenow, but aria-valuetext is built from the value you passed, so the two can disagree. Pass a value on a step and inside min and max.

Information: Did you know?

If an issue here affects your service, list it in your accessibility statement with its workaround.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassslider.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassslider.test.tsx › valueText sets aria-valuetext, and it follows each change; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass10 rows, each names a test (slider.test.tsx › Tab moves to the slider, one stop)
Focus order and managementpassn/apassslider.test.tsx asserts focus (toHaveFocus); first rule: Tab moves to the slider, one stop
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

  • Tab to the slider: you hear the label, "slider", the value with its unit and the help text.
  • Press Right or Up, then Left or Down: the value moves one step each time and the new value is spoken with its unit.
  • Press Home and End: the value jumps to the ends. Press Page Up and Page Down: it jumps by a larger step.
  • With a NumberInput beside it, type a number: the slider moves. Click the label: the number box is focused.
  • Disable the Field: Tab skips the slider and the help text says why.
  • On a phone with a screen reader, change the value without dragging: the value text is spoken, and the NumberInput works.