# Slider

Picks a value, or a range, by dragging along a track.

```tsx
import { Slider, RangeSlider } from '@halo-ds/react';
```

## When to use it
Use a slider when people choose a rough value and see the effect straight away, like volume, brightness or a price range. Slider is the track and handles, Single or Range; Slider field adds a label, the current value and the min and max.

## Do
- Show the current value next to the label.
- Use steps that make sense, like 5% or whole numbers.
- Pair with a Number input when exact values matter.
- Label the ends when the scale isn't obvious.

## Don't
- Use a slider for exact values, like a phone number or an age.
- Hide the value; people can't guess it from the handle.
- Make the handle smaller than the touch area.
- Use a slider with only two or three options; use a Segmented control.

## Slider or Number input?
Both set a number. Pick by how exact it needs to be.
- **Slider**: A rough value, where people watch the effect. A clear scale with a minimum and a maximum. Ranges in filters, like price.
- **Number input**: An exact value, like a quantity or an age. Very large scales, where dragging is fiddly. Pair both when people need speed and precision.

## Props
### Slider props
| Prop | Type | What it does |
| --- | --- | --- |
| `value` | `number \| undefined` |  |
| `defaultValue` | `number \| undefined` |  |
| `onValueChange` | `((value: number) => void) \| undefined` |  |
| `min` | `number \| undefined` |  |
| `max` | `number \| undefined` |  |
| `step` | `number \| undefined` |  |
| `size` | `"sm" \| "md" \| undefined` | 4px track with a 16px handle (sm), or 6px with 20px (md). Figma: Size. |
| `disabled` | `boolean \| undefined` |  |
| `label` | `ReactNode` | Names the slider. Shown above it with the value. Figma: Label. |
| `showValue` | `boolean \| undefined` | Shows the current value next to the label. Figma: Value. |
| `showMinMax` | `boolean \| undefined` | Shows min and max under the ends. Figma: Min max. |
| `formatValue` | `((value: number) => string) \| undefined` | Turns a number into readable text, like "40%". Used on screen and for screen readers. |
| `aria-label` | `string \| undefined` | Screen reader name when there is no visible label. |
| `className` | `string \| undefined` |  |
| `classNames` | `Partial<Record<SliderPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<SliderPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

### RangeSlider props
| Prop | Type | What it does |
| --- | --- | --- |
| `value` | `[number, number] \| undefined` |  |
| `defaultValue` | `[number, number] \| undefined` |  |
| `onValueChange` | `((value: [number, number]) => void) \| undefined` |  |
| `handleLabels` | `[string, string] \| undefined` | Screen reader names for the two handles. |
| `min` | `number \| undefined` |  |
| `max` | `number \| undefined` |  |
| `step` | `number \| undefined` |  |
| `size` | `"sm" \| "md" \| undefined` | 4px track with a 16px handle (sm), or 6px with 20px (md). Figma: Size. |
| `disabled` | `boolean \| undefined` |  |
| `label` | `ReactNode` | Names the slider. Shown above it with the value. Figma: Label. |
| `showValue` | `boolean \| undefined` | Shows the current value next to the label. Figma: Value. |
| `showMinMax` | `boolean \| undefined` | Shows min and max under the ends. Figma: Min max. |
| `formatValue` | `((value: number) => string) \| undefined` | Turns a number into readable text, like "40%". Used on screen and for screen readers. |
| `aria-label` | `string \| undefined` | Screen reader name when there is no visible label. |
| `className` | `string \| undefined` |  |
| `classNames` | `Partial<Record<SliderPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<SliderPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { RangeSlider, Slider } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ display: 'grid', gap: 32, width: 320 }}>
      <Slider label="Volume" defaultValue={60} />
      <RangeSlider label="Price" min={0} max={500} defaultValue={[80, 320]} formatValue={(v) => `$${v}`} />
    </div>
  );
}
```

### Range
Two handles for a start and an end, with the limits shown.
```tsx
import { RangeSlider } from '@halo-ds/react';

export default function Demo() {
  return (
    <div style={{ width: 320 }}>
      <RangeSlider label="Price" min={0} max={200} defaultValue={[20, 60]} showMinMax handleLabels={['Minimum price', 'Maximum price']} />
    </div>
  );
}
```

### Small and disabled
A small slider for tight panels, and a disabled one.
```tsx
import { Slider } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack" style={{ width: 320, gap: 32 }}>
      <Slider size="sm" aria-label="Opacity" defaultValue={60} />
      <Slider label="Volume" defaultValue={40} disabled />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/slider
