Slider

updated

Slide to unlock. Just kidding, slide to pick.

Demo

Open Props in the corner of the example to try variant, color, size, disabled and aria-invalid. A disabled slider turns grey and ignores the pointer and the keyboard. An invalid one turns its range and thumbs --destructive, whatever its color, and sets aria-invalid on each thumb.

size sets the handle, from 12px for xs to 24px for xl, and the track thickness with it, from 4px to 10px. md, the default, is a 16px thumb on a 6px track.

color draws the range and the thumbs in Button's colours or your own, primary by default. On the default track they sit on the page, so they take the colour's text shade, and the rest of the track is the colour at 20%. An inset thumb carries its handle, so it and the range keep the fill colour, and the track stays grey.

Installation

Usage

<script setup lang="ts">
import { Slider } from '@/components/ui/slider'

const volume = ref(50)
</script>

<template>
  <Slider v-model="volume" aria-label="Volume" />
</template>

v-model is a number for one thumb. Pass an array, and you get one thumb per item.

Click anywhere on the track, and the nearest thumb jumps there. The thumb looks and reacts like the Switch handle.

Give it an aria-label, or put it in a Field.

Set name, and the value submits with its form, one field per thumb named name[0], name[1] and so on, a single thumb included: a slider named volume posts volume[0]=50. Rails and PHP read that as an array.

Examples

Min, max and step

The value runs from min to max, 0 to 100 by default, in steps of step. When the number alone does not say what it means, set aria-valuetext, which screen readers read instead.

Min and max
21.0 °C

From 16 to 28 °C in steps of 0.5.

Multiple

Pass an array for a range, like a price filter. Each thumb gets a screen reader name on its own, so you don't need to label them one by one.

Multiple
$200 – $800

Min steps between thumbs

min-steps-between-thumbs keeps neighbouring thumbs that many steps apart. Without it, thumbs can meet and pass each other.

Min steps
9:00 – 18:00

At least 4 hours apart.

Orientation

orientation="vertical" runs the slider from bottom to top. It fills the height of its container and is at least 176px tall.

Orientation
60 Hz
250 Hz
1 kHz
4 kHz
16 kHz

Inset

variant="inset" puts the thumb inside a thick track, with the fill wrapping around it. The handle shrinks a little while you drag it.

Keep the default thumb-alignment="contain". With overflow, the fill and the handle drift apart near the ends.

Inset
30%

Inverted

Same slider, but in Australia. inverted flips it: right to left, or top to bottom. Arrow keys still move the thumb the way they point.

On a right-to-left page, a horizontal slider already runs right to left on its own.

Inverted

Touch target

Smaller than a finger? Add touch-target: expand when it stands alone, wrapper when it sits close to others. How it works.

The track and each thumb get their own area: press a thumb's to pick it up where it is, or the track's to pull the nearest thumb to your finger.

Touch target
none
expand
wrapper

Parts

Need a look the props don't cover? Lay the slider out yourself: a SliderTrack holding a SliderRange, and a SliderThumb per value, each with a SliderHandle inside unless you put something else there. Every part takes a class. The slot gives you thumbs and values, so a range can loop: <SliderThumb v-for="i in thumbs" :key="i" />.

Give each thumb of a range its own aria-label when the defaults, Minimum and Maximum, don't say enough.

Parts

API Reference

class is also accepted and merged over the base classes.

Data attributes

data-slotAttributesDescription
sliderdata-orientation, data-disabled, data-touch-target, data-variant, data-color, data-sizeThe root, and the pressable area of the track.
slider-trackdata-orientation, data-disabledThe full length of the track.
slider-rangedata-orientation, data-disabledThe filled part of the track.
slider-thumbdata-orientation, data-disabled, data-hoveredOne thumb, with role="slider". Draws the state layer, or, with inset, the disc around the handle.
slider-handle—The circle inside a thumb.

