Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Slider for Vue UI SDK

A track with one or more thumbs for picking a value or a range, like a volume level or a price band in a filter. A slider for Vue on UI SDK.

On this page9 sections

A slider picks a number by dragging a thumb along a track, or a range with two thumbs, where the position says more than the digits: volume, brightness, a price band in a search filter. The arrow keys move it one step at a time.

When the exact figure matters, such as a quantity or a sum of money, use a Number Input. For an angle, a dial or a knob, see the Angle Slider and the Circular Slider.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Slider anatomy: root, label, value, track, range, thumb

Usage

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

<template>
  <Slider
    :aria-label='["Volume"]'
    class="mx-auto w-full max-w-xs"
    :defaultValue="[75]"
    :max="100"
    :step="1"
  />
</template>

Accessibility

Each thumb holds a visually hidden <input type="range">, so a screen reader announces it as a slider with its value, and the slider is a group around the thumbs. A SliderLabel inside Slider names every thumb. With no visible label, pass aria-label to Slider as an array with one name per thumb, such as ["Volume"], or as one string that names them all.

When the number needs a unit to make sense, such as 12 months, getAriaValueText sets the words a screen reader says for it.

Arrow Right
Increases the value by one step, in either orientation. In a horizontal slider that reads right to left, it decreases it.
Arrow Left
Decreases the value by one step, in either orientation. In a horizontal slider that reads right to left, it increases it.
Arrow Up
Increases the value by one step.
Arrow Down
Decreases the value by one step.
Page Up
Increases the value by largeStep, ten steps unless you set it.
Page Down
Decreases the value by largeStep.
Shift+Arrow Up
Increases the value by largeStep, as Shift with the Right arrow does.
Shift+Arrow Down
Decreases the value by largeStep, as Shift with the Left arrow does.
Home
Sets the value to its minimum, or in a range, as low as the thumb before it allows.
End
Sets the value to its maximum, or in a range, as high as the thumb after it allows.

Demos

Range

Two thumbs start at 25 and 50 and move in steps of 5, and the filled stretch between them is the selection: a price band, a time window, an age range. Use two thumbs when both ends are the person's choice. A thumb stops when it meets the other one.

Preview width

Multiple

Three thumbs at 10, 20 and 70 move in steps of 10, and the fill runs from the first to the last. Use more than two when the track is cut into bands, such as the thresholds between low, medium and high, rather than one span. Each extra thumb is one more decision for the person, so add it only when the bands need it.

Preview width

Vertical

Two sliders with orientation set to vertical and the class h-40, one at 50 and one at 25, fill from the bottom. A vertical slider takes the height it is given, so without one it has nowhere to travel. Use it beside something tall, like a mixer channel or a zoom control, where a horizontal track would widen the row.

Preview width

Controlled

A Temperature slider from 0 to 1 in steps of 0.1 holds two thumbs, at 0.3 and 0.7, in state, and the SliderValue beside its SliderLabel prints both as they move. Control the value when it shows elsewhere, drives a chart or saves while the thumb is still down. When only the final value matters, an uncontrolled slider and its end-of-drag callback are less code.

Preview width

Disabled

Setting disabled holds the thumb at 50 and fades the track, and neither dragging nor the arrow keys move it, since the thumb leaves the tab order. Keep a disabled slider when a parent switch or the plan has turned the setting off and its last value still tells people something. When the setting doesn't apply at all, remove the slider.

Preview width

RTL

One thumb at 75 in a Direction provider set to right to left, which sets dir on an element around the slider. UI SDK reads the direction from the page's CSS, so the track fills from the right, dragging follows the mirrored track, and the Right arrow lowers the value, which is still 75 out of 100. Use this on a page in Arabic or Hebrew.

Preview width

FAQ

How do I make a range Slider with two thumbs in Vue?

Give it two values instead of one, such as defaultValue set to [25, 50]. The slider draws one thumb per entry, and the fill runs between them, as in the Range demo.

A thumb stops when it meets the other one, and UI SDK has no option to let it push or cross. Set minStepsBetweenThumbs to keep a gap of that many steps between them, and Home and End then stop at that gap too.

How do I control the Slider value in Vue?

Keep the array in a ref and bind it with v-model, which reads and writes one value per thumb.

An array draws one thumb per entry. A plain number, or no value at all, draws a single thumb, and then v-model and @value-change get a number rather than an array.

How do I run code only when the user releases the Slider?

Listen to @value-change-end, or UI SDK's own @value-commit. It fires once when the pointer lifts or the key comes up, so holding an arrow key commits only once, and only if the value changed. @value-change fires on every step of a drag.

That is the place to save a setting or send a filtered search, so a request doesn't go out for each pixel the thumb moves.

How do I set the min, max and step of a Slider?

Set min, max and step on Slider. They default to 0, 100 and 1, and step can be a fraction, such as 0.1 in the Controlled demo.

Page Up, Page Down and Shift with an arrow move by largeStep, ten steps unless you set it.

How do I make a vertical Slider?

Set orientation to vertical and give the slider a height, such as the h-40 class in the Vertical demo. A vertical track fills the height it is given, so in a container with no height it has nowhere to go.

