# Slider

Picks a value or a range by dragging along a track, with locale-formatted output.

- Family: selection
- Import: `import { Slider } from "@senvenli/react/selection";`
- Page: https://design.senvenli.com/docs/components/slider
- JSON: https://design.senvenli.com/docs/components/slider.json

## Example: One value

The value is always shown in text beside the label.

```tsx
import { Slider } from "@senvenli/react/selection";

export default function Example() {
  return (
    <div className="max-w-80">
      <Slider label="Volume" defaultValue={60} />
    </div>
  );
}
```

## Examples

### A range

Two thumbs and a formatted output: a price filter.

```tsx
import { Slider } from "@senvenli/react/selection";
import { useState } from "react";

export default function Example() {
  const [price, setPrice] = useState<[number, number]>([200, 800]);
  return (
    <div className="max-w-80">
      <Slider
        label="Order total"
        description="Orders outside the range are hidden."
        minValue={0}
        maxValue={1000}
        step={50}
        formatOptions={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }}
        value={price}
        onChange={setPrice}
      />
    </div>
  );
}
```

### Steps and a disabled slider

A coarse step with a unit, and a value that cannot change.

```tsx
import { Stack } from "@senvenli/react/layout";
import { Slider } from "@senvenli/react/selection";

export default function Example() {
  return (
    <div className="max-w-80">
      <Stack space="space-300">
        <Slider label="Discount" defaultValue={0.15} maxValue={0.5} step={0.05} formatOptions={{ style: "percent" }} />
        <Slider label="Storage used" description="Managed by your plan." defaultValue={72} formatOptions={{ style: "unit", unit: "gigabyte" }} isDisabled />
      </Stack>
    </div>
  );
}
```

### Sizes

`size` small, medium (default) or large: the thumb and the bar.

```tsx
import { Stack } from "@senvenli/react/layout";
import { Slider } from "@senvenli/react/selection";

export default function Example() {
  return (
    <Stack space="space-300">
      <Slider label="Small" size="small" defaultValue={30} />
      <Slider label="Medium" defaultValue={50} />
      <Slider label="Large" size="large" defaultValue={70} />
    </Stack>
  );
}
```

## Guidelines

### When to use it

- An approximate value or range matters more than an exact number.

### Use something else when

- [input](https://design.senvenli.com/docs/components/input.md): The exact number matters.

## Accessibility

Keyboard:

- Arrow keys: Moves the thumb by a step.
- Home, End: Moves to the minimum or maximum.

Built in:

- Each thumb is a slider with its value text.

You provide:

- A label.

## API reference

### Slider

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `React.ReactNode` |  | yes |  |
| description | `React.ReactNode` |  | no |  |
| size | `ComponentSize` |  | no | `small`, `medium` (default) or `large`; the provider default otherwise. |

Behavior props from React Aria:

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| defaultValue | `T` |  | no | The default value (uncontrolled). |
| onChange | `(value: T) => void` |  | no | Handler that is called when the value changes. |
| value | `T` |  | no | The current value (controlled). |
| formatOptions | `NumberFormatOptions` |  | no | The display format of the value label. |

Also takes 81 standard DOM and ARIA attributes (id, aria-label, event handlers…).

### Tokens it reads

`--ds-background-brand-bold`, `--ds-background-disabled`, `--ds-background-input`, `--ds-background-neutral-pressed`, `--ds-background-selected`, `--ds-border`, `--ds-border-bold`, `--ds-border-danger`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-radius-full`, `--ds-radius-large`, `--ds-radius-xsmall`, `--ds-shadow-raised`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-surface-raised`, `--ds-text`, `--ds-text-disabled`, `--ds-text-on-brand`, `--ds-text-subtlest`

## Related

- [input](https://design.senvenli.com/docs/components/input.md)
