# Slider (/docs/native/components/slider)

<Preview id="slider/anatomy" title={null} />

## Installation [#installation]

<ComponentInstall name="slider" />

## Usage [#usage]

```tsx title="app/index.tsx"
import { Slider } from "@/components/ui/slider";
import { useState } from "react";
import { View } from "react-native";

export default function Example() {
  const [volume, setVolume] = useState<number | number[]>(40);

  return (
    <View className="flex-1 justify-center p-4">
      <Slider value={volume} onChange={setVolume}>
        <Slider.Output />
        <Slider.Track>
          <Slider.Fill />
          <Slider.Thumb />
        </Slider.Track>
      </Slider>
    </View>
  );
}
```

## Colours [#colours]

`color` paints the fill, the capsule and the knob — never the groove.

<Preview id="slider/colours" />

```tsx
<Slider color="default" />
<Slider color="primary" />
<Slider color="success" />
<Slider color="warning" />
<Slider color="destructive" />
<Slider color="info" />
```

## Sizes [#sizes]

One axis drives the groove's thickness, the thumb's diameter and the readout's type step.

<Preview id="slider/sizes" />

```tsx
<Slider size="sm" />
<Slider size="md" />
<Slider size="lg" />
```

## Ranges [#ranges]

Pass an array and the slider becomes a range, one thumb per entry.

<Preview id="slider/a-range" />

```tsx
<Slider value={[20, 80]} onChange={setRange}>
  <Slider.Track>
    {({ values }) => (
      <>
        <Slider.Fill />
        {values.map((_, index) => (
          <Slider.Thumb index={index} key={index} />
        ))}
      </>
    )}
  </Slider.Track>
</Slider>
```

<Callout type="warn">
Which shape you speak is **locked on first render**. A slider that silently started reporting an
array to a caller holding a number would be a bug with no error attached, so a flip warns once in
development and the slider keeps following you.
</Callout>

## Vertical [#vertical]

`orientation="vertical"` counts up from the bottom.

<Preview id="slider/vertical" />

```tsx
<View className="h-48">
  <Slider orientation="vertical" value={volume} onChange={setVolume}>
    <Slider.Track>
      <Slider.Fill />
      <Slider.Thumb />
    </Slider.Track>
  </Slider>
</View>
```

<Callout type="warn" title="A vertical slider needs a definite height">
It fills whatever it is given, so a wrapper with `h-48` or `flex-1` is not optional.
</Callout>

## Accessibility [#accessibility]

Each thumb is an `adjustable` with a value, and its **name** comes from the `Field.Label` a row
away. A lone thumb reads the label — "Volume, 40". A range's two thumbs read the label plus the end
each holds — "Price range, minimum" and "Price range, maximum" — and with no label at all they
still read "Minimum" and "Maximum", because two thumbs announcing the same bare number are
indistinguishable.

```tsx
<Field>
  <Field.Label>Price range</Field.Label>
  <Slider value={range} onChange={setRange}>
    <Slider.Track>
      {({ values }) => (
        <>
          <Slider.Fill />
          {values.map((_, index) => (
            <Slider.Thumb index={index} key={index} />
          ))}
        </>
      )}
    </Slider.Track>
  </Slider>
</Field>
```

An `accessibilityLabel` on a thumb names that thumb yourself, and wins outright:

```tsx
<Slider.Thumb accessibilityLabel="Lowest price" index={0} />
<Slider.Thumb accessibilityLabel="Highest price" index={1} />
```

<Callout type="warn">
A `Text.Label` sitting beside the slider does not name it — React Native has no `<label for>`. Only
a `Field.Label` reaches the thumb, and only when its children are a plain string. A slider with
neither reads its value alone.
</Callout>

A VoiceOver or TalkBack swipe steps the value by `step`, or by a tenth of the range when the slider
is continuous.

## Inside a scroll view [#inside-a-scroll-view]

```tsx
<ScrollView delaysContentTouches={false}>
  <Slider value={volume} onChange={setVolume}>…</Slider>
</ScrollView>
```

<Callout type="warn">
On iOS the slider feels late until the scrollable sets `delaysContentTouches={false}`. That is a
prop at your call site, not something the component can set.
</Callout>

## API Reference [#api-reference]

### Anatomy [#anatomy]

| Part | What it is |
| --- | --- |
| `Slider` | Owns the value, the gesture and the axes |
| `Slider.Output` | The current value, formatted. Give it a function to label it yourself |
| `Slider.Track` | The groove the thumbs run along, and the surface the drag is claimed on. Takes a render function for a range |
| `Slider.Fill` | The painted part of the groove, from the minimum — or between two thumbs |
| `Slider.Thumb` | The grab handle. `index` picks which value it drives in a range |

### Slider [#slider]

<TypeTable
  type={{
    value: { type: "number | number[]", description: "Controlled value. An array makes it a range, one thumb per entry." },
    defaultValue: { type: "number | number[]", description: "Starting value while uncontrolled." },
    minValue: { type: "number", default: "0" },
    maxValue: { type: "number", default: "100" },
    step: { type: "number", default: "1", description: "The increment the value snaps to. 0 is continuous, and never ticks." },
    formatOptions: { type: "Intl.NumberFormatOptions", description: "Passed to Intl.NumberFormat by Slider.Output." },
    orientation: {
      type: '"horizontal" | "vertical"',
      default: '"horizontal"',
      description: "vertical counts up from the bottom, and needs a definite height.",
    },
    color: {
      type: '"default" | "primary" | "success" | "warning" | "destructive" | "info"',
      default: '"default"',
      description: "What the filled part of the track means.",
    },
    size: { type: '"sm" | "md" | "lg"', default: '"md"' },
    isDisabled: { type: "boolean", description: "Blocks the drag and fades the control. Inherited from an enclosing Field." },
    isInvalid: { type: "boolean", description: "Reports an invalid value. Inherited from an enclosing Field." },
    haptic: {
      type: 'false | "selection" | "light" | "medium" | "heavy" | "success" | "warning" | "error"',
      description: "Played on grab and as the value crosses a step.",
    },
    onChange: { type: "(value: number | number[]) => void", description: "Fires on every change during the drag." },
    onChangeEnd: { type: "(value: number | number[]) => void", description: "Fires once, when the drag is released. Where a network write belongs." },
    children: { type: "ReactNode" },
  }}
/>

### Slider.Thumb [#sliderthumb]

<TypeTable
  type={{
    index: { type: "number", default: "0", description: "Which value this thumb drives, in a range." },
    accessibilityLabel: {
      type: "string",
      description:
        "The name a screen reader reads before the value. Defaults to the enclosing Field.Label's text — plus \"minimum\" or \"maximum\" in a range. Wins outright when set.",
    },
    className: { type: "string" },
    knobProps: { type: "ViewProps", description: "Passed to the knob inside the capsule." },
  }}
/>
