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

# mds-pref-theme - examples

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

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

#### Basic Theme Chooser

The canonical form. Place `<mds-pref-theme>` inside [`<mds-pref>`](../mds-pref/AGENTS.md) and populate its default slot with one [`<mds-pref-theme-item>`](../mds-pref-theme-item/AGENTS.md) per available theme. The item whose `name` matches the active theme (the stored choice, or the parent's `name` on a first visit) starts as selected. The named themes are `business`, the base theme of the management software, and `editorial`, the theme of the publishing side.

```html
<mds-pref>
  <mds-pref-theme name="business" scheme="all">
    <mds-pref-theme-item label="Gestionale" name="business" scheme="all"></mds-pref-theme-item>
    <mds-pref-theme-item label="Editoriale" name="editorial" scheme="all"></mds-pref-theme-item>
  </mds-pref-theme>
</mds-pref>
```

#### Scheme-Constrained Themes

A theme made for one colour mode says so in its file (`"modes": ["dark"]` in `color`), and the component applies it in that mode with no attribute on the item. For a theme whose file says nothing, use `scheme="light"` or `scheme="dark"` on a `<mds-pref-theme-item>`. Either way the component writes the scheme on `<html>` and forces that mode even if the user's OS or mode preference asks for the opposite, and `<mds-pref-mode>` shows that mode with its other items locked.

```html
<mds-pref>
  <mds-pref-theme name="business" scheme="all">
    <mds-pref-theme-item label="Gestionale" name="business" scheme="all"></mds-pref-theme-item>
    <!-- light only - dark mode will not activate with this item -->
    <mds-pref-theme-item label="Editoriale chiaro" name="editorial" scheme="light"></mds-pref-theme-item>
  </mds-pref-theme>
</mds-pref>
```

#### Listening for the Change Event

React to the user's selection via `mdsPrefThemeChange`. The event detail carries `{ name, scheme }` - use them to update any app-level state that tracks the active theme.

```html
<mds-pref>
  <mds-pref-theme id="theme-chooser" name="business" scheme="all">
    <mds-pref-theme-item label="Gestionale" name="business" scheme="all"></mds-pref-theme-item>
    <mds-pref-theme-item label="Editoriale" name="editorial" scheme="all"></mds-pref-theme-item>
  </mds-pref-theme>
</mds-pref>

<script>
  document.querySelector('#theme-chooser').addEventListener('mdsPrefThemeChange', (e) => {
    console.log('Tema attivo:', e.detail.name, '- Schema:', e.detail.scheme);
  });
</script>
```

#### Declaring the Initial Theme

Use the `name` prop to declare which theme should be active before the user makes a selection. If `localStorage` already holds a persisted value it takes precedence; otherwise the component applies `name` on first render.

```html
<mds-pref>
  <!-- First-visit default is "editorial" -->
  <mds-pref-theme name="editorial" scheme="all">
    <mds-pref-theme-item label="Gestionale" name="business" scheme="all"></mds-pref-theme-item>
    <mds-pref-theme-item label="Editoriale" name="editorial" scheme="all"></mds-pref-theme-item>
  </mds-pref-theme>
</mds-pref>
```

#### Controlling Item Size

Pass `size="sm"` when the preferences panel must fit a compact layout: on `<mds-pref>`, which forwards it to every preference control.

```html
<mds-pref size="sm">
  <mds-pref-theme name="business" scheme="all">
    <mds-pref-theme-item label="Gestionale" name="business" scheme="all"></mds-pref-theme-item>
    <mds-pref-theme-item label="Editoriale" name="editorial" scheme="all"></mds-pref-theme-item>
  </mds-pref-theme>
</mds-pref>
```

#### Styling Theme Items

Style theme items only through their public `--mds-pref-theme-item-*` CSS custom properties. Set them on the item host or on a parent selector; name a semantic role, `rgb(var(--magma-<role>))` ([`agents/color.md`](../../../../agents/color.md)), so the colours follow the mode, the theme and the contrast preference.

```css
mds-pref-theme-item {
  --mds-pref-theme-item-color-background: rgb(var(--magma-surface-inverse));
}
```