The Up and Down arrows move the thumb, and Left and Right work too.

Does the UI SDK Slider submit its value in a form?

Yes. Give Slider a name, and a hidden input beside it submits the value: a number for one thumb, and one entry such as 25/50 for a range, so split it on the slash on the server. Resetting the form puts the thumbs back where they started.

A slider with disabled set sends nothing, as with any disabled field.

How do I label a Slider for screen readers?

Put a SliderLabel inside Slider, and every thumb is named by it. With no visible label, pass aria-label to Slider as an array with one name per thumb, such as Minimum price and Maximum price for a range.

Add getAriaValueText when the number alone means little, so a screen reader says 12 months rather than 12.

How do I show the current value next to a Slider?

Put a SliderValue inside Slider. It prints the current value on its own, formatted by format on Slider, with the values of a range joined by an en dash, and getAriaValueText changes only what a screen reader hears, never this text. Give it children to show something else from your state, as the Controlled demo does with both values joined by a comma.

A label and its value usually share one row above the track, so wrap the two in a flex row and push the value to the end, as the Controlled demo does with its readout.

API Reference

Slider

<div> data-slot="slider"

PropType
value?Value

The value: a number, or one number per thumb. Pass it to control the slider. Two-way with v-model.

defaultValue?Value

The value at first: a number, or one number per thumb, like [20, 80].

controller?{ getValue: () → Value; setValue(value: Value, options?: SliderChangeOptions): boolean; focus: (thumb?: number) → void; }

Moves the slider from anywhere. Create one with createSliderController().

Type
{ getValue: () → Value; setValue(value: Value, options?: SliderChangeOptions): boolean; focus: (thumb?: number) → void; }
debug?boolean

Logs every state change with its reason to the console, in development builds.

disabled?boolean

Disables it like a native control: thumbs can't be focused or moved, and nothing is submitted.

form?string

The id of a form to belong to, when it isn't inside that form.

format?NumberFormat

How values read, for screen readers and SliderValue: Intl.NumberFormat options, like { style: "currency", currency: "EUR" }, or a function of the value.

getAriaLabel?(index: number) → string

Names each thumb, by its index. Wins over the slider's aria-label.

Parameters

index – number

Returns
string
getAriaValueText?( formattedValue: string, value: number, index: number ) → string

What a screen reader says for a thumb's value.

Type
( formattedValue: string, value: number, index: number ) → string
Parameters

formattedValue – string

value – number

index – number

Returns
string
largeStep?number

How far Page Up, Page Down and Shift with an arrow key move.

Default
10 steps
locale?string

The locale values are formatted in. Defaults to the page's lang, or the browser's language without one, once the page is interactive. Server markup uses English until then, so it never disagrees with the client.

max?number

The highest value. When the steps from min don't land on it, the highest step below it is the end.

Default
100
min?number

The lowest value.

Default
0
minStepsBetweenThumbs?number

How many steps apart neighbouring thumbs stay. Thumbs never pass each other. UI SDK's default: 0.

Default
minStepsBetweenValues
minStepsBetweenValues?number

Base UI's name for minStepsBetweenThumbs.

name?string

The name it submits under: the number, or a range as "start/end". Without one, it submits nothing.

orientation?"horizontal" | "vertical"

The slider's axis. Vertical sliders grow upward.

Type
"horizontal" | "vertical"
Default
"horizontal"
readOnly?boolean

Keeps it focusable and submitted, but stops drags, keys and screen reader adjustments from changing it.

required?boolean

Marks it required, with data-required. A slider always has a value, so it never blocks a form.

step?number

How far one step moves, from min.

Default
1
#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

EventType
@value-change(value: Value, details: SliderValueChangeDetails) → void

Runs while the value changes: on every step of a drag or a key.

Type
(value: Value, details: SliderValueChangeDetails) → void
Parameters

value – Value

details – SliderValueChangeDetails

Returns
void
@value-commit(value: Value, details: SliderValueCommitDetails) → void

Runs once an interaction that changed the value ends. For saving.

Type
(value: Value, details: SliderValueCommitDetails) → void
Default
onValueCommitted
Parameters

value – Value

details – SliderValueCommitDetails

Returns
void
@value-committed(value: Value, details: SliderValueCommitDetails) → void

Base UI's name for onValueCommit.

Type
(value: Value, details: SliderValueCommitDetails) → void
Parameters

value – Value

details – SliderValueCommitDetails

Returns
void
AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-dragging?string

Present while a thumb is being dragged.

data-orientation"horizontal" | "vertical"

The slider's axis.

Type
"horizontal" | "vertical"
CSS variableType
--slider-thumb-sizestring

SliderLabel

PropType
debug?boolean

Logs every state change with its reason to the console, in development builds.

#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

SliderValue

<output> data-slot="slider-value"

PropType
children?VNode

Replaces the formatted value, a range joined with an en dash.

#render?{ props, state, children }

A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.

class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled, by its own prop or a <fieldset>.

data-dragging?string

Present while a thumb is being dragged.

data-orientation"horizontal" | "vertical"

The slider's axis.

Type
"horizontal" | "vertical"