Skip to content

Slider

Adjust a value or range along a track.

import { Slider } from '@youj/react'

Installation & usage

npm i @youj/react

For an app already set up with npx @youj/cli init, which installs the peers (@youj/core, @youj/motion, motion and Base UI) and wires the provider. New app? Start at Installation.

Use Slider to adjust a value within a range, such as a threshold or zoom level. Show the current value beside it. Add an Input, or use one instead, when people need to enter an exact number.

Inside a Field, the FieldLabel names the thumb.

import { Field, FieldLabel, Slider } from '@youj/react'

export function Volume() {
  return (
    <Field>
      <FieldLabel>Volume</FieldLabel>
      <Slider defaultValue={60} />
    </Field>
  )
}

min and max default to 0 and 100. step sets the increment, and format takes Intl.NumberFormat options, so the value is announced as "$400" rather than "400". Use onValueChange for what follows the drag, like a readout, and Base UI's onValueCommitted for work that should wait until the thumb is released, like a save.

Reference

Propsaria-label, thumbLabels, getAriaValueText, variant, tone, ticks, sound (7)

Props

Slider props
PropType
aria-labelstring

Names the thumb. Base UI names a thumb from a Field/Slider label, never from the root's aria-label, so it is forwarded to the thumb here.

–
thumbLabelsstring[]

One name per thumb for a range, e.g. ['Minimum price', 'Maximum price'].

–
getAriaValueText(formattedValue: string, value: number, index: number) => string

What a screen reader says for a thumb's value when format can't say it, e.g. with a unit.

–
variant'default' | 'liquid' | 'ticks'

liquid fills a tube with a liquid whose surface sloshes as the value changes, leaning with the drag's speed and settling on a spring. ticks reads the value off a ruler of fine ticks, lit up to the thumb (horizontal only).

default 'default'

'default'
toneSliderTone
'accent''neutral''success''warning''danger''chart-1''chart-2''chart-3''chart-4''chart-5'

The fill's color in the default and ticks variants: the accent, a chart color, a status or neutral.

default 'accent'

'accent'
ticksnumber

With variant="ticks": how many ticks the ruler has, ends included. Default: one per step while that's at most 61, else 41.

–
soundboolean

With sound on (YoujProvider’s sound), the liquid slider plays tick as its value changes. false keeps this one silent.

default true

true
Other props pass through: extends SliderPrimitive.Root.Props.
Data attributeValues
data-variantdefault, liquid, ticks
data-tonethe tone
data-orientation, data-dragging, data-disabledBase UI's state

It also takes Base UI's value, defaultValue, onValueChange, onValueCommitted, min, max, step, largeStep, minStepsBetweenValues, format, locale, orientation, disabled and name.

MotionThumb, halo · reduced motion

Motion

Motion tokens
MomentTokens
Thumbtracks the pointer 1:1, and keyboard steps land at once
no token

no easing

no easing
Halogrows behind the thumb on hover, focus and drag
duration.fastease.enter

140ms · cubic-bezier(0.16, 1, 0.3, 1)

140mscubic-bezier(0.16, 1, 0.3, 1)

Reduced motion

The halo fades in without growing. The thumb follows the pointer and the keyboard exactly as before.

A slider is direct manipulation, so the thumb never eases toward the pointer. The motion is in the halo, a disc that grows behind the thumb on transform and opacity.

Liquid

Motion tokens
MomentTokens
Leanthe surface tilts back from the thumb with the value’s speed
spring.snappy

0.2s, bounce 0

0.2s, bounce 0
Sloshonce the value rests, the surface swings past level and settles
spring.settle

0.45s, bounce 0.3

0.45s, bounce 0.3
Swellthe middle of the surface bulges with the speed of the lean, a second wave on the first
spring.settle

0.45s, bounce 0.3

0.45s, bounce 0.3

Reduced motion

The surface stays level. The thumb and the fill follow the value exactly as on the plain slider.

The thumb and the length of the fill stay direct; only the liquid's surface moves. With sound on, the liquid slider plays tick as its value changes (the sound pack keeps a fast drag from repeating it), and sound={false} keeps one silent.

Ticks

The lit ticks follow the value with no easing, like the thumb; only the halo moves.

Reduced motion

The halo appears without scaling. Everything else is already direct: the thumb moves with the pointer, and the value changes as it moves. The liquid's surface stays level.

AccessibilityKeyboard, ARIA

Accessibility

  • Each thumb is a slider with aria-valuenow, and aria-valuetext formatted with format.
  • The arrow keys step. Page Up, Page Down and Shift with an arrow take large steps, and Home and End jump to the ends.
  • Name it with a FieldLabel, with aria-label for a single thumb, or with thumbLabels for a range.
  • The liquid and the ruler's ticks are decorative and hidden from assistive tech; the thumb's value says the same thing.
  • The thumb has a 32px hit area, and 40px on touch screens. Under right-to-left, the range fills from the right and the arrow keys follow the reading direction.