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

# mds-text - examples

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

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

#### Body Text via Default Slot

The canonical form for body copy. Slot a plain text string; `typography` defaults to `'detail'`, which renders a `<p>` element with the detail style.

```html
<mds-text>Inserisci il testo del messaggio nel campo sottostante.</mds-text>
```

#### A Title by Its Role

Write a title by what it is: `page`, `section`, `dialog`, `part` or `group`. Each takes the style of its role (the page as `h3`, a section and a dialog as `h5`, a part and a group as `h6`), and `tag` gives the level of its place in the outline: one `h1`, the title of the page.

```html
<mds-text typography="page" tag="h1">Pratiche di residenza</mds-text>
<mds-text typography="section" tag="h2">Dati del richiedente</mds-text>
<mds-text typography="part" tag="h3">Indirizzo di residenza</mds-text>
```

#### Choosing a Typography Role

Set `typography` to match the semantic role. The component resolves the correct HTML element automatically - `h1`-`h6` emit heading elements, a title role emits the heading of the level it is styled as, `paragraph`/`detail` emit `<p>`, `label`/`option` emit `<label>`, `snippet`/`hack` emit `<code>`, `tip` and `action` emit `<div>` and `<span>` respectively.

```html
<mds-text typography="page" tag="h1">Benvenuto nel portale</mds-text>
<mds-text typography="section" tag="h2">Impostazioni account</mds-text>
<mds-text typography="paragraph">Questo e' il corpo principale della pagina.</mds-text>
<mds-text typography="caption">Nota: i campi obbligatori sono contrassegnati con *</mds-text>
<mds-text typography="label">Nome utente</mds-text>
<mds-text typography="tip">Usa almeno 8 caratteri</mds-text>
```

#### Text via `text` Prop

Use the `text` prop when the string comes from a JavaScript expression or data binding. This is also required for the `yugop` animation (see below) since the animation watches the prop value.

```html
<mds-text typography="detail" text="Ultimo accesso: 3 giugno 2026"></mds-text>
```

#### Reading Variant

Apply `variant="read"` to `paragraph`, `detail`, or `caption` when rendering long-form editorial content. The reading variant keeps the same sizes but switches to the serif reading font (lighter for `paragraph` and `detail`), optimized for sustained reading.

```html
<mds-text typography="paragraph" variant="read">
  Il regolamento definisce le modalita' di accesso ai servizi digitali
  dell'amministrazione pubblica, in conformita' con le vigenti normative europee.
</mds-text>

<mds-text typography="detail" variant="read">
  Fonte: Gazzetta Ufficiale, serie generale n. 128 del 4 giugno 2026.
</mds-text>
```

#### A Link Inside the Text

The default slot takes plain text and one component, `<mds-link>`: a word or a phrase that links, with the font of the typography role, wrapping with the text around it.

```html
<mds-text typography="paragraph">
  Leggi l'<mds-link href="/privacy">informativa sulla privacy</mds-link> prima di continuare.
</mds-text>
```

#### Monospaced Code Styles

Use `typography="snippet"` for inline code values or short terminal output. Use `typography="hack"` for a smaller monospaced size suited to code-dense interfaces.

```html
<mds-text typography="snippet">SELECT * FROM utenti WHERE attivo = true;</mds-text>
<mds-text typography="hack">npm install @maggioli-design-system/magma</mds-text>
```

#### Overriding the Rendered Tag

`tag` is an escape hatch to pin a specific HTML element when the `typography` default is semantically wrong - for example, rendering a visually large heading as a `<span>` for a non-heading context, or marking inline text as `<strong>` or `<em>` without changing the visual scale.

```html
<!-- Visually h2 scale but not a landmark heading -->
<mds-text typography="h2" tag="span">Sezione in evidenza</mds-text>

<!-- Emphasized inline detail without changing scale -->
<mds-text typography="detail" tag="strong">Attenzione: questa operazione e' irreversibile.</mds-text>

<!-- Marked text using caption scale -->
<mds-text typography="caption" tag="mark">Modificato il 3 giugno 2026</mds-text>
```

#### Single-Line Truncation

Use `truncate="word"` to clamp text to a single line with an ellipsis when layout space is constrained, for example inside a table cell or card header.

```html
<div style="width: 200px;">
  <mds-text typography="detail" truncate="word">
    Titolo molto lungo che supera la larghezza disponibile del contenitore
  </mds-text>
</div>
```

#### Multi-Line Clamp

Use `truncate="all"` to clamp text to a fixed number of lines. Control the number of visible lines via `--mds-text-line-clamp` (default 3).

```html
<mds-text typography="paragraph" truncate="all" style="--mds-text-line-clamp: 2;">
  Questo paragrafo mostra solo le prime due righe di testo, troncando il resto
  con i puntini di sospensione indipendentemente dai caratteri di confine.
</mds-text>
```

#### Animated Character Reveal

Set `animation="yugop"` together with the `text` prop to run a randomized character-decode reveal each time `text` changes; the initial value is rendered without animation. Tune the speed via `--mds-text-animation-speed`.

```html
<mds-text
  typography="h3"
  animation="yugop"
  text="Benvenuto nel sistema"
  style="--mds-text-animation-speed: 1;"
></mds-text>
```

#### Custom Line-Clamp Count

Set `--mds-text-line-clamp` on the component host (or a parent selector) to vary the clamp depth per context without duplicating attributes.

```css
.abstract mds-text {
  --mds-text-line-clamp: 4;
}

.anteprima mds-text {
  --mds-text-line-clamp: 2;
}
```
