Skip to content

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

Components

Free

Slider for Svelte

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 Svelte on Ark UI.

On this page9 sections
UI library: Ark UI

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 lang="ts">
import { Slider } from "@/components/ui/slider";
</script>

defaultValue is an array with one entry per thumb: [75] draws one thumb, and [25, 75] draws a range. aria-label is an array too, one name per thumb.

<Slider
  aria-label={["Volume"]}
  class="w-full max-w-xs"
  defaultValue={[75]}
/>

Accessibility

Each thumb has the slider role and reports its value, its minimum and maximum, and its orientation. 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"].

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
In a horizontal slider, increases the value by one step.
Arrow Left
In a horizontal slider, decreases the value by one step.
Arrow Up
In a vertical slider, increases the value by one step.
Arrow Down
In a vertical slider, decreases the value by one step.
Page Up
Increases the value by the largeStep amount.
Page Down
Decreases the value by the largeStep amount.
Shift+Arrow Up
In a vertical slider, increases the value by the largeStep amount, ten steps unless you set it.
Shift+Arrow Down
In a vertical slider, decreases the value by the largeStep amount.
Home
Sets the value to its minimum.
End
Sets the value to its maximum.
Shift+Arrow Right
In a horizontal slider, increases the value by the largeStep amount.
Shift+Arrow Left
In a horizontal slider, decreases the value by the largeStep amount.

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. Ark UI takes the direction from the provider and sets it on the track, range and thumb, which dir on Slider alone wouldn't reach. The track fills from the right, 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 Svelte?

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. Set minStepsBetweenThumbs to keep a gap of that many steps between them, or thumbCollisionBehavior to push to let one thumb shove the other along.

How do I control the Slider value in Svelte?

Keep the array in state, pass it as value and update it in onValueChange, which receives an object, so read the new array from details.value.

Pass an array even for one thumb, since the number of entries is the number of thumbs. With neither value nor defaultValue, the slider has one thumb, at min.

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

Use onValueChangeEnd. It fires once when a drag ends and after each key press, while onValueChange 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 do nothing in a vertical slider.

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

Yes. Give Slider a name, and each thumb submits through a hidden input, named name[] when there is more than one thumb. On the server, read a range with your framework's way of collecting repeated fields, such as getAll on the form data.

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, with the values of a range joined by a comma. Give it children to show something else, such as the value with a unit, read from your state.

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?number[]

The controlled value of the slider.

defaultValue?number[]

The initial value of the slider when rendered. Use when you don't need to control the value of the slider.

onValueChange?(details: ValueChangeDetails) → void

Function invoked when the value of the slider changes.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number[] }

Returns
void
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the slider is disabled.

form?string

The associate form of the underlying input element.

getAriaValueText?(details: ValueTextDetails) → string

Function that returns a human readable value for the slider thumb.

Type
(details: ValueTextDetails) → string
Parameters

details – { value: number; index: number }

Returns
string
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the slider. Useful for composition.

Type
{ root?: string; thumb?: (index: number) → string; hiddenInput?: (index: number) → string; control?: string; track?: string; range?: string; label?: string; valueText?: string; marker?: (index: number) → string }
invalid?boolean

Whether the slider is invalid.

largeStep?number

The step value of the slider when the Shift key is held, or the PageUp/PageDown keys are used.

Default
10 * step
markerInterval?number

The interval between markers.

Default
1
markerLabels?string[]

The labels to show on the markers.

Default
[]
max?number

The maximum value of the slider.

Default
100
min?number

The minimum value of the slider.

Default
0
minStepsBetweenThumbs?number

The minimum permitted steps between multiple thumbs. minStepsBetweenThumbs * step should reflect the gap between the thumbs. - step: 1 and minStepsBetweenThumbs: 10 => gap is 10 - step: 10 and minStepsBetweenThumbs: 2 => gap is 20

Default
0
name?string

The name associated with each slider thumb (when used in a form).

onFocusChange?(details: FocusChangeDetails) → void

