# Slider

> Picks a value or a range on a track, such as where a preview starts and ends.

A slider picks a value, or a range with two thumbs, by dragging along a track, such as the part of a song to use as a preview.

## When to use

- Use it when the rough position matters more than the exact number: a preview window, a volume.
- Always show the value as text next to it, such as "0:30–1:00 dari 3:20".
- When the number has to be exact, such as a price or a percentage in a contract, use an [Input](https://design.sosialoka.tech/components/input/).

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/slider/_

## Anatomy

1. Track
2. Selected range
3. Thumb
4. Value

| Part | Style |
| --- | --- |
| Track | 6px (`h-1.5`), `rounded-full`, `bg-muted` |
| Range | `bg-primary` |
| Thumb | 16px (`size-4`), `rounded-full`, a `primary` border on `background` |

With two thumbs, they never pass each other.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `value`, `onValueChange` | `v-model` | `ShadSlider initialValue`, `onChanged` | number[] / number | none | The value, or the start and end of a range. |
| `min`, `max`, `step` | same | `min`, `max`, `divisions` | number | 0, 100, 1 | The range and the step. |
| `getAriaLabel` | `get-aria-label` | `Semantics label` | function | none | Names each thumb, such as "Awal" and "Akhir". |
| `getAriaValueText` | `get-aria-value-text` | `semanticFormatterCallback` | function | the number | What screen readers say, such as "1:05". |

`shadcn_ui` has one thumb; for a range in Flutter, use Material's `RangeSlider` inside `SosialokaTheme.material`.

## Code

**React**
```tsx
import { useState } from "react"
import { Slider } from "@/components/ui/slider"

const time = (seconds: number) => `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`

export default function SliderDemo() {
  const [range, setRange] = useState([30, 60])
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <div className="flex items-baseline justify-between text-sm">
        <p id="preview-label" className="font-medium">
          Potong preview
        </p>
        <p className="text-muted-foreground tabular-nums">
          {time(range[0])}–{time(range[1])} dari 3:20
        </p>
      </div>
      <Slider
        aria-labelledby="preview-label"
        value={range}
        onValueChange={(value) => setRange(value as number[])}
        max={200}
        getAriaLabel={(index) => (index === 0 ? "Awal" : "Akhir")}
        getAriaValueText={(_, value) => time(value)}
      />
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { ref } from "vue"
import { Slider } from "@/components/ui/slider"

const range = ref([30, 60])
const time = (seconds: number) => `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`
</script>

<template>
  <div class="flex w-full max-w-sm flex-col gap-3">
    <div class="flex items-baseline justify-between text-sm">
      <p id="preview-label" class="font-medium">
        Potong preview
      </p>
      <p class="text-muted-foreground tabular-nums">
        {{ time(range[0]) }}–{{ time(range[1]) }} dari 3:20
      </p>
    </div>
    <Slider
      v-model="range"
      aria-labelledby="preview-label"
      :max="200"
      :get-aria-label="(index) => (index === 0 ? 'Awal' : 'Akhir')"
      :get-aria-value-text="(value) => time(value)"
    />
  </div>
</template>
```

**Flutter**
```dart
// ShadSlider has one thumb. For a range (start and end of a preview), use Material's RangeSlider inside
// SosialokaTheme.material, whose slider colors come from the same tokens.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

String _time(double seconds) => '${seconds ~/ 60}:${(seconds % 60).toInt().toString().padLeft(2, '0')}';

class SliderDemo extends StatefulWidget {
  const SliderDemo({super.key});

  @override
  State<SliderDemo> createState() => _SliderDemoState();
}

class _SliderDemoState extends State<SliderDemo> {
  double start = 30;

  @override
  Widget build(BuildContext context) {
    final theme = ShadTheme.of(context);
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 384),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Row(
            children: [
              const Expanded(child: Text('Awal preview', style: TextStyle(fontWeight: FontWeight.w500))),
              Text('${_time(start)} dari 3:20', style: theme.textTheme.muted),
            ],
          ),
          const SizedBox(height: 12),
          ShadSlider(
            initialValue: start,
            max: 200,
            divisions: 200,
            semanticFormatterCallback: _time,
            onChanged: (value) => setState(() => start = value),
          ),
        ],
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/slider.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/slider.json
```

## Accessibility

- Each thumb is a `slider` with a name and a spoken value, such as "Awal, 0:30".
- Arrow keys move one step, Page Up and Page Down ten, Home and End to the ends.
- The value also shows as text, so nobody has to drag to read it.
