# Slider

Picks a number, or a range of two, by dragging a raised knob along a flat rail: a volume, a price range, a zoom level. The accent range runs from the start to the knob over the soft gray track. variant steps draws a tick under the track at each step and the thumb lands on those ticks. marks puts a tick and a label under the track at each value you name. The arrow keys move it one step. A tooltip over the knob shows the value while it is dragged or moved with the keyboard. showValue prints the value beside the track.

**Dependencies:** npm: 1 direct, 3 total; registry: 3 direct, 3 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Pick a number where the feel matters more than the exact value: volume, zoom, brightness.
- Pick a range between two values, such as a price filter.

### When not to use

- An exact number, such as a quantity. Use Input.
- A few named choices. Use RadioGroup, or Select for a long list.
- Showing how much of a task is done. Use Progress.

### Native

**Volume**

```text
WerewolfSlider(label: "Volume", value: volume, onValueChange: { volume = $0 })
```

```text
WerewolfSlider(label = "Volume", value = volume, onValueChange = { volume = it })
```

**Save when the finger lifts**

```text
WerewolfSlider(label: "Brightness", defaultValue: 40, onValueCommit: { save(brightness: $0) })
```

```text
WerewolfSlider(label = "Brightness", defaultValue = 40f, onValueCommit = { save(brightness = it) })
```

**Fixed steps with a tick**

```text
WerewolfSlider(label: "Zoom", defaultValue: 50, step: 25, variant: .steps)
```

```text
WerewolfSlider(label = "Zoom", defaultValue = 50f, step = 25f, variant = WerewolfSliderVariant.Steps)
```

**Steps without the haptic**

```text
WerewolfSlider(label: "Zoom", defaultValue: 50, step: 25, variant: .steps, haptics: false)
```

```text
WerewolfSlider(label = "Zoom", defaultValue = 50f, step = 25f, variant = WerewolfSliderVariant.Steps, haptics = false)
```

**Disabled**

```text
WerewolfSlider(label: "Brightness", defaultValue: 30).disabled(true)
```

```text
WerewolfSlider(label = "Brightness", defaultValue = 30f, enabled = false)
```

## Variants

### variant

default is a smooth track. steps draws a tick under the track at each step, and the thumb lands on those ticks. With no step, the ticks split the range into four even parts.

Values: `default`, `steps`. Default: `default`.

### orientation

horizontal fills the width of its parent. vertical fills the height of its parent, at least 160px.

Values: `horizontal`, `vertical`. Default: `horizontal`.

## Examples

### Volume

```tsx
<div className="flex w-full max-w-xs flex-col gap-sm">
  <span id="volume-label" className="text-sm font-medium text-text-secondary">Volume</span>
  <Slider aria-labelledby="volume-label" defaultValue={[60]} />
</div>
```

### Price range

Two values make two thumbs, each with its own name.

```tsx
<Slider aria-label="Price" thumbLabels={["Minimum price", "Maximum price"]} defaultValue={[20, 80]} step={5} className="max-w-xs" />
```

### Marks

marks names a tick and a label under the track at each value. The mark at the current value is emphasised.

```tsx
<div className="w-full max-w-xs">
  <Slider aria-label="Level" defaultValue={[50]} variant="steps" marks={levelMarks} />
</div>
```

### Fixed steps

variant steps lands on even ticks. showValue prints the value beside the track. The tooltip still shows while dragging.

```tsx
<Slider aria-label="Zoom" defaultValue={[50]} variant="steps" showValue className="max-w-xs" />
```

### Disabled

```tsx
<Slider aria-label="Brightness" defaultValue={[30]} disabled className="max-w-xs" />
```

## Do and don't

### Do

- Use it when the exact number matters less than the feel: volume, zoom, a rough price range.
- Use showValue when the number should stay on screen. The tooltip still shows it while dragging. Offer an Input when an exact number matters.
- Save in onValueCommit, not in onValueChange, so a drag doesn't send a request per step.

### Don't

