Skip to main content

Slider

A slider lets the user select a value, or a range of values, from a continuous or discrete set. It follows the Material Design 3 Slider specification.

The component supports four MD3 configurations from one API:

  • Continuous: one thumb, any value in the range
  • Discrete: one thumb that snaps to a step, with tick marks that the component renders
  • Range: two thumbs (value={[low, high]})
  • Centered: the active track fills from the midpoint to the thumb, for a symmetric value such as a balance

Usage​

import { Slider } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { View } from 'react-native'

export default function App() {
const [value, setValue] = useState(0.4)
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24 }}>
<Slider value={value} onValueChange={(v) => setValue(v as number)} />
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Discrete​

Set step to a positive number to snap to fixed increments. The component renders the tick marks for you.

import { Slider, Typography } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { View } from 'react-native'

export default function App() {
const [value, setValue] = useState(40)
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24, gap: 12 }}>
<Slider
value={value}
onValueChange={(v) => setValue(v as number)}
minimumValue={0}
maximumValue={100}
step={10}
/>
<Typography variant="bodySmall">Value: {value}</Typography>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Range​

Pass a [low, high] tuple for a range slider with two thumbs. Both thumbs obey step when you set it.

import { Slider, Typography } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { View } from 'react-native'

export default function App() {
const [range, setRange] = useState([25, 75])
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24, gap: 12 }}>
<Slider
value={range}
onValueChange={(v) => setRange(v)}
minimumValue={0}
maximumValue={100}
showValueLabel="always"
/>
<Typography variant="bodySmall">
{Math.round(range[0])} – {Math.round(range[1])}
</Typography>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Each thumb is its own control for a screen reader and for the keyboard. A screen reader user moves each thumb with its own adjust gesture, and on the web Tab moves from one thumb to the other. The thumbs read "Minimum" and "Maximum" by default. Change them with startThumbAccessibilityLabel and endThumbAccessibilityLabel. When you also pass accessibilityLabel, each thumb reads both labels, for example "Runtime, Shortest".

<Slider
accessibilityLabel="Runtime"
startThumbAccessibilityLabel="Shortest"
endThumbAccessibilityLabel="Longest"
value={runtime}
onValueChange={(v) => setRuntime(v)}
minimumValue={60}
maximumValue={180}
/>

Centered​

Pass centered for a slider whose active track fills out from the midpoint, not from the start. Use it for a symmetric value such as a balance, an EQ band, or a zoom adjustment.

import { Slider, Typography } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { View } from 'react-native'

export default function App() {
const [balance, setBalance] = useState(0)
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24, gap: 12 }}>
<Slider
value={balance}
onValueChange={(v) => setBalance(v as number)}
minimumValue={-50}
maximumValue={50}
step={5}
centered
showValueLabel="always"
/>
<Typography variant="bodySmall">Balance: {balance}</Typography>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

With Icons​

Add an icon at either end of the track with startIcon and endIcon. Use them for volume, brightness, or any other control where the bounds need a visual label.

import { Slider } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { View } from 'react-native'

export default function App() {
const [volume, setVolume] = useState(0.6)
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24 }}>
<Slider
value={volume}
onValueChange={(v) => setVolume(v as number)}
startIcon="volume-low"
endIcon="volume-high"
/>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Value label​

By default, the value-label bubble appears above the active thumb during a drag. Pass 'always' to keep it visible, or false to hide it. Use formatValueLabel to change the displayed string.

<Slider value={value} onValueChange={setValue} showValueLabel="always" />

<Slider
value={price}
onValueChange={setPrice}
minimumValue={0}
maximumValue={1000}
step={10}
formatValueLabel={(v) => `$${v}`}
/>

Custom colors​

containerColor overrides the active track, contentColor overrides the thumb, and inactiveTrackColor overrides the inactive track. The ticks and stop indicators on the active segment stay onPrimary, as MD3 specifies. They don't derive from these overrides. The MD3 disabled treatment stays in place, whatever you override.

import { Slider } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'
import { View } from 'react-native'

export default function App() {
const [value, setValue] = useState(0.5)
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24 }}>
<Slider
value={value}
onValueChange={(v) => setValue(v as number)}
containerColor="#2E7D32"
contentColor="#1B5E20"
inactiveTrackColor="#C8E6C9"
/>
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Disabled​

