# Slider

## Usage

Use `Slider` to choose a number or numeric range.

```vue
<script setup lang="ts">
import { Slider } from '@astryx-vue/core/Slider'
import { shallowRef } from 'vue'

const volume = shallowRef(50)
const budget = shallowRef<[number, number]>([20, 80])
</script>

<template>
  <Slider v-model="volume" label="Volume" />
  <Slider v-model="budget" label="Budget" value-display="text" />
</template>
```

Import the theme once in the application entry:

```ts
import '@astryx-vue/themes/neutral.css'
```

### Anatomy

- **Label**: Text identifying the numeric setting controlled by the slider.
- **Description**: Helper text between the label and the slider control.
- **Slider**: Control row containing the track, thumb or thumbs, and optional text value.
- **Interactive control**: Pointer and keyboard interaction surface containing the rail, fill, marks, and thumbs.
- **Track**: Background rail representing the available numeric range.
- **Filled range**: Accent segment from the minimum to a single value, or between two range values.
- **Tick mark**: Position marker supplied through the marks collection.
- **Mark label**: Optional text displayed beside a tick mark.
- **Thumb**: Draggable value indicator; range mode renders a minimum and maximum thumb.
- **Value display**: Formatted current value shown beside the slider when valueDisplay is text.
- **Value tooltip**: Formatted current value shown in a tooltip when valueDisplay is tooltip.
- **Status message**: Error, warning, or success message below the slider.

## Best practices

| Guidance | Practices |
| --- | --- |
| Do | Always provide a label, even if visually hidden, so the slider is accessible to screen readers. |
| Do | Format values with meaningful units like "$50" or "75%" instead of raw numbers. |
| Don't | Use for precise numeric entry; pair with a text input or use NumberInput instead. |
| Don't | Set a step size so large that only a few positions are possible; use SegmentedControl or radio buttons instead. |
| Don't | Wrap a disabled slider in Tooltip to explain why it is disabled; disabled controls swallow the hover events the wrapper needs. Use the disabledMessage prop instead. |

## Examples

### Formatted value

Temperature in Fahrenheit, shown beside the slider.

```vue
<script setup lang="ts">
import { Slider } from '@astryx-vue/core/Slider'
import { ref } from 'vue'

const value = ref(72)

function formatValue(next: number): string {
  return `${next}°F`
}
</script>

<template>
  <div style="width: 300px; max-width: 100%">
    <Slider
      v-model="value"
      label="Temperature"
      value-display="text"
      :min="60"
      :max="90"
      :step="1"
      :format-value="formatValue"
    />
  </div>
</template>
```

### Range

A price range with a lower and an upper thumb.

```vue
<script setup lang="ts">
import { Slider } from '@astryx-vue/core/Slider'
import { ref } from 'vue'

const value = ref<[number, number]>([20, 80])
</script>

<template>
  <div style="width: 300px; max-width: 100%">
    <Slider v-model="value" label="Price range" />
  </div>
</template>
```

### Marks

Volume with tick labels at 0, 25, 50, 75, and 100.

```vue
<script setup lang="ts">
import { Slider } from '@astryx-vue/core/Slider'
import { ref } from 'vue'

const value = ref(50)
const marks = [
  { value: 0, label: '0' },
  { value: 25, label: '25' },
  { value: 50, label: '50' },
  { value: 75, label: '75' },
  { value: 100, label: '100' },
]
</script>

<template>
  <div style="width: 300px; max-width: 100%">
    <Slider v-model="value" label="Volume" :marks="marks" />
  </div>
</template>
```

### Validation states

Error, warning, and success on CPU, memory, and disk.

```vue
<script setup lang="ts">
import { Slider } from '@astryx-vue/core/Slider'
import { VStack } from '@astryx-vue/core/Stack'
import { ref } from 'vue'

const cpu = ref(95)
const memory = ref(50)
const disk = ref(75)
</script>

<template>
  <div style="width: 300px; max-width: 100%">
    <VStack :gap="6">
      <Slider v-model="cpu" label="CPU Usage" :status="{ type: 'error', message: 'CPU usage is critically high' }" />
      <Slider v-model="memory" label="Memory" :status="{ type: 'warning', message: 'Memory usage is moderate' }" />
      <Slider v-model="disk" label="Disk" :status="{ type: 'success', message: 'Disk usage is healthy' }" />
    </VStack>
  </div>
</template>
```

## Keyboard controls

The slider is fully operable with the keyboard, so users never have to drag. Once a thumb has focus, these keys change its value:

- Tab moves focus to each thumb in turn.
- Left / Right move one step in the horizontal direction; the direction is reversed in right-to-left layouts.
- Up / Down increase or decrease the value by one step.
- PageUp / PageDown increase or decrease the value by ten steps.
- Home / End move to the allowed minimum or maximum.
- Escape closes the value tooltip or the disabled reason tooltip.

In range mode each thumb has its own name, taken from `minLabel` and `maxLabel`, so assistive technology can tell which bound is being adjusted. Each thumb's ARIA bounds also update when its sibling changes, so the range that is announced always matches the values the thumb can actually take.

