# Slider

A styled native range input for picking a number from a continuous or stepped range, where the exact value matters less than its position, such as volume or a price limit.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-slider`
- Source: [libs/ui/src/lib/molecules/slider/slider.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/molecules/slider/slider.ts)

```typescript title="Import"
import { Slider } from '@vigdx/ui/slider';
```

## Basic

Bind `[(value)]` to a `number | null` signal and set `min`, `max` (default 0 and 100) and `step`. `showValue` prints the current value next to the label. The filled part of the track follows the reading direction.

```typescript title="slider-basic.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Slider } from '@vigdx/ui/slider';

@Component({
  selector: 'app-slider-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Slider],
  template: `
    <div class="grid gap-6 sm:grid-cols-2">
      <vig-slider label="Volume" [showValue]="true" [(value)]="volume" />
      <vig-slider label="Team size" [min]="1" [max]="20" [showValue]="true" [(value)]="team" />
    </div>
    <p class="text-sm text-muted-foreground">Volume: {{ volume() }} · Team: {{ team() }}</p>
  `,
})
export class SliderBasicDemo {
  protected readonly volume = signal<number | null>(40);
  protected readonly team = signal<number | null>(6);
}
```

## Value template

`valueTemplate` formats the shown value and `aria-valuetext`; `{0}` is replaced by the number formatted for the active locale, so units and decimals read correctly.

```typescript title="slider-value-template.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Slider } from '@vigdx/ui/slider';

@Component({
  selector: 'app-slider-value-template',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Slider],
  template: `
    <div class="grid gap-6 sm:grid-cols-2">
      <vig-slider
        label="Discount"
        valueTemplate="{0} %"
        [max]="50"
        [step]="5"
        [value]="15"
        [showValue]="true"
      />
      <vig-slider
        label="Search radius"
        valueTemplate="{0} km"
        [min]="0.5"
        [max]="10"
        [step]="0.5"
        [value]="2.5"
        [showValue]="true"
      />
    </div>
  `,
})
export class SliderValueTemplateDemo {}
```

## Ticks

`[ticks]="true"` draws a mark per step (up to 50 steps). An array of values draws labelled marks at those values instead. Ticks are visual only.

```typescript title="slider-ticks.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Slider } from '@vigdx/ui/slider';

@Component({
  selector: 'app-slider-ticks',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Slider],
  template: `
    <div class="grid gap-6 sm:grid-cols-2">
      <vig-slider
        label="Rating"
        [min]="1"
        [max]="5"
        [value]="4"
        [ticks]="true"
        [showValue]="true"
      />
      <vig-slider
        label="Monthly budget"
        valueTemplate="{0} USD"
        [max]="10000"
        [step]="500"
        [value]="2500"
        [ticks]="[0, 2500, 5000, 7500, 10000]"
        [showValue]="true"
      />
    </div>
  `,
})
export class SliderTicksDemo {}
```

## Empty, hidden label, disabled, read-only and error

An empty slider (`null`) rests at `min` until moved. `hideLabel` keeps the label for screen readers only. `readonly` keeps the thumb focusable but ignores changes. Errors show once the slider is `touched` and `invalid`.

```typescript title="slider-states.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Slider } from '@vigdx/ui/slider';

