Playground

Slider

Pick a number, or a range, by dragging or with the keyboard.

  • Built on radix-ui/Slider
  • Each thumb is role=slider with aria-valuenow, min and max.
  • Arrow keys step; Home and End jump to the ends; PageUp and PageDown take larger steps.
  • aria-label is required; a range labels its thumbs minimum and maximum.
  • Styles in one file: slider.css

Import

import '@monoline/css';
import {Slider} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add slider

It needs radix-ui; install it if your project does not have it:

npm install radix-ui

Usage

import {Slider} from '@monoline/react';

<div style={{display: 'grid', gap: 'var(--s5)', maxWidth: 420}}>
  <Slider aria-label="Volume" defaultValue={60} />
  <Slider aria-label="Price" defaultValue={[20, 70]} />
</div>

Examples

Single and range

Props

SliderProps

PropTypeDefaultDescription
valuenumber | [number, number]–A number for one thumb, or [low, high] for a range.
defaultValuenumber | [number, number]–The starting value when uncontrolled: a number for one thumb or `[low, high]` for a range. Default 0.
onValueChange((value: number[]) => void)–Called with an array holding the value of each thumb while the user drags or uses the keyboard.
aria-label requiredstring–Accessible name for each thumb. One string for a single thumb, or [low, high] labels for a range.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[data-disabled]slider.css
[data-state="on"]slider.css

CSS classes

ClassStylesheet
.sliderslider.css
.slider-rangeslider.css
.slider-thumbslider.css
.slider-trackslider.css
.toggleslider.css
.toggle-groupslider.css
.progress12-range.css
.range12-range.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.slider {
  border-color: var(--accent);
}
.slider[data-disabled] {
  background: var(--wash);
}

Try the props

Change a prop and the component above updates; the code below is what you would write. To edit the code itself, use the Playground.

example.tsx
<Slider aria-label="Volume" defaultValue={40} />

Accessibility

  • Each thumb is role=slider with aria-valuenow, min and max.
  • Arrow keys step; Home and End jump to the ends; PageUp and PageDown take larger steps.
  • aria-label is required; a range labels its thumbs minimum and maximum.

Keyboard

KeysAction
Arrow keysStep
Home / EndJump to the ends
PageUp / PageDownTake larger steps