Changelog

  1. registry 0.24.1

    35a5371 — No component styles an element through a :has() on its parent or group any more. Chrome restyles the whole subtree of every :has() anchor above a node that is added or removed, once some rule puts :has() away from the element it styles: a page wrapped in one such anchor recalculated every element for each list row it mounted. The Dialog, Drawer and Tour headers, the InputGroup and InputNumber insets, the Combobox clear button, the InputFloating notch, a FieldLabel beside a disabled checkbox or radio, the Rating ring, and the Slider and ScrollArea examples now read their neighbours with ~ or a :has() on the element itself. The InputGroup addons fade through a variable that the group sets. FileUpload, Image, Marquee and DataTable mark the state on an element instead: the hidden input's focus, an ImageError inside, keyboard focus, and the body above pinned bottom rows. scripts/has-selectors.test.ts keeps the pattern out.

    useSwipeSnap no longer flushes styles for an instant move that leaves the offset where it is, such as new points at rest.

  2. core 0.17.1

    35a5371 — No component styles an element through a :has() on its parent or group any more. Chrome restyles the whole subtree of every :has() anchor above a node that is added or removed, once some rule puts :has() away from the element it styles: a page wrapped in one such anchor recalculated every element for each list row it mounted. The Dialog, Drawer and Tour headers, the InputGroup and InputNumber insets, the Combobox clear button, the InputFloating notch, a FieldLabel beside a disabled checkbox or radio, the Rating ring, and the Slider and ScrollArea examples now read their neighbours with ~ or a :has() on the element itself. The InputGroup addons fade through a variable that the group sets. FileUpload, Image, Marquee and DataTable mark the state on an element instead: the hidden input's focus, an ImageError inside, keyboard focus, and the body above pinned bottom rows. scripts/has-selectors.test.ts keeps the pattern out.

    useSwipeSnap no longer flushes styles for an instant move that leaves the offset where it is, such as new points at rest.

  3. registry 0.22.0

    ac0cc05 — Checkbox, Radio, Switch and Slider no longer declare as and asChild, as Rating already does: Reka's slot cloned the control's own indicator, thumb or track into the root, which left an inert control. The props are gone from their API tables too.

    750df71 — A Field or FieldSet inside a disabled FieldSet is disabled too, so a Slider, Rating or ColorPicker in its own Field stops taking focus and input while the form is locked. Only disabled passes down; invalid and required stay with the part that sets them.

    70d1620 — Colours written inside arbitrary values and variables now resolve where the element sits, so a .dark island (or any subtree that sets a token) gets its own: the filled Input, InputGroup and DatePicker underline, the Table hover, selected and stripe tints, the disabled Slider and Rating fills and the ColorPicker checkerboard read --theme(--color-x) instead of var(--color-x), which Tailwind resolves once on :root. The ripple wave reads --state-pressed where it is pressed (the ripple item no longer pins --kappa-ripple-opacity on :root), so a subtree that changes --state-pressed changes the wave as well as the state layer.

  4. core 0.16.0

    70d1620 — Colours written inside arbitrary values and variables now resolve where the element sits, so a .dark island (or any subtree that sets a token) gets its own: the filled Input, InputGroup and DatePicker underline, the Table hover, selected and stripe tints, the disabled Slider and Rating fills and the ColorPicker checkerboard read --theme(--color-x) instead of var(--color-x), which Tailwind resolves once on :root. The ripple wave reads --state-pressed where it is pressed (the ripple item no longer pins --kappa-ripple-opacity on :root), so a subtree that changes --state-pressed changes the wave as well as the state layer.

  5. registry 0.21.0

    e5495ef — size works the same way everywhere. Every component exports its size type (AlertSize, BadgeSize, CardSize, CheckboxSize, RadioSize, SwitchSize, EmptySize, InputFloatingSize, ItemSize, ListboxSize, SeparatorSize, SliderSize, StepperSize, TabsSize, TextareaSize, TimelineSize) and the props take it without null; Button-based parts take ButtonSize, Input TextControlSize, SelectTrigger SelectSize and Tree TreeSize. ToggleGroup and ToolbarToggleGroup carry data-size on the group as well. Table's row heights moved into tableSize, so a class on DataTable's ui.table overrides --table-row-h at every size, not only at md.

  6. registry 0.16.0

    18e317c — Slider's parts are components of their own: SliderTrack, SliderRange, SliderThumb and SliderHandle, each taking a class. Slider still draws them itself when given no children, so <Slider v-model> works as before. Lay them out in its slot, which gives { thumbs, values }, to restyle a part or to name each thumb of a range on its own.

  7. registry 0.13.0

    5a478d3 — Checkbox, Radio, Switch, Slider, Progress, Stepper and TabsList take color: primary (the default), neutral, destructive, success, warning, info, or any name you declare on [data-slot][data-color="<name>"], as on Button. The root carries data-color. Whatever they draw straight on the page takes the colour's --tone-text: a checked edge, the radio's dot, the default slider's range and thumb, the progress bar and the tab line. That keeps a bright success, warning or info at 3:1 against the page. Fills with something on them, like the checked box, the switch track and the active step, keep --tone. On the choice controls the unchecked edge stays --input and an invalid control still turns destructive. Keyboard focus rings a control in a colour other than primary in its --tone-text, red once it's invalid. CheckboxGroup and RadioGroup pass their color to every control in them without one, and ChoiceGroup takes color for the selected card's edge, the focused card's ring and the selected row's tint. Switch, Checkbox, Radio and Slider now set data-size, md by default. They need the @kappa-ui/core release that lets tone-control follow data-color.

    Recoloured Switch, Checkbox, Radio or Slider with class="[--tone:…] [--tone-foreground:…]", as the theming page used to suggest? Their edges and marks now read --tone-text, so add [--tone-text:…] to the class, or use color.

  8. core 0.10.0

    5a478d3 — tone-control works with a color prop. On an element with data-slot and data-color it leaves --tone, --tone-foreground and --tone-text to the [data-slot][data-color] rules, so the control takes that tone, and it keeps --input as the unchecked edge. A colour other than primary also rings the control in its --tone-text. Any other element gets primary exactly as before, now including --tone-text, so copies of Switch, Checkbox, Radio and Slider made before the prop look the same. tone-invalid now turns --tone-text destructive along with the rest of the tone, so edges, marks and rings drawn in it go red on an invalid control too. Copies made before the prop don't read --tone-text, so they look the same. choice-row tints a selected option in its group's tone when the group has data-slot and data-color, and in primary when it doesn't.

  9. registry 0.10.0

    0f38faa — Switch, Checkbox, Radio and Slider take their tone from core's tone-control and tone-invalid utilities instead of a dozen variable assignments each, so they need the @kappa-ui/core release that adds them. They look the same, and a [--tone:…] class still recolours them. Long class strings across the components are now wrapped by variant group.

  10. registry 0.8.0

    8c1319e — ColorPicker: ColorPicker, ColorPickerArea, ColorPickerSlider, ColorPickerField, ColorPickerPreview, ColorPickerSwatches and ColorPickerSwatch over Reka UI's colour primitives. One v-model string in hex, rgb, hsl or hsb feeds every part; the area, the sliders and the swatches work from the keyboard and read out as sliders and options; the sliders are drawn on Slider's parts with its variant, touch-target, sizes, halo and press scale, one size from xs to xl scales the parts together, and a surrounding Field supplies the label, description, error and disabled state.