Skip to content

Material 3

Components

Slider

Sliders allow users to make selections from a range of values.

9:30

Sound

Media volume
Call volume
Ring volume
Alarm volume

Sliders let people make selections from a range of values. They are ideal for adjusting settings such as volume and brightness, or the intensity of an image filter. A change made with a slider should take effect immediately, so that people see its effect as they move the handle.

To enter an exact value, use a text field instead.

import { Slider } from '@material-web/react'
import { Label, SliderOutput } from 'react-aria-components'

Basic slider

A slider selects a value along a track, from 0 to 100. Set the initial value with defaultValue, and another range with minValue and maxValue.

40
import { Slider } from '@material-web/react'
import { Label, SliderOutput } from 'react-aria-components'

export default function Basic() {
  return (
    <Slider defaultValue={40} className="w-80 gap-space-100">
      <div className="flex justify-between gap-space-200 px-space-150 text-body-md text-on-surface">
        <Label>Volume</Label>
        <SliderOutput className="text-on-surface-variant" />
      </div>
    </Slider>
  )
}

Use one Slider for every variant: it has a handle for each value you pass. The value selects the variant. Pass a number for a standard slider, a number with isCentered for a centered slider, or an array of two numbers for a range slider.

import { Slider } from '@material-web/react'

export default function Variants() {
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider aria-label="Standard" defaultValue={40} />
      <Slider aria-label="Centered" minValue={-50} maxValue={50} defaultValue={-20} isCentered />
      <Slider aria-label="Range" defaultValue={[30, 70]} />
    </div>
  )
}

Configure any variant with size, showStops, showValueIndicator and an inset icon. Each variant is shown below enabled, and disabled with isDisabled. A disabled slider cannot be moved or focused.

Standard slider

A standard slider selects one value from a range of values. Its active track runs from the start to the value. Use it when the slider should start from zero or the beginning of a sequence.

import { Slider } from '@material-web/react'

export default function Standard() {
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider aria-label="Volume" defaultValue={40} />
      <Slider aria-label="Volume, disabled" defaultValue={40} isDisabled />
    </div>
  )
}

Centered slider

A centered slider selects a value from a positive and negative value range. Its active track runs from the middle to the value, on whichever side that is. Use it when zero, or the default value, is in the middle of the range. isCentered applies to a single value only: a range slider ignores it.

import { Slider } from '@material-web/react'

export default function Centered() {
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider aria-label="Balance" minValue={-50} maxValue={50} defaultValue={-20} isCentered />
      <Slider
        aria-label="Balance, disabled"
        minValue={-50}
        maxValue={50}
        defaultValue={20}
        isCentered
        isDisabled
      />
    </div>
  )
}

Range slider

A range slider selects two values on one slider to create a range. Its active track runs between its two values. Use it to define a minimum and a maximum value. The handles do not pass each other: each one stops at the other's value.

import { Slider } from '@material-web/react'

export default function Range() {
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider aria-label="Price" defaultValue={[30, 70]} />
      <Slider aria-label="Price, disabled" step={10} defaultValue={[30, 70]} showStops isDisabled />
    </div>
  )
}

Label and output

Write a Label as a child of the slider to name it, and a SliderOutput to show its value, as in the first example of this page. Import both from react-aria-components, as for React Aria's Slider. The children are shown before the track, so arrange them with the slider's className.

Without a Label, set an aria-label on the slider.

formatOptions formats the value, including the text of the output and of the value indicator. It takes the options of Intl.NumberFormat. A SliderOutput's children can be a function of the slider's state, as in this example, which writes the two values of a range in its own words.

$100 to $350
import { Slider } from '@material-web/react'
import { Label, SliderOutput } from 'react-aria-components'

export default function Output() {
  return (
    <Slider
      maxValue={500}
      step={10}
      defaultValue={[100, 350]}
      formatOptions={{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }}
      className="w-80 gap-space-100"
    >
      <div className="flex justify-between px-space-150 text-body-md text-on-surface">
        <Label>Price</Label>
        <SliderOutput className="text-on-surface-variant">
          {({ state }) => `${state.getThumbValueLabel(0)} to ${state.getThumbValueLabel(1)}`}
        </SliderOutput>
      </div>
    </Slider>
  )
}

Inset icon

