Sound
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.
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.
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.
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.
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.
| Size | Track thickness |
|---|---|
xs | 16dp |
s | 24dp |
m | 40dp |
l | 56dp |
xl | 96dp |
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.
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:
classNamedoes 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-draggingattribute while it is held. To style the slider while one of its handles is held, use thehas-dragging:variant, as in this example. A disabled slider matchesdisabled:. See States. - Functions:
className,styleandchildrencan be functions of the slider's state:orientation,isDisabledandstate. The state's type is exported asSliderRenderProps. 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.
Use cases
Material lists what people should be able to do with assistive technology:
- Navigate to a slider
- Select a range by controlling a handle along a track
- Get appropriate feedback based on input type
Interaction and style
- States: While a handle is held or dragged, or has keyboard focus, it narrows from 4dp to 2dp, and with a value indicator its value shows: the cues Material describes. The pointer is a hand over the track. Keyboard focus shows a focus ring around the handle, and a press with a mouse or touch does not.
- Touch target: A press anywhere along the track moves the nearer handle there. The track's touch target is 48dp thick, and thicker on an
m,lorxlslider. - Color contrast: Every slider has a stop at the end of its inactive track, the visual anchor Material advises so that the end has 3:1 contrast with the background. In the light and dark schemes, at every contrast level, that end keeps 3:1 contrast with the surface. On another background, keep that contrast, or mark the ends with icons that have it.
- Motion: With reduced motion, the value indicator appears and disappears instantly.
- Forced colors: In forced colors, the active track is drawn in the system's highlight color, and the inactive track and the handle in the system's text color. A disabled slider is grayed out.
- Right to left: In a right-to-left locale, the slider runs from the right. Its direction follows the locale, not the
dirattribute: see Right to left.
Focus
- Tab moves the focus to each handle, as Material advises: the focused handle then takes the arrow keys.
- A disabled slider cannot be focused.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus to a handle |
| Arrow keys | Increase or decrease the value by one step |
| Page Up or Page Down | Increase or decrease the value by a tenth of the range |
| Shift+Arrow keys | Increase or decrease the value by a tenth of the range |
| Space+Arrow keys | Increase or decrease the value by a tenth of the range |
| Home | Move the value to the minimum |
| End | Move the value to the maximum |
- Up arrow, and the arrow that points in the reading direction, increase the value. In a right-to-left locale, Left arrow increases it.
- A tenth of the range is rounded to whole steps, and is at least one step.
- In a range slider, a handle stops at the other handle's value, Home and End included.
Labeling elements
- Each handle has the slider role. A
Labelor anaria-labelnames the slider. Both handles of a range slider take the slider's name: a handle takes no name of its own. See Handle props. - A handle's value is announced as
formatOptionsformats it. The value indicator is hidden from assistive technology. - A vertical slider reports its orientation to assistive technology.
- For buttons beside the slider, such as ones that step the value, use an icon button, which has the button role, as Material advises. Give it an
aria-label.
size's' | 'xs' | 'm' | 'l' | 'xl'Default'xs'Sliders come in different sizes: XS, S, M, L, and XL. Use larger sizes to increase the targets and provide a larger visual emphasis.
The active and inactive tracks should always be the same size.
xs: 16dps: 24dpm: 40dpl: 56dpxl: 96dp
XL sliders should be reserved for hero moments, where the slider itself is the most important element on the page.
isCenteredbooleanDefaultfalseCentered sliders select a value from a positive and negative value range. Use this when zero, or the default value, is in the middle of the range.
showStopsbooleanDefaultfalseStop indicators show which predetermined values can be chosen on the slider. The slider handle snaps to the closest stop.
Avoid having too many stop indicators on a slider, because it can become visually crowded and difficult to adjust the value.
showValueIndicatorbooleanDefaultfalseThe value displays the specific value that corresponds with the handle’s placement.
A value appears when interacting with the corresponding handle. For range sliders, only one value should be shown at a time.
If the value is shown elsewhere, the indicator is not required.
iconReactNodeStandard sliders that are M, L, or XL can include an icon within the track. This icon should illustrate what the slider controls. Avoid adding inset icons to XS or S sliders.
When there’s not enough space for the icon on the active track, like at a low value, the icon moves to the inactive track.
Consider swapping which icon is displayed at zero, like a volume icon becoming a mute icon.
Don’t use inset icons on centered or range sliders. It makes it unclear where the start of the slider is.
Props of Slider (23)
classNameClassNameOrFunction<SliderRenderProps>Default'react-aria-Slider'The CSS className for the element. A function may be provided to compute the class based on component state.
formatOptionsIntl.NumberFormatOptionsThe display format of the value label.
stepnumberDefault1The slider's step value.
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
isDisabledbooleanWhether the whole Slider is disabled.
valuenumber | number[]The current value (controlled).
onChange(value: T) => voidHandler that is called when the value changes.
minValuenumberDefault0The slider's minimum value.
maxValuenumberDefault100The slider's maximum value.
defaultValuenumber | number[]The default value (uncontrolled).
orientation'horizontal' | 'vertical'Default'horizontal'The orientation of the Slider.
onChangeEnd(value: T) => voidFired when the slider stops moving, due to being let go.
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: SliderRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', SliderRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.formstringThe
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN.namestringThe name of the input element, used when submitting an HTML form. See MDN.
refRef<HTMLDivElement>
Slider also takes the 70 attributes and events of the element it renders.