Slider

A control element that allows for a range of selections.

Anatomy

To set up the slider correctly, you’ll need to understand its anatomy and how we name its parts.

Each part includes a data-part attribute to help identify them in the DOM.

Examples

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'

const Basic = () => {
  return (
    <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>
  )
}

Using as a range slider

You can use the Slider component as a range slider by setting the value with an array of two numbers.

The first number is the start of the range, and the second number is the end of the range.

import { Slider } from '@ark-ui/react'

const Range = () => {
  return (
    <Slider.Root min={-50} max={50} defaultValue={[-10, 20]}>
      <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.MarkerGroup>
        <Slider.Marker value={-30}>*</Slider.Marker>
        <Slider.Marker value={0}>*</Slider.Marker>
        <Slider.Marker value={30}>*</Slider.Marker>
      </Slider.MarkerGroup>
    </Slider.Root>
  )
}

Adding slider marks

You 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.

import { Slider } from '@ark-ui/react'

const WithMarks = () => {
  return (
    <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.MarkerGroup>
        <Slider.Marker value={0}>0</Slider.Marker>
        <Slider.Marker value={25}>*</Slider.Marker>
        <Slider.Marker value={50}>50</Slider.Marker>
        <Slider.Marker value={75}>*</Slider.Marker>
      </Slider.MarkerGroup>
    </Slider.Root>
  )
}

Setting the initial value

To set the slider’s initial value, set the defaultValue prop to the array of numbers.

import { Slider } from '@ark-ui/react'

const InitialValue = () => (
  <Slider.Root defaultValue={[42]}>
    <Slider.Label>Label</Slider.Label>
    <Slider.ValueText />
    <Slider.Control>
      <Slider.Track>
        <Slider.Range />
      </Slider.Track>
      <Slider.Thumb key={0} index={0} />
    </Slider.Control>
    <Slider.MarkerGroup>
      <Slider.Marker value={0}>*</Slider.Marker>
      <Slider.Marker value={30}>*</Slider.Marker>
      <Slider.Marker value={60}>*</Slider.Marker>
    </Slider.MarkerGroup>
  </Slider.Root>
)

Specifying the minimum and maximum

By 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:

import { Slider } from '@ark-ui/react'

const MinMax = () => {
  return (
    <Slider.Root min={-10} 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>
  )
}

Setting the value’s granularity

By 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'

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 key={0} index={0} />
      </Slider.Control>
    </Slider.Root>
  )
}

Listening for changes

When the slider value changes, the onValueChange and onValueChangeEnd callbacks are invoked. You can use this to set up custom behaviors in your app.

import { Slider } from '@ark-ui/react'

const OnEvent = () => {
  return (
    <Slider.Root
      onValueChange={(details) => console.log(details.value)}
      onValueChangeEnd={(details) => console.log(details.value)}
    >
      <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>
  )
}

Changing the orientation

By 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'

const Vertical = () => {
  return (
    <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>
  )
}

Changing the origin

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.

import { Slider } from '@ark-ui/react'

const CenterOrigin = () => {
  return (
    <Slider.Root origin="center">
      <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>
  )
}

API Reference

Root

PropTypeDefault
aria-label
string[]
aria-labelledby
string[]
asChild
boolean
defaultValue
number[]
dir
'ltr' | 'rtl'"ltr"
disabled
boolean
form
string
getAriaValueText
(value: number, index: number) => string
getRootNode
() => Node | ShadowRoot | Document
id
string
ids
Partial<{ root: string thumb(index: number): string control: string track: string range: string label: string valueText: string marker(index: number): string }>
invalid
boolean
max
number
min
number
minStepsBetweenThumbs
number
name
string
onFocusChange
(details: FocusChangeDetails) => void
onValueChange
(details: ValueChangeDetails) => void
onValueChangeEnd
(details: ValueChangeDetails) => void
orientation
'horizontal' | 'vertical'
origin
'center' | 'start'
readOnly
boolean
step
number
thumbAlignment
'center' | 'contain'
thumbSize
{ width: number; height: number }
value
number[]

Label

PropTypeDefault
asChild
boolean

Range

PropTypeDefault
asChild
boolean

Thumb

PropTypeDefault
index
number
asChild
boolean

Track

PropTypeDefault
asChild
boolean

Marker

PropTypeDefault
value
number
asChild
boolean

Control

PropTypeDefault
asChild
boolean

ValueText

PropTypeDefault
asChild
boolean

MarkerGroup

PropTypeDefault
asChild
boolean

Previous

Select