[Tutti i componenti](components.md)

# Tema dell'host

Segue il tema chiaro o scuro dell'host. Scrive `data-theme` sull'elemento `<html>`, così il tuo foglio di stile può reagire con `:root[data-theme='dark'] { ... }`, e lì imposta anche `color-scheme`. Questa seconda parte è importante: quando lo schema di colori della tua pagina è diverso da quello dell'host, il browser dipinge il riquadro di bianco invece di lasciarlo trasparente. Copia anche le variabili di stile che l'host invia (colori, font) sull'elemento `<html>`, così il tuo CSS può usarle con un fallback: `color: var(--color-text-primary, #16203a)`. Lascia trasparente lo sfondo della pagina: lo disegna l'host.

(Qui la pagina HTML mostra una demo dal vivo: https://umb0o.github.io/ngx-mcp-apps/it/component-mcp-theme.html)

**Uso:** add `McpTheme` to the `hostDirectives` of your root component, or put `mcpTheme` on any element.

File: [`mcp-theme.ts`](../components/mcp-theme.ts)

```ts
/**
 * Host theme
 *
 * Follows the light or dark theme of the host. It writes `data-theme` on the `<html>` element, so your
 * stylesheet can react with `:root[data-theme='dark'] { ... }`, and sets `color-scheme` there too. That
 * second part matters: when the colour scheme of your page differs from the one of the host, the browser
 * paints the frame white instead of leaving it transparent. It also copies the style variables the host
 * sends (colours, fonts) to the `<html>` element, so your CSS can use them with a fallback:
 * `color: var(--color-text-primary, #16203a)`. Keep the page background transparent: the host draws it.
 *
 * Usage: add `McpTheme` to the `hostDirectives` of your root component, or put `mcpTheme` on any element.
 */
import { DOCUMENT } from '@angular/common';
import { Directive, effect, inject } from '@angular/core';
import { McpApp } from 'ngx-mcp-apps';

@Directive({
  selector: '[mcpTheme]',
  standalone: true,
})
export class McpTheme {
  constructor() {
    const mcp = inject(McpApp);
    const root = inject(DOCUMENT).documentElement;
    effect(() => {
      const theme = mcp.theme();
      // No theme from the host (or standalone): leave the choice to the browser
      if (theme) root.setAttribute('data-theme', theme);
      else root.removeAttribute('data-theme');
      root.style.colorScheme = theme ?? 'light dark';
    });

    // The style variables of the host, as they come. The ones of the previous context are removed first
    let applied: string[] = [];
    effect(() => {
      const variables = mcp.hostContext()?.styles?.variables ?? {};
      for (const name of applied) root.style.removeProperty(name);
      applied = [];
      for (const [name, value] of Object.entries(variables)) {
        if (name.startsWith('--') && typeof value === 'string' && value) {
          root.style.setProperty(name, value);
          applied.push(name);
        }
      }
    });
  }
}
```


