Skip to content
TopClass UI

Slider

A single value or a range of two. Arrow keys and a press on the track give the same result as every drag.

client - size not measured yet

Slider

client - size not measured yet

A single value or a range of two. Arrow keys and a press on the track give the same result as every drag.

Install

Try it

size
invalid
fullWidth
showValue
min
max
step
largeStep
minStepsBetweenValues

The code

slider.tsx
import { Slider } from 'topclassui/slider'

<div className="grid justify-items-start gap-2">
  <span id="slider-playground-label" className="text-sm text-body">Volume</span>
  <Slider aria-labelledby="slider-playground-label" defaultValue={60} />
</div>

A hidden range input carries the value, so the slider submits with its form; the thumb moves once the page hydrates.

Every state

4 states

Import from the package

topclassui/slider
import { Slider } from 'topclassui/slider'

Install once:npm i topclassui@beta zod

Examples

  • For a number picked by feel inside known limits. For an exact number, a NumberField.
  • min, max and step are 0, 100 and 1, clamped to plus or minus 1e15; largeStep is never under the step, so Page Up always moves.
  • minStepsBetweenValues keeps the two thumbs of a range apart; they never cross.
  • showValue writes the value after the track, in the provider's locale and digits. A long value moves below the track.
  • The width is an Input's of the same size; fullWidth fills the parent. A slider always has a value, so required is accepted and dropped.
basic
Volume
range
Price
show-value
40
$200 – $800
form
Volume
sizes
In Arabic, right to left
مستوى الصوت
30
نطاق السعر

Props

The props of Slider
PropTypeDefaultLimitsDescription
aria-labelstring--The name read out when no visible text names the group.
aria-labelledbystring--The id of the visible text that names the group.
classNamestring--Merged last, so a consumer class wins a conflict with the slider's own.
fullWidthbooleanfalse-Fills its parent instead of taking the width of an Input of its size.
invalidbooleanfalse-Marks the value as wrong: sets aria-invalid on the group and on each range input, and the fill, the knob and the line around the track turn to danger.
largeStepnumber, min 0.000001, max 100000000000000010min 0.000001, max 1000000000000000What Page Up, Page Down and Shift with an arrow move; clamped to at least the step, so they always move.
maxnumber, min -1000000000000000, max 1000000000000000100min -1000000000000000, max 1000000000000000The highest value; clamped to plus or minus 1e15.
minnumber, min -1000000000000000, max 10000000000000000min -1000000000000000, max 1000000000000000The lowest value, and the value of a slider with no default; clamped to plus or minus 1e15.
minStepsBetweenValuesnumber, min 0, max 10000000000000000min 0, max 1000000000000000The fewest steps between the two thumbs of a range; clamped to at least 0, so the thumbs never cross.
refRef<HTMLDivElement>--The root element, the <div role="group">.
requiredboolean--Accepted so a Field can spread its object, and dropped: Base UI has none, and a slider always has a value.
showValuebooleanfalse-Writes the value after the track, in the locale and the digits of the provider.
sizexs | sm | md | lg | xl | custom"md"-The scale of the track and the knob, and the width of an Input of that size; custom emits no size classes.
stepnumber, min 0.000001, max 10000000000000001min 0.000001, max 1000000000000000What one arrow key moves; clamped to at least 0.000001, since a step of 0 would never move.

Keyboard and accessibility

The keyboard of Slider
KeysWhat it does
TabMoves the focus to a thumb; each thumb of a range is a tab stop.
ArrowRightArrowUpAdd one step.
ArrowLeftArrowDownTake one step away.
PageUpPageDownMove by the large step, 10 by default; so does Shift with an arrow.
HomeEndGo to the minimum and the maximum, or to the other thumb of a range.
  • Name it with aria-label or aria-labelledby: what takes the focus is a range input inside the thumb. The two thumbs of a range are named Minimum and Maximum.
  • Every drag has a keyboard path (WCAG 2.5.7). The thumb is the target: 24px, 44px under a touch pointer.
  • invalid sets aria-invalid on the group and on each range input; the fill, the knob and the line around the track turn to danger.

Right to left

Under a right-to-left TopClassProvider the track runs from the right: ArrowLeft adds a step, and a value of 30 sits 30% in from the right.

Server and client

A client component: it renders on the server and hydrates in the browser.

Files: index.tsx, client.tsx.

On the chip: client - size not measured yet.