Function invoked when the slider's focused index changes.

Type
(details: FocusChangeDetails) → void
Parameters

details – { focusedIndex: number; value: number[] }

Returns
void
onValueChangeEnd?(details: ValueChangeDetails) → void

Function invoked when the slider value change is done.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number[] }

Returns
void
orientation?"vertical" | "horizontal"

The orientation of the slider.

Type
"vertical" | "horizontal"
Default
"horizontal"
origin?"start" | "center" | "end"

The origin of the slider range. The track is filled from the origin to the thumb for single values. - "start": Useful when the value represents an absolute value - "center": Useful when the value represents an offset (relative) - "end": Useful when the value represents an offset from the end

Type
"start" | "center" | "end"
Default
"start"
readOnly?boolean

Whether the slider is read-only.

showMarkers?boolean

Whether to show markers.

Default
false
step?number

The step value of the slider.

Default
1
thumbAlignment?"contain" | "center"

The alignment of the slider thumb relative to the track - center: the thumb will extend beyond the bounds of the slider track. - contain: the thumb will be contained within the bounds of the track.

Type
"contain" | "center"
Default
"contain"
thumbCollisionBehavior?"none" | "push" | "swap"

Controls how thumbs behave when they collide during pointer interactions. - none (default): Thumbs cannot move past each other; excess movement is ignored. - push: Thumbs push each other without restoring their previous positions when dragged back. - swap: Thumbs swap places when dragged past each other.

Type
"none" | "push" | "swap"
Default
"none"
thumbSize?{ width: number; height: number; }

The slider thumbs dimensions.

Type
{ width: number; height: number; }
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-dragging?string

Present when in the dragging state.

data-focus?string

Present when focused.

data-invalid?string

Present when invalid.

data-orientation"vertical" | "horizontal"

The orientation of the slider.

Type
"vertical" | "horizontal"
CSS variableType
--slider-range-endstring

The range end of the slider.

--slider-range-startstring

The range start of the slider.

--slider-thumb-heightstring

The thumb height of the slider.

--slider-thumb-transformstring

The thumb transform of the slider.

--slider-thumb-widthstring

The thumb width of the slider.

--translate-xstring

The horizontal translation value.

--translate-ystring

The vertical translation value.

SliderLabel

<label> data-slot="slider-label"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-dragging?string

Present when in the dragging state.

data-focus?string

Present when focused.

data-invalid?string

Present when invalid.

data-orientation"vertical" | "horizontal"

The orientation of the label.

Type
"vertical" | "horizontal"

SliderThumb

<div> data-slot="slider-thumb"

PropType
indexnumber

The thumb's position in its list.

name?string

The name it submits with its form.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-disabled?string

Present when disabled.

data-dragging?string

Present when in the dragging state.

data-focus?string

Present when focused.

data-indexstring

The index of the item.

data-namestring

Mirrors name.

data-orientation"vertical" | "horizontal"

The orientation of the thumb.

Type
"vertical" | "horizontal"

SliderValue

<span> data-slot="progress-value"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled.

data-focus?string

Present when focused.

data-invalid?string

Present when invalid.

data-orientation"vertical" | "horizontal"

The orientation of the valuetext.

Type
"vertical" | "horizontal"

SliderTrack

<div> data-slot="slider-track"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-disabled?string

Present when disabled.

data-dragging?string

Present when in the dragging state.

data-focus?string

Present when focused.

data-invalid?string

Present when invalid.

data-orientation"vertical" | "horizontal"

The orientation of the track.

Type
"vertical" | "horizontal"

SliderRange

<div> data-slot="slider-range"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-disabled?string

Present when disabled.

data-dragging?string

Present when in the dragging state.

data-focus?string

Present when focused.

data-invalid?string

Present when invalid.

data-orientation"vertical" | "horizontal"

The orientation of the range.

Type
"vertical" | "horizontal"

SliderControl

<div> data-slot="slider-control"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-disabled?string

Present when disabled.

data-dragging?string

Present when in the dragging state.