- Don't use it for a precise number such as a quantity; use Input.
- Don't use it for a few named choices; use RadioGroup or Select.
- Don't use it to show progress; use Progress.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `defaultValue` | `number` |  |  | One number per thumb, as an array: [40], or [20, 80] for a range. The value on first render, when you don't control it. |
| `value` | `number` |  |  | The values as an array, when you control them. Pair it with onValueChange. |
| `onValueChange` | `function` |  |  | Called with the new values on every step, while dragging too. Update what the slider previews here. |
| `onValueCommit` | `function` |  |  | Called once when the user lets go or presses a key. Save or fetch here. |
| `min` | `number` |  | `0` | The lowest value. |
| `max` | `number` |  | `100` | The highest value. |
| `variant` | `enum` | `default`, `steps` | `default` | default is a smooth track. steps draws a tick under the track at each step, and the thumb lands on those ticks. With no step, the ticks split the range into four even parts. |
| `marks` | `string` |  |  | Optional { value: number; label: string }[]. A tick and a label under the track at each value (to the right when vertical), in either variant. The mark equal to the current value is emphasised. Labels are decoration for sighted users; screen readers read the value, so use formatValue when a label carries meaning. (The spec schema has no array type, so it is declared as string.) Not generated for iOS and Android. |
| `step` | `number` |  | `1` | How far one arrow key press moves the thumb. On variant steps this is also the distance between stops. More than 21 ticks are not drawn. |
| `orientation` | `enum` | `horizontal`, `vertical` | `horizontal` | horizontal fills the width of its parent. vertical fills the height of its parent, at least 160px. Not generated for iOS and Android. |
| `disabled` | `boolean` |  | `false` | Can't be focused or moved; the range, the grip and the ticks turn to the disabled color and the knob lies flat. |
| `name` | `string` |  |  | Form name. Inside a form, each value is submitted under it. Not generated for iOS and Android. |
| `thumbLabels` | `string` |  |  | Optional string[] with one accessible name per thumb, in order, for a range (Minimum price, Maximum price). Each name replaces aria-label on its thumb. (The spec schema has no array type, so it is declared as string.) Not generated for iOS and Android. |
| `showValue` | `boolean` |  | `false` | Prints the value beside the track, or under it when vertical. The tooltip still shows while dragging or using the keyboard. Not generated for iOS and Android. |
| `formatValue` | `function` |  |  | (value, index) => string. Writes the value for the tooltip, the printed value and screen readers (aria-valuetext): (value) => `$${value}`. Not generated for iOS and Android. |
| `label` (required) | `string` |  |  | The accessible name, read before the value. On iOS and Android it is the label argument; on the web it is aria-label or aria-labelledby. Only on iOS, Android. |
| `haptics` | `boolean` |  | `true` | With variant steps, a drag that lands on a new step gives one throttled selection tick. false turns it off. The default variant never gives one. Only on iOS, Android. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | Slider: a row as wide as its parent (a column when vertical), 24px tall, that holds the track, the range, the ticks and the knobs. It holds the two knob centers as --slider-start and --slider-end. With marks it leaves room below (to the right when vertical) for the labels. |
| `track` | The flat rail: a 12px, fully round bar in the soft gray fill, the full length of the slider. |
| `range` | The accent fill over the track, from the start to the knob, or between the two knobs of a range. Fully round and flat. It ends at the knob's center. |
| `thumb` | A knob: a raised key 16px along the track and 24px across, centered on the track. Border-border, background-raised and shadow-low. The focus ring goes around it, and it shrinks to scale-pressed while pressed. |
| `grip` | One 2px line, 12px long, in the middle of the knob, turned to match the orientation. Tertiary text color at rest; accent while the pointer is over the knob, while it has keyboard focus and while it is pressed. Decorative. |
| `indicator` | A tick under the track (to the right when vertical): 1px by 6px, spacing-xs below the slider's bottom edge so the knob never covers it, one at each step with variant steps and at each mark. The tick at a current value is accent. |
| `label` | The text of a mark: 14px secondary text under its tick, primary and medium while its value is the current one. Decorative: aria-hidden. |
| `hitArea` | Transparent ::before centered on each knob, never smaller than size-target-min. |
| `value` | The value tooltip: Tooltip's surface (inverse fill, inverse 14px text, radius-element, shadow-med), 4px above a horizontal handle or to the right of a vertical one. It is drawn inside the handle, so a parent with overflow hidden clips it. showValue prints the value beside the track in 14px secondary text, under the track when the slider is vertical. |

## Accessibility

Role: `slider`

| Keys | Action |
| --- | --- |
| `ArrowRight, ArrowUp` | Raises the value by one step. |
| `ArrowLeft, ArrowDown` | Lowers the value by one step. |
| `PageUp, PageDown` | Raises or lowers the value by ten steps. |
| `Home, End` | Sets the value to min or max. |
| `Tab` | Moves to the next thumb; each thumb is its own tab stop. |

- Each thumb is a slider with aria-valuenow, aria-valuemin and aria-valuemax. The type requires aria-label or aria-labelledby on Slider, which every thumb takes; give each thumb of a range its own name with thumbLabels (Minimum price, Maximum price).
- The value tooltip shows while a thumb is dragged, and while it has keyboard focus; it is aria-hidden, since screen readers read aria-valuenow, or aria-valuetext when formatValue is set. showValue prints the same value beside the track, also aria-hidden.
- With formatValue, every thumb gets aria-valuetext, so a screen reader says "$40", not "40".
- The knob is 16px wide and its hit area is at least size-target-min (24px on mouse, 44px on touch); a press anywhere on the slider moves the nearest knob there.
- The grip lines, the ticks and the mark labels are aria-hidden decoration. A screen reader reads the value (aria-valuenow, or aria-valuetext with formatValue), so a label such as Mid is never the only way to know the value: pass formatValue when a label carries meaning.
- Disabled slides nowhere and turns the range, the grip line and the ticks to the disabled color; the knob lies flat.

## Install

```bash
npx werewolf-ui add slider
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add slider --platform ios
npx werewolf-ui add slider --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
