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

# mds-pref-consumption - examples

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

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

#### Default Use Inside `mds-pref`

The canonical form. Place `<mds-pref-consumption>` as a direct slot child of [`<mds-pref>`](../mds-pref/AGENTS.md). Omit `mode` so the component restores the persisted user choice automatically; the parent forwards its own `size` to every `mds-pref-*` child.

```html
<mds-pref>
  <mds-pref-consumption></mds-pref-consumption>
</mds-pref>
```

#### Forcing an Initial Mode

Set `mode` explicitly only when the application needs to override the persisted value - for example, when seeding preferences from a server-side profile. All three accepted values are `"low"`, `"medium"`, and `"high"`.

```html
<mds-pref>
  <mds-pref-consumption mode="low"></mds-pref-consumption>
</mds-pref>
```

#### Controlling Size

Use the `size` prop on the control itself when it stands outside `<mds-pref>`; inside it, set `size` on the parent, which forwards it to every child. Accepted values are `"sm"` and `"md"`.

```html
<mds-pref-consumption size="sm"></mds-pref-consumption>
```

#### Reacting to Mode Changes

Listen for the `mdsPrefChange` event to detect when the user picks a new consumption level. The event detail always carries `{ preference: "consumption" }`; the event fires once per change, from a pick or from a new `mode` set by a script, and not on page load.

```html
<mds-pref-consumption id="consumption-pref"></mds-pref-consumption>

<script>
  document
    .querySelector('#consumption-pref')
    .addEventListener('mdsPrefChange', (event) => {
      console.log('Preferenza consumo cambiata:', event.detail.preference);
    });
</script>
```

#### Reading the Active Mode After a Change

The `mode` prop is mutable and reflected as an attribute. The event is emitted once the new mode is applied and stored, so inside the handler the element already holds the new value.

```html
<mds-pref-consumption id="consumption-pref"></mds-pref-consumption>

<script>
  const pref = document.querySelector('#consumption-pref');
  pref.addEventListener('mdsPrefChange', () => {
    console.log('Modalita attiva:', pref.mode);
  });
</script>
```