## Usage guidance

A slider suits choosing a value along a continuous range, not entering an exact number. Keep the following in mind:

- Provide a numeric input when exact entry matters.
- `isRequired` only provides a required description, so assistive technology knows the field is required.
- Validate accepted values in application code.

## Props

| Property | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `number \| [number, number]` | — | Required. A single value or a range. |
| `label` | `string` | — | Required. The slider name. |
| `id` | `string` | — | Association ID prefix. Generated when omitted. |
| `min` | `number` | 0 | Minimum value. |
| `max` | `number` | 100 | Maximum value. |
| `step` | `number` | 1 | Increment per step. Must be greater than 0. |
| `minStepsBetweenThumbs` | `number` | 0 | Minimum number of steps between range thumbs. |
| `orientation` | `'horizontal' \| 'vertical'` | horizontal | Slider orientation. |
| `valueDisplay` | `'tooltip' \| 'text' \| 'none'` | tooltip | How to display values. |
| `formatValue` | `(value: number) => string` | — | Formats display values and accessible value text. |
| `marks` | `SliderMark[]` | — | Marks. Each has a value and optional label. |
| `description` | `string` | — | Description below the label. |
| `isLabelHidden` | `boolean` | false | Hides the visual label. Preserves its accessible name. |
| `isDisabled` | `boolean` | false | Prevents changes. Excludes form values. |
| `disabledMessage` | `string` | — | Disabled reason. Keeps disabled thumbs focusable. |
| `isRequired` | `boolean` | false | Shows the required marker. Does not run native required validation. |
| `isOptional` | `boolean` | false | Shows the optional marker. Overrides isRequired. |
| `requiredLabel` | `string` | Required | Required marker text. The default follows the active locale. |
| `optionalLabel` | `string` | Optional | Optional marker text. |
| `minLabel` | `string` | Minimum value | Accessible name for the lower thumb. The default follows the active locale. |
| `maxLabel` | `string` | Maximum value | Accessible name for the upper thumb. The default follows the active locale. |
| `width` | `number \| string` | — | Field width. Numbers use pixels. |
| `htmlName` | `string` | — | Name for hidden form values. |
| `form` | `string` | — | ID of the associated form. |
| `status` | `FieldStatusInput` | — | Error, warning or success status. |
| `labelIcon` | `Component` | — | Decorative label icon. |
| `labelTooltip` | `string` | — | Information tooltip beside the label. |
| `containerClass` | `string` | — | Class on the field shell. It has no effect inside a horizontal-labels form. |
| `containerStyle` | `StyleValue` | — | Inline style on the field shell. |
| `controlClass` | `string` | — | Class on the track container. It does not land on the thumb. |
| `controlStyle` | `StyleValue` | — | Inline style on the track container. |

## Events

- `update:modelValue(value)`: updates a number or range.
- `change(value)`: fires when the value changes.
- `changeEnd(value)`: fires after a drag, pointer cancellation or keyboard adjustment.

Each supported key press fires `changeEnd`.
An unchanged value at a boundary does not fire `change`.
Disabling the component during a drag cancels that drag.
This cancellation does not fire `changeEnd`.

## Ranges and steps

- Use a `number` for single-value mode.
- Use `[number, number]` for range mode.
- Steps are relative to `min`.
- Minimum separation is `minStepsBetweenThumbs × step`.
- Thumbs cannot cross.
- Values outside the bounds display at the nearest bound.
- Input ranges display in ascending order.
- Display normalization does not update the bound model by itself.
- Non-positive steps use `1`.
- If `max` is below `min`, the interval collapses to `min`.

## Forms

- Set `htmlName` to submit one hidden field for a single value.
- Ranges submit two hidden fields with the same name.
- Read ranges with `FormData.getAll(name)`.
- Disabled sliders do not submit values.
- Native form reset does not change `v-model`.
- Update the bound value in the form's `reset` handler to reset it.

## Template references and attributes

- `element`: the control's `HTMLDivElement | null`.
- `focus(index = 0, options?)`: focuses the specified thumb.
- `blur()`: removes thumb focus.
- `class`, `style` and other attributes apply to the outer field.
- `aria-labelledby` and `aria-describedby` merge into each thumb.
- The component owns thumb roles, values, bounds and disabled states.
- The `labelIcon` slot replaces the label icon.

## Theme

```css
.my-slider {
  --astryx-slider-accent: #047857;
  --astryx-slider-focus: #047857;
  --astryx-slider-height: 200px;
}
```

- `--astryx-slider-track`: unfilled track color.
- `--astryx-slider-accent`: filled track and thumb color.
- `--astryx-slider-focus`: keyboard focus color.
- `--astryx-slider-height`: vertical control height.

Thumb diameter is fixed at `20px`. Track thickness is `4px`.
Neutral follows the active light or dark mode.

## Accessibility

### Thumb

- Criterion: 1.4.11 Non-text Contrast
- Check: 3:1
- States: Rest, Hover, Pointer down

The thumb must have at least 3:1 contrast with the track and the surface behind it. Pointer down is the whole drag: measure the thumb with the pressed overlay applied.
