Slider
A control element that allows for a range of selections.
A control element that allows for a range of selections.
To set up the slider correctly, you’ll need to understand its anatomy and how we name its parts.
Each part includes a
data-partattribute to help identify them in the DOM.
Learn how to use the Slider component in your project. Let’s take a look at
the most basic example:
import { Slider } from '@ark-ui/react'
export const Basic = () => {
return (
<Slider.Root>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
</Slider.Control>
</Slider.Root>
)
}
import { Slider } from '@ark-ui/solid'
export const Basic = () => {
return (
<Slider.Root>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
</Slider.Control>
</Slider.Root>
)
}
<script setup lang="ts">
import { Slider } from '@ark-ui/vue'
</script>
<template>
<Slider.Root>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb :key="0" :index="0" />
</Slider.Control>
</Slider.Root>
</template>
You can add multiple thumbs to the slider by adding multiple Slider.Thumb
import { Slider } from '@ark-ui/react'
export const Range = () => {
return (
<Slider.Root defaultValue={[5, 10]}>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb key={0} index={0} />
<Slider.Thumb key={1} index={1} />
</Slider.Control>
</Slider.Root>
)
}
import { Slider } from '@ark-ui/solid'
export const Range = () => {
return (
<Slider.Root value={[5, 10]}>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
<Slider.Thumb index={1} />
</Slider.Control>
</Slider.Root>
)
}
Story not foundYou can add marks to the slider track by using the Slider.MarkerGroup and
Slider.Marker components.
Position the Slider.Marker components relative to the track by providing the
value prop.
Story not foundStory not foundStory not foundTo set the slider’s initial value, set the defaultValue prop to the array of
numbers.
Story not foundStory not foundStory not foundBy default, the minimum is 0 and the maximum is 100. If that’s not what you
want, you can easily specify different bounds by changing the values of the
min and/or max props.
For example, to ask the user for a value between -10 and 10, you can use:
Story not foundStory not foundStory not foundBy default, the granularity, is 1, meaning that the value is always an
integer. You can change the step attribute to control the granularity.
For example, If you need a value between 5 and 10, accurate to two decimal
places, you should set the value of step to 0.01:
import { Slider } from '@ark-ui/react'
export const Step = () => {
return (
<Slider.Root step={0.01} min={5} max={10}>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
</Slider.Control>
</Slider.Root>
)
}
import { Slider } from '@ark-ui/solid'
export const Step = () => {
return (
<Slider.Root step={0.01} min={5} max={10}>
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
</Slider.Control>
</Slider.Root>
)
}
<script setup lang="ts">
import { Slider } from '@ark-ui/vue'
</script>
<template>
<Slider.Root :step="0.01" :min="5" :max="10">
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb :key="0" :index="0" />
</Slider.Control>
</Slider.Root>
</template>
When the slider value changes, the onValueChange and onValueChangeEnd
callbacks are invoked. You can use this to set up custom behaviors in your app.
Story not foundStory not foundStory not foundBy default, the slider is assumed to be horizontal. To change the orientation to vertical, set the orientation property in the machine’s context to vertical.
In this mode, the slider will use the arrow up and down keys to increment/decrement its value.
Don’t forget to change the styles of the vertical slider by specifying its height
import { Slider } from '@ark-ui/react'
export const Vertical = () => {
return (
<Slider.Root orientation="vertical">
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
</Slider.Control>
</Slider.Root>
)
}
import { Slider } from '@ark-ui/solid'
export const Vertical = () => {
return (
<Slider.Root orientation="vertical">
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb index={0} />
</Slider.Control>
</Slider.Root>
)
}
<script setup lang="ts">
import { Slider } from '@ark-ui/vue'
</script>
<template>
<Slider.Root orientation="vertical">
<Slider.Label>Label</Slider.Label>
<Slider.ValueText />
<Slider.Control>
<Slider.Track>
<Slider.Range />
</Slider.Track>
<Slider.Thumb :key="0" :index="0" />
</Slider.Control>
</Slider.Root>
</template>
By default, the slider’s origin is at the start of the track. To change the
origin to the center of the track, set the origin prop to center.
Story not foundStory not foundStory not found| Prop | Type | Default |
|---|---|---|
aria-labelThe aria-label of each slider thumb. Useful for providing an accessible name to the slider | string[] | |
aria-labelledbyThe `id` of the elements that labels each slider thumb. Useful for providing an accessible name to the slider | string[] | |
asChildRender as a different element type. | boolean | |
defaultValueThe initial value of the slider when it is first rendered.
Use when you do not need to control the state of the slider picker. | number[] | |
disabledWhether the slider is disabled | boolean | |
formThe associate form of the underlying input element. | string | |
getAriaValueTextFunction that returns a human readable value for the slider thumb | (details: ValueTextDetails) => string | |
idThe unique identifier of the machine. | string | |
idsThe ids of the elements in the range slider. Useful for composition. | Partial<{
root: string
thumb(index: number): string
control: string
track: string
range: string
label: string
valueText: string
marker(index: number): string
}> | |
invalidWhether the slider is invalid | boolean | |
maxThe maximum value of the slider | number | |
minThe minimum value of the slider | number | |
minStepsBetweenThumbsThe minimum permitted steps between multiple thumbs. | number | |
nameThe name associated with each slider thumb (when used in a form) | string | |
onFocusChangeFunction invoked when the slider's focused index changes | (details: FocusChangeDetails) => void | |
onValueChangeFunction invoked when the value of the slider changes | (details: ValueChangeDetails) => void | |
onValueChangeEndFunction invoked when the slider value change is done | (details: ValueChangeDetails) => void | |
orientationThe orientation of the slider | 'horizontal' | 'vertical' | |
origin- "start": Useful when the value represents an absolute value
- "center": Useful when the value represents an offset (relative) | 'center' | 'start' | |
readOnlyWhether the slider is read-only | boolean | |
stepThe step value of the slider | number | |
thumbAlignmentThe 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. | 'center' | 'contain' | |
thumbSizeThe slider thumbs dimensions | { width: number; height: number } | |
valueThe value of the range slider | number[] |
| Prop | Type | Default |
|---|---|---|
asChildRender as a different element type. | boolean |
| Prop | Type | Default |
|---|---|---|
asChildRender as a different element type. | boolean |
| Prop | Type | Default |
|---|---|---|
asChildRender as a different element type. | boolean |
| Prop | Type | Default |
|---|---|---|
value | number | |
asChildRender as a different element type. | boolean |
| Prop | Type | Default |
|---|---|---|
asChildRender as a different element type. | boolean |
| Prop | Type | Default |
|---|---|---|
index | number | |
asChildRender as a different element type. | boolean | |
name | string |
| Prop | Type | Default |
|---|---|---|
asChildRender as a different element type. | boolean |
| Prop | Type | Default |
|---|---|---|
asChildRender as a different element type. | boolean |