@Component({
  selector: 'app-slider-states',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Slider],
  template: `
    <div class="grid gap-6 sm:grid-cols-2">
      <vig-slider label="Not set yet" hint="Rests at the minimum until moved." />
      <vig-slider
        label="Brightness"
        hint="Label hidden, still announced."
        [value]="70"
        [hideLabel]="true"
      />
      <vig-slider label="Disabled" [value]="30" [disabled]="true" />
      <vig-slider label="Read-only" [value]="60" [readonly]="true" [showValue]="true" />
      <vig-slider
        label="Seats"
        [value]="5"
        [max]="50"
        [showValue]="true"
        [touched]="true"
        [invalid]="true"
        [errors]="[{ kind: 'min', message: 'Book at least 10 seats.' }]"
      />
    </div>
  `,
})
export class SliderStatesDemo {}
```

## Reactive Forms

Import `VigValueAccessor` next to the component to use `formControlName`, `[formControl]` or `ngModel`.

```typescript title="slider-reactive-forms.ts"
import { JsonPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
import { Button } from '@vigdx/ui/button';
import { VigValueAccessor } from '@vigdx/ui/core';
import { Slider } from '@vigdx/ui/slider';

@Component({
  selector: 'app-slider-reactive-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [JsonPipe, ReactiveFormsModule, Button, Slider, VigValueAccessor],
  template: `
    <form class="flex flex-col gap-6" [formGroup]="form">
      <div class="grid gap-6 sm:grid-cols-2">
        <vig-slider label="Bass" formControlName="bass" [min]="-10" [max]="10" [showValue]="true" />
        <vig-slider
          label="Treble"
          formControlName="treble"
          [min]="-10"
          [max]="10"
          [showValue]="true"
        />
      </div>
      <div class="flex gap-2">
        <vig-button
          label="Flat"
          variant="secondary"
          (click)="form.setValue({ bass: 0, treble: 0 })"
        />
      </div>
      <pre class="rounded-input bg-subtle p-3 font-mono text-xs">{{ form.value | json }}</pre>
    </form>
  `,
})
export class SliderReactiveFormsDemo {
  protected readonly form = new FormGroup({
    bass: new FormControl<number | null>(3),
    treble: new FormControl<number | null>(-2),
  });
}
```

## Signal Forms

`vig-slider` implements `FormValueControl<number | null>`. Schema `min()` and `max()` rules bind to the slider's range; other rules, such as this `validate()`, show their message once the slider is touched.

```typescript title="slider-signal-forms.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { FormField, form, max, min, validate } from '@angular/forms/signals';
import { Slider } from '@vigdx/ui/slider';

@Component({
  selector: 'app-slider-signal-forms',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [FormField, Slider],
  template: `
    <div class="max-w-sm">
      <vig-slider
        label="Price limit"
        valueTemplate="{0} €"
        hint="50 to 500 €, in steps of 20 €."
        [step]="10"
        [showValue]="true"
        [formField]="filters.price"
      />
    </div>
    <p class="text-sm text-muted-foreground">
      Valid: {{ filters().valid() ? 'yes' : 'no' }} · {{ model().price }}
    </p>
  `,
})
export class SliderSignalFormsDemo {
  protected readonly model = signal<{ price: number | null }>({ price: 120 });
  protected readonly filters = form(this.model, (path) => {
    // min() and max() also set the slider's range.
    min(path.price, 50);
    max(path.price, 500);
    validate(path.price, ({ value }) =>
      (value() ?? 0) % 20 === 0 ? undefined : { kind: 'even', message: 'Pick a multiple of 20 €.' },
    );
  });
}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="slider"][data-part="header"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="slider"]`

| Part | Element | data-state |
| --- | --- | --- |
| `header` | Row of the label and the value. |   |
| `label` | The `<label>`. |   |
| `value` | The formatted value (`showValue`). |   |
| `input` | The native range input: track and thumb are its pseudo-elements. |   |
| `ticks` | Row of tick marks (`ticks`). |   |
| `tick` | One tick mark with its label. |   |
| `hint` | The `vig-hint` (a `display: contents` host; style its `text` part). |   |
| `disabled-reason` | The `vig-hint` with the disabled reason (`display: contents`). |   |
| `error` | The `vig-field-error` (a `display: contents` host; style its `message` part). |   |

## Accessibility

- A native `<input type="range">`, exposed as a slider: the arrows change the value by `step`, Page Up and Page Down by larger steps, Home and End jump to `min` and `max`.
- It is named by a real `<label for>`, also with `hideLabel`. The printed value is hidden from screen readers because it is already announced through `aria-valuetext`.
- Use `valueTemplate` to add units, so "15 %" is announced rather than "15".
- `readonly` and an invalid state set `aria-readonly` and `aria-invalid`. The slider role does not support `aria-required`, so `required` adds a visually hidden "Required" (`requiredLabel`) to the description; hint and error are linked through `aria-describedby` too.
- The track is a light `--color-muted` bar inside a `--color-border-control` border: the border is at least 3:1 against the page like other control boundaries, and the fill at least 3:1 against the track. Forced-colors mode draws both with system colors.
- Sliders are hard to set precisely on touch screens; for exact numbers offer a number input instead or next to it.

## API

### Slider

Selector: `vig-slider` · Kind: component

Range slider built on the native `<input type="range">`, so keyboard (arrows, Page Up/Down, Home/End) and assistive-tech support come from the browser. Styled entirely with tokens: track `--color-muted` inside a `--color-border-control` border, fill `--color-primary`, thumb `--color-background` with a primary border. Empty is null; an empty slider rests at `min` until moved.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `dirty` | `boolean` | `false` | — |
| `disabled` | `boolean` | `false` | — |
| `disabledReasons` | `readonly WithOptionalFieldTree<DisabledReason>[]` | `[]` | — |
| `errorMessages` | `Record<string, string>` | — | — |
| `errors` | `readonly ValidationError.WithOptionalFieldTree[]` | `[]` | — |
| `hidden` | `boolean` | `false` | — |
| `hideLabel` | `boolean` | `false` | Keeps the label for assistive tech only. |
| `hint` | `string \| undefined` | `undefined` | — |
| `invalid` | `boolean` | `false` | — |
| `label` (required) | `string` | — | — |
| `max` | `number \| undefined` | `100` | — |
| `min` | `number \| undefined` | `0` | — |
| `name` | `string` | `''` | — |
| `pending` | `boolean` | `false` | — |
| `readonly` | `boolean` | `false` | — |
| `required` | `boolean` | `false` | — |
| `requiredLabel` | `string \| undefined` | `undefined` | Screen-reader text conveying `required`: the slider role does not support `aria-required`, so it is part of the description instead. |
| `showValue` | `boolean` | `false` | Shows the formatted value beside the label. |
| `step` | `number` | `1` | — |
| `ticks` | `boolean \| readonly number[]` | `false` | Tick marks under the track: `true` draws one per step (up to 50 steps), an array draws labelled marks at those values. |
| `touched` | `boolean` | `false` | — |
| `value` (model) | `number \| null` | `null` | — |
| `valueTemplate` | `string \| ((value: number) => string)` | `'{0}'` | Formats the value for display, `aria-valuetext` and tick labels: a function of the number, e.g. `(n) => n + ' km'`, or a string where `{0}` is replaced by the locale-formatted number, e.g. `'{0} %'`. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `touch` | `void` | — |
| `valueChange` | `number \| null` | — |

#### Methods

| Signature | Description |
| --- | --- |
| `focus(options?: FocusOptions): void` | Moves focus to the control's interactive element (the input, or the trigger of a popup control). Signal Forms calls it through `field().focusBoundControl()`; the host element itself cannot take focus. |
| `reset(): void` | Returns the control to its pristine look: drops in-progress text, closes an open popup, clears errors the component raised itself and the submitted state of `errorDisplay: 'interacted'`. Signal Forms calls it from `field().reset()`; `VigValueAccessor` from `control.reset()`. It never changes the value: the bound form owns that. |

### VigValueAccessor

Kind: directive

Bridges the vigdx form fields to Reactive Forms (`formControl`, `formControlName`) and template-driven forms (`ngModel`). Import it next to the component; Signal Forms (`[formField]`) need no accessor.

#### Methods

| Signature | Description |
| --- | --- |
| `registerOnChange(fn: (value: unknown) => void): void` | — |
| `registerOnTouched(fn: () => void): void` | — |
| `setDisabledState(disabled: boolean): void` | — |
| `writeValue(value: unknown): void` | — |
