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

# mds-pref-theme-item - examples

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

Correct and idiomatic ways to use the `<mds-pref-theme-item>` 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 Usage Inside the Parent

`<mds-pref-theme-item>` must always be a direct child of [`<mds-pref-theme>`](../mds-pref-theme/AGENTS.md). Each item represents one selectable colour theme; the parent manages selection state so all siblings stay in sync.

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

#### Auto-Derived Label

Omit `label` and let the component generate it from `name`: the first letter is capitalised and hyphens are replaced with spaces. Use this shorthand when the theme name is already human-readable.

```html
<mds-pref-theme name="business" scheme="all">
  <!-- renders "Business" and "Editorial" without explicit label props -->
  <mds-pref-theme-item name="business" scheme="all"></mds-pref-theme-item>
  <mds-pref-theme-item name="editorial" scheme="all"></mds-pref-theme-item>
</mds-pref-theme>
```

#### Scheme-Specific Themes

Set `scheme` to `"light"` or `"dark"` when a theme must be offered in a single colour mode only. The parent applies the forced mode when the item is selected.

```html
<mds-pref-theme name="business" scheme="all">
  <!-- this theme works in both modes -->
  <mds-pref-theme-item name="business" label="Gestionale" scheme="all"></mds-pref-theme-item>

  <!-- offered light-only; selecting it locks the UI in light mode -->
  <mds-pref-theme-item name="editorial" label="Editoriale chiaro" scheme="light"></mds-pref-theme-item>
</mds-pref-theme>
```

#### Listening to the Selection Event

Attach a listener for `mdsPrefThemeItemSelect` when you need to react to a selection at the item level - for example to log analytics. The detail carries `{ name, scheme }`. Normally the parent handles theme application automatically; only listen at this level for side-effects.

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

<script>
  document.querySelectorAll('mds-pref-theme-item').forEach((item) => {
    item.addEventListener('mdsPrefThemeItemSelect', (e) => {
      console.log('Tema selezionato:', e.detail.name, 'schema:', e.detail.scheme);
    });
  });
</script>
```

#### Setting the Initially Selected Item

Declare it on the parent, not with `selected`: when it loads, `<mds-pref-theme>` marks the item whose `name` matches the active theme (the stored choice, or its own `name` on a first visit) and overwrites any `selected` written in the markup.

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

#### CSS Customization via Documented Properties

Override the swatch only through the public `--mds-pref-theme-item-color-background` CSS custom property, the background behind the preview dots; the dots themselves are painted by the component. Set it on the host element or a parent selector and name a semantic role for the colour value, `rgb(var(--magma-<role>))` ([`agents/color.md`](../../../../agents/color.md)), so mode, theme and contrast keep working. It replaces the preview of the theme the item names.

```css
/* override the swatch background of one theme item */
mds-pref-theme-item[name="editorial"] {
  --mds-pref-theme-item-color-background: rgb(var(--magma-wash-strong));
}
```