Set icon to show an icon in the track, one that illustrates what the slider controls. It is shown on a standard slider only, and an Icon in it is sized to the slider. Use an inset icon on an m, l or xl slider, and not on an xs or s one.

The icon is on the active track while there is room for it there, and on the inactive track otherwise. In this example, the first slider also swaps its icon at zero, from a volume icon to a mute icon.

import { Icon, Slider } from '@material-web/react'
import { useState } from 'react'

export default function InsetIcon() {
  const [volume, setVolume] = useState(30)
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider
        aria-label="Volume"
        size="l"
        value={volume}
        onChange={setVolume}
        icon={<Icon name={volume === 0 ? 'volume_off' : 'volume_up'} />}
      />
      <Slider
        aria-label="Brightness"
        size="xl"
        defaultValue={5}
        icon={<Icon name="light_mode" />}
      />
    </div>
  )
}

Value indicator

Set showValueIndicator to show the value above a handle while it is held or dragged, or has keyboard focus. Only that handle shows its value, so a range slider shows one value at a time. Format the text with formatOptions. Where the value is shown elsewhere, the slider needs no indicator.

import { Slider } from '@material-web/react'

export default function ValueIndicator() {
  return (
    <div className="flex w-80 flex-col gap-space-600 pt-space-400">
      <Slider aria-label="Brightness" defaultValue={50} showValueIndicator />
      <Slider
        aria-label="Price"
        maxValue={500}
        step={10}
        defaultValue={[100, 350]}
        formatOptions={{ style: 'currency', currency: 'USD', maximumFractionDigits: 0 }}
        showValueIndicator
      />
    </div>
  )
}

Controlled

Control the value with value and onChange, in place of defaultValue. Each takes a number, or an array of two numbers for a range slider. onChange is called as the value moves, and onChangeEnd once the handle is released.

18 to 24 °C, saved 18 to 24
import { Slider } from '@material-web/react'
import { useState } from 'react'
import { Label } from 'react-aria-components'

export default function Controlled() {
  const [range, setRange] = useState([18, 24])
  const [saved, setSaved] = useState(range)
  return (
    <Slider
      minValue={10}
      maxValue={30}
      step={0.5}
      value={range}
      onChange={setRange}
      onChangeEnd={setSaved}
      className="w-80 gap-space-100"
    >
      <div className="flex justify-between px-space-150 text-body-md text-on-surface">
        <Label>Temperature</Label>
        <span className="text-on-surface-variant">
          {range.join(' to ')} °C, saved {saved.join(' to ')}
        </span>
      </div>
    </Slider>
  )
}

Forms

Set a name to include a slider in a form. Its value is submitted with the form, and a form reset restores defaultValue. A range slider submits both values under its name, the start first: read them with getAll on the form's FormData. A disabled slider submits nothing.

Set form to the id of a form to include a slider that is outside it.

40
100–350
Nothing sent yet
import { Button, Slider } from '@material-web/react'
import { useState } from 'react'
import { Label, SliderOutput } from 'react-aria-components'

export default function Form() {
  const [sent, setSent] = useState('Nothing sent yet')
  return (
    <form
      className="flex flex-col items-start gap-space-300"
      onSubmit={(event) => {
        event.preventDefault()
        const entries = [...new FormData(event.currentTarget)]
        setSent(entries.map(([name, value]) => `${name}=${String(value)}`).join('&'))
      }}
      onReset={() => setSent('Nothing sent yet')}
    >
      <Slider name="volume" defaultValue={40} className="w-80 gap-space-100">
        <div className="flex justify-between px-space-150 text-body-md text-on-surface">
          <Label>Volume</Label>
          <SliderOutput className="text-on-surface-variant" />
        </div>
      </Slider>
      <Slider
        name="price"
        maxValue={500}
        step={10}
        defaultValue={[100, 350]}
        className="w-80 gap-space-100"
      >
        <div className="flex justify-between px-space-150 text-body-md text-on-surface">
          <Label>Price</Label>
          <SliderOutput className="text-on-surface-variant" />
        </div>
      </Slider>
      <div className="flex gap-space-100">
        <Button type="reset" variant="text">
          Reset
        </Button>
        <Button type="submit">Save</Button>
      </div>
      <output className="text-body-sm text-on-surface-variant">{sent}</output>
    </form>
  )
}

Discrete sliders

