oakoss/ui
Components

Slider

A control for picking a value, or a range, along a track.

SourceReact Aria
40
Applies to every speaker.

Installation

npx shadcn@latest add @oakoss/slider

Usage

import { Slider } from '#/components/ui/inputs/slider';

<Slider defaultValue={40} label="Volume" />;

Examples

Range

An array value or defaultValue gives a thumb per value, and thumbLabels names each one. The output formats the range with formatOptions.

$200 – $800

Vertical

orientation="vertical" stands the track upright. The track is at least 10rem tall and fills any height you give the slider beyond that.

60

Custom layout

children replace the layout. Compose FieldLabel, SliderOutput, SliderTrack and FieldDescription in any order; they still wire up. A range still takes thumbLabels on the Slider.

Of the selected layer.25%

Accessibility

React Aria renders a group named by the label, with a visually hidden <input type="range"> in each thumb, so screen readers announce each thumb as a slider with its formatted value. Pressing the label focuses the first thumb, and the output isn't announced as it changes, since each thumb already reads its value. Follows the WAI-ARIA Slider and Multi-Thumb Slider patterns.

  • Thumb names: a range's thumbs are named by their thumb label and the slider's label ("Minimum Price"). The type requires thumbLabels whenever the value may be a range, with or without children, though it can't check there's a name per thumb. A generic wrapper (SliderProps<T>) has to pin T to spread its props into Slider.
  • Description: React Aria's Slider has no description slot, so the slider links description, or a FieldDescription in its children, to every thumb, ahead of any aria-describedby of your own, and drops the link when the description unmounts.
  • Target size: the track takes presses in a 44px band centered on the rail, and moves the nearest thumb to the point pressed.
  • Contrast: the rail and fill each reach 3:1 against the page. In Windows High Contrast the fill takes the system's highlight color and the rail thins to an outline, so the fill stands out by shape too.
  • Disabled: the label, output and track fade, and the group is marked aria-disabled as well as the inputs, so the whole slider is exposed as disabled and contrast checkers treat the faded text as inactive, which WCAG exempts from its minimum.
KeyAction
Arrow keysMove the focused thumb one step; with Shift, one page
Page Up / Page DownMove the focused thumb a page, a tenth of the range
Home / EndMove the focused thumb to its minimum or maximum
TabMove to the next thumb

Right to left, Left and Right arrows move the other way.

API

Slider

Extends React Aria's Slider. With children, it renders only them.

Props

Prop

Type

Also accepts all React Aria Slider props.

Data attributes

AttributePresent when
data-slotAlways slider
data-orientationAlways; horizontal or vertical
data-disabledisDisabled is set

SliderTrack

Extends React Aria's SliderTrack: the press area, which draws the rail, the fill and a thumb per value, named by the Slider's thumbLabels. Accepts React Aria SliderTrack props.

Data attributes

AttributePresent when
data-slotAlways: slider-track on the track, slider-rail, slider-range (the fill), slider-thumb
data-orientationOn the track and fill; horizontal or vertical
data-disabledThe slider is disabled
data-hoveredOn the track, fill or a thumb, while a pointer hovers it
data-draggingOn a thumb, while it's dragged
data-focus-visibleOn a thumb, while it has keyboard focus

SliderOutput

Extends React Aria's SliderOutput, which shows the value, or the range, formatted for the locale.

Data attributes

AttributePresent when
data-slotAlways slider-output
data-disabledThe slider is disabled