import { Slider } from '@rootnative/components'
import { mdiResolver } from '@rootnative/components/mdi'
import { ThemeProvider } from '@rootnative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { View } from 'react-native'

export default function App() {
return (
<SafeAreaProvider>
<ThemeProvider iconResolver={mdiResolver}>
<View style={{ flex: 1, justifyContent: 'center', padding: 24 }}>
<Slider value={0.7} disabled />
</View>
</ThemeProvider>
</SafeAreaProvider>
)
}

Controlled vs uncontrolled​

Pass value for a controlled slider: you own the state, and each change comes through onValueChange. Omit value and pass defaultValue for an uncontrolled slider that keeps its own state.

// Controlled
<Slider value={value} onValueChange={setValue} />

// Uncontrolled — pass `[low, high]` to enable range mode
<Slider defaultValue={[20, 80]} onSlidingComplete={(v) => console.log(v)} />

onValueChange fires continuously during a drag. onSlidingComplete fires once, when the user releases the thumb. Use it to commit an expensive update (a network call, a large recompute) only on release.

Accessibility​

The track has accessibilityRole="adjustable". It sets accessibilityValue with min/max/now, rounded to integers, because the native accessibility APIs require integer values, and with a formatted text field that a screen reader reads aloud. Pass accessibilityLabel to say what the slider controls.

<Slider
value={volume}
onValueChange={setVolume}
accessibilityLabel="Output volume"
/>

Props​

PropTypeDefaultRequiredDescription
valueSliderValue-NoCurrent value. Pass a number for single-thumb mode or `[low, high]` for range mode. Use `defaultValue` for uncontrolled usage.
defaultValueSliderValue-NoInitial value for uncontrolled usage. Pass a `[low, high]` tuple to enable range mode.
startThumbAccessibilityLabelstringMinimumNoScreen reader label of the start thumb of a range slider, the thumb of `value[0]`. Each thumb is its own control, so a screen reader can move either one. With `accessibilityLabel`, the thumb reads "<accessibilityLabel>, <this label>". Range mode only.
endThumbAccessibilityLabelstringMaximumNoScreen reader label of the end thumb of a range slider, the thumb of `value[1]`. With `accessibilityLabel`, the thumb reads "<accessibilityLabel>, <this label>". Range mode only.
onValueChange(value: SliderValue) => void-NoCalled continuously during a drag with the latest value.
onSlidingComplete(value: SliderValue) => void-NoCalled once when the user releases the thumb.
minimumValuenumber0NoLowest allowed value.
maximumValuenumber1NoHighest allowed value.
stepnumber0NoStep interval. Set to a positive number to enable discrete mode (values snap to multiples of `step` and tick marks are shown by default). Omit or set to `0` for a continuous slider.
centeredbooleanNoCentered slider — the active track fills from the midpoint to the thumb instead of from the start. Useful for symmetric values like balance or EQ adjustments. Single-thumb mode only; ignored for range sliders.
showTickMarksboolean-NoWhether to render tick marks along the track. Defaults to `true` when `step > 0`, otherwise `false`. Pass an explicit boolean to override.
showValueLabelboolean | "always"NoShow the value bubble above the thumb. `true` shows it during drag only, `'always'` keeps it visible, `false` hides it entirely.
formatValueLabel(value: number) => string-NoCustom formatter for the value-label text. Defaults to integer rounding for discrete sliders and 2-decimal rounding for continuous sliders.
startIconIconSource-NoOptional icon decoration shown at the start (leading edge) of the track. Accepts a string name (resolved via the theme's `iconResolver`), a pre-rendered element, or a render function.
endIconIconSource-NoOptional icon decoration shown at the end (trailing edge) of the track.
containerColorstring-NoOverride the active track color.
contentColorstring-NoOverride the thumb color.
inactiveTrackColorstring-NoOverride the inactive track color.
disabledbooleanNoDisable interaction. Renders the standard MD3 disabled treatment (38% `onSurface`); `containerColor`/`contentColor` are ignored.
styleStyleProp<ViewStyle>-NoStyle applied to the root container.