Set step to the interval between values, which is 1 by default. Set showStops to show a stop at every step from minValue. The handle snaps to the nearest stop as it is dragged. Avoid too many stops on one slider: they crowd the track and make the value hard to adjust.

The end stops are shown with or without showStops: every slider has a stop at the end of its track, and a centered or range slider has one at the start too.

import { Slider } from '@material-web/react'

export default function Stops() {
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider aria-label="Standard" step={10} defaultValue={40} showStops />
      <Slider
        aria-label="Centered"
        minValue={-50}
        maxValue={50}
        step={10}
        defaultValue={20}
        isCentered
        showStops
      />
      <Slider aria-label="Range" step={10} defaultValue={[20, 60]} showStops />
    </div>
  )
}

Sizes

Set size to xs (the default), s, m, l or xl. A larger size has a thicker track, a larger target and more visual emphasis. Reserve xl for hero moments, where the slider is the most important element on the page.

SizeTrack thickness
xs16dp
s24dp
m40dp
l56dp
xl96dp
import { Slider } from '@material-web/react'

export default function Sizes() {
  return (
    <div className="flex w-80 flex-col gap-space-300">
      <Slider aria-label="Extra small" size="xs" defaultValue={40} />
      <Slider aria-label="Small" size="s" defaultValue={40} />
      <Slider aria-label="Medium" size="m" defaultValue={40} />
      <Slider aria-label="Large" size="l" defaultValue={40} />
      <Slider aria-label="Extra large" size="xl" defaultValue={40} />
    </div>
  )
}

Orientation

Set orientation="vertical" for a slider whose value runs from the bottom. Give it a height with className or style: the track is as long as the slider is tall. The value indicator then appears at the handle's inline start. Avoid a vertical range slider: it adds cognitive load, and people expect most sliders to be horizontal.

import { Icon, Slider } from '@material-web/react'

export default function Vertical() {
  return (
    <div className="flex h-64 gap-space-600 ps-space-600">
      <Slider aria-label="Bass" orientation="vertical" defaultValue={40} showValueIndicator />
      <Slider aria-label="Treble" orientation="vertical" step={10} defaultValue={60} showStops />
      <Slider
        aria-label="Brightness"
        orientation="vertical"
        size="m"
        defaultValue={70}
        icon={<Icon name="light_mode" />}
      />
    </div>
  )
}

Right to left

A slider's direction follows the locale, not the dir attribute. To run the value from the right, wrap the slider in an I18nProvider with a right-to-left locale, and set dir for the rest of the layout. See Internationalization.

import { Icon, Slider } from '@material-web/react'
import { I18nProvider } from 'react-aria-components'

export default function RightToLeft() {
  return (
    <I18nProvider locale="ar-EG">
      <div dir="rtl" className="flex w-80 flex-col gap-space-600">
        <Slider aria-label="Volume" size="m" defaultValue={40} icon={<Icon name="volume_up" />} />
        <Slider aria-label="Price" defaultValue={[30, 70]} showValueIndicator />
      </div>
    </I18nProvider>
  )
}

Customization

Pass classes in className to size the slider and to arrange its children. They go on its outermost element, as do style and ref. See Styling for the classes, the states and the theme.

40
import { Slider } from '@material-web/react'
import { Label, SliderOutput } from 'react-aria-components'

export default function Custom() {
  return (
    <Slider
      defaultValue={40}
      className="w-80 gap-space-100 text-on-surface-variant has-dragging:text-primary"
    >
      <div className="flex justify-between px-space-150 text-body-md">
        <Label>Volume</Label>
        <SliderOutput />
      </div>
    </Slider>
  )
}
  • Color: className does not reach the track and the handles. To change their colors, set the color roles they use. See Theme.
  • States: A handle has the data-dragging attribute while it is held. To style the slider while one of its handles is held, use the has-dragging: variant, as in this example. A disabled slider matches disabled:. See States.
  • Functions: className, style and children can be functions of the slider's state: orientation, isDisabled and state. The state's type is exported as SliderRenderProps. See Children as a function.
  • Every slider: To restyle all the sliders at once, change the theme.

Limitations

Handle props

A handle takes no props of its own: you cannot set an aria-label or inputRef on a single handle. The slider's name and form apply to every handle, so the two handles of a range slider cannot be submitted under different names.