<!--
  GENERATED FILE - do not edit by hand.
  Source of truth: src/components/mds-input-range/usage/ in the magma monorepo.
-->

# mds-input-range - examples

> The API of this component: [`AGENTS.md`](AGENTS.md).

Correct and idiomatic ways to use the `<mds-input-range>` component, ordered from most common to most specialized. The examples assume a working knowledge of the shared component rules in [`agents/conventions.md`](../../../../agents/conventions.md).

#### Basic Slider with a Label

The minimum viable form. Put the field's text label in the default slot - it is rendered in the header and used as the accessible name for the underlying input. The default range is 0-100 with a step of 1.

```html
<mds-input-range name="volume">Volume</mds-input-range>
```

#### Setting `min`, `max`, and `step`

Constrain the range to a meaningful interval. Use `step` to enforce granularity - the component snaps the selected value to the nearest multiple automatically.

```html
<!-- Percentage slider, 5-point increments -->
<mds-input-range name="completamento" min="0" max="100" step="5">Completamento (%)</mds-input-range>

<!-- Temperature slider, half-degree steps -->
<mds-input-range name="temperatura" min="16" max="30" step="0.5">Temperatura (C)</mds-input-range>
```

#### Pre-selected Value via `value`

Pass `value` to initialize the thumb at a specific position. The prop is reflected, so it also stays in sync when the user drags.

```html
<mds-input-range name="priorita" min="1" max="10" value="7">Priorita</mds-input-range>
```

#### Listening to `mdsInputRangeChange`

The component emits `mdsInputRangeChange` with the new numeric value in `event.detail` whenever the committed value changes. Do not listen to the raw `input` or `change` events - they may not bubble out of shadow DOM as expected.

```html
<mds-input-range id="rating" name="valutazione" min="1" max="5">Valutazione</mds-input-range>

<script>
  document.querySelector('#rating').addEventListener('mdsInputRangeChange', (event) => {
    console.log('Nuovo valore:', event.detail);
  });
</script>
```

#### Custom Value Display with `formatValue`

Supply a `(value: number) => string` function to `formatValue` to change how the current value is shown in the header label - for example to add a unit suffix, format as currency, or humanize byte counts. The underlying numeric `value` is not affected.

```html
<mds-input-range id="prezzo" name="prezzo" min="0" max="10000" step="50">Prezzo massimo</mds-input-range>

<script>
  document.querySelector('#prezzo').formatValue = (v) =>
    v.toLocaleString('it-IT', { style: 'currency', currency: 'EUR' });
</script>
```

#### Disabled Slider

Set the boolean `disabled` attribute to prevent interaction. The component applies disabled visual tokens and clears the reported form value automatically. Do not use `disabled="false"` to re-enable - remove the attribute instead.

```html
<mds-input-range name="soglia" min="0" max="100" value="40" disabled>Soglia (disabilitato)</mds-input-range>
```

#### Form Participation

`<mds-input-range>` is form-associated. Place it inside a `<form>` with a `name` attribute and the selected value is submitted automatically alongside other fields.

```html
<form action="/impostazioni" method="post">
  <mds-input-range name="luminosita" min="0" max="100" value="75">Luminosita</mds-input-range>
  <mds-input-range name="contrasto" min="0" max="100" value="50">Contrasto</mds-input-range>

  <mds-button type="submit" label="Salva impostazioni" variant="primary" tone="strong"></mds-button>
</form>
```

#### Several Values: an Interval

Put an [`<mds-input-range-value>`](../mds-input-range-value/AGENTS.md) per value inside the range: a thumb each. The text of the range is the label of the group, the text of each value names its thumb. The header shows the two values as an interval, formatted by `formatValue` as the one value.

```html
<mds-input-range id="prezzo-intervallo" min="0" max="500" step="10" name="prezzo">
  Prezzo
  <mds-input-range-value value="100">Minimo</mds-input-range-value>
  <mds-input-range-value value="400">Massimo</mds-input-range-value>
</mds-input-range>

<script>
  document.querySelector('#prezzo-intervallo').formatValue = (v) =>
    v.toLocaleString('it-IT', { style: 'currency', currency: 'EUR' });
</script>
```

The form receives one entry per value under `name`, in order: `prezzo=100&prezzo=400`, read on the server as a list, as the entries of a `<select multiple>`.

#### Several Values: Listening to `mdsInputRangeValuesChange`

With values, the range emits `mdsInputRangeValuesChange`, whose `detail.values` lists them in order from the lowest. It fires whenever the list changes: a thumb moved, a value set from code, a value added or removed, a form reset.

```html
<mds-input-range id="scala" min="1" max="21" step="0.5" name="contrasto">
  Scala di contrasto
  <mds-input-range-value value="3"></mds-input-range-value>
  <mds-input-range-value value="4.5"></mds-input-range-value>
  <mds-input-range-value value="7"></mds-input-range-value>
  <mds-input-range-value value="12"></mds-input-range-value>
</mds-input-range>

<script>
  document.querySelector('#scala').addEventListener('mdsInputRangeValuesChange', (event) => {
    console.log('Passi:', event.detail.values); // [3, 4.5, 7, 12]
  });
</script>
```

The values without text are named after the label and their position ("Scala di contrasto, 2 di 4"). Adding a stop is appending an `<mds-input-range-value>`, removing one is removing it.

#### Hiding the Header via `::part(header)`

Use `::part(header)` to hide the label-and-value header when the surrounding UI already provides that context - for example in a compact settings row where the label lives outside the component.

```css
.slider-compatto mds-input-range::part(header) {
  display: none;
}
```

#### Styling Customization

`<mds-input-range>` has no public CSS custom property: the thumb and the track take their sizes from the component and their colours from the semantic color roles ([`agents/color.md`](../../../../agents/color.md)), the progress from the accent, so themes and dark mode recolour it without overrides. Adjust the slider only through its documented `header` and `track` parts, as in the example above.