data-focus?string

Present when focused.

data-invalid?string

Present when invalid.

data-orientation"vertical" | "horizontal"

The orientation of the control.

Type
"vertical" | "horizontal"

SliderMarkerGroup

<div>

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-orientation"vertical" | "horizontal"

The orientation of the markergroup.

Type
"vertical" | "horizontal"

SliderMarker

<span> data-slot="slider-marker"

PropType
valuenumber

The marker's value.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
AttributeType
data-disabled?string

Present when disabled.

data-interval?string
data-orientation"vertical" | "horizontal"

The orientation of the marker.

Type
"vertical" | "horizontal"
data-state"under-value" | "over-value" | "at-value"

"under-value", "over-value" or "at-value".

Type
"under-value" | "over-value" | "at-value"
data-valuestring

The value of the item.

useSlider

PropType
value?number[]

The controlled value of the slider.

defaultValue?number[]

The initial value of the slider when rendered. Use when you don't need to control the value of the slider.

onValueChange?(details: ValueChangeDetails) → void

Function invoked when the value of the slider changes.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number[] }

Returns
void
disabled?boolean

Whether the slider is disabled.

form?string

The associate form of the underlying input element.

getAriaValueText?(details: ValueTextDetails) → string

Function that returns a human readable value for the slider thumb.

Type
(details: ValueTextDetails) → string
Parameters

details – { value: number; index: number }

Returns
string
id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the slider. Useful for composition.

Type
{ root?: string; thumb?: (index: number) → string; hiddenInput?: (index: number) → string; control?: string; track?: string; range?: string; label?: string; valueText?: string; marker?: (index: number) → string }
invalid?boolean

Whether the slider is invalid.

largeStep?number

The step value of the slider when the Shift key is held, or the PageUp/PageDown keys are used.

Default
10 * step
max?number

The maximum value of the slider.

Default
100
min?number

The minimum value of the slider.

Default
0
minStepsBetweenThumbs?number

The minimum permitted steps between multiple thumbs. minStepsBetweenThumbs * step should reflect the gap between the thumbs. - step: 1 and minStepsBetweenThumbs: 10 => gap is 10 - step: 10 and minStepsBetweenThumbs: 2 => gap is 20

Default
0
name?string

The name associated with each slider thumb (when used in a form).

onFocusChange?(details: FocusChangeDetails) → void

Function invoked when the slider's focused index changes.

Type
(details: FocusChangeDetails) → void
Parameters

details – { focusedIndex: number; value: number[] }

Returns
void
onValueChangeEnd?(details: ValueChangeDetails) → void

Function invoked when the slider value change is done.

Type
(details: ValueChangeDetails) → void
Parameters

details – { value: number[] }

Returns
void
orientation?"vertical" | "horizontal"

The orientation of the slider.

Type
"vertical" | "horizontal"
Default
"horizontal"
origin?"start" | "center" | "end"

The origin of the slider range. The track is filled from the origin to the thumb for single values. - "start": Useful when the value represents an absolute value - "center": Useful when the value represents an offset (relative) - "end": Useful when the value represents an offset from the end

Type
"start" | "center" | "end"
Default
"start"
readOnly?boolean

Whether the slider is read-only.

step?number

The step value of the slider.

Default
1
thumbAlignment?"contain" | "center"

The alignment of the slider thumb relative to the track - center: the thumb will extend beyond the bounds of the slider track. - contain: the thumb will be contained within the bounds of the track.

Type
"contain" | "center"
Default
"contain"
thumbCollisionBehavior?"none" | "push" | "swap"

Controls how thumbs behave when they collide during pointer interactions. - none (default): Thumbs cannot move past each other; excess movement is ignored. - push: Thumbs push each other without restoring their previous positions when dragged back. - swap: Thumbs swap places when dragged past each other.

Type
"none" | "push" | "swap"
Default
"none"
thumbSize?{ width: number; height: number; }

The slider thumbs dimensions.

Type
{ width: number; height: number; }