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

# mds-link - examples

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

Correct and idiomatic ways to use the `<mds-link>` 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).

#### A Link Inside a Sentence

The canonical form: the link sits in the default slot of an `<mds-text>`, with its text in its own default slot, and takes the font of the paragraph.

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

#### Any Typography

The link has no typography of its own: the same markup works in a caption, a title or the reading variant.

```html
<mds-text typography="caption">
  Fonte: <mds-link href="/report">rapporto annuale 2025</mds-link>
</mds-text>
<mds-text typography="paragraph" variant="read">
  Il testo completo e' nell'<mds-link href="/regolamento">allegato A</mds-link>.
</mds-text>
```

#### An Inline Action

Without `href` the link is an inline button: listen for `click` on the host, which a click, Enter and Space all fire.

```html
<mds-text typography="paragraph">
  Non trovi quello che cerchi? <mds-link id="help">Chiedi aiuto</mds-link> al supporto.
</mds-text>

<script>
  document.getElementById('help').addEventListener('click', () => openHelp());
</script>
```

#### A Link That Opens a New Tab

With `target="blank"` the component adds `rel="noopener noreferrer"` and tells the assistive technology the link opens a new tab; the icon is a choice of the design.

```html
<mds-text typography="detail">
  Consulta il
  <mds-link href="https://example.com/report.pdf" target="blank" icon="mi/baseline/open-in-new">rapporto completo</mds-link>.
</mds-text>
```

#### A Link That Downloads Its File

`download` saves the file instead of showing it in the tab; its value is the name of the saved file. The URL has to be on the origin of the page.

```html
<mds-text typography="detail">
  Scarica i colori come
  <mds-link href="/downloads/colors.xml" download="colors.xml" icon="mi/baseline/download">file XML</mds-link>.
</mds-text>
```

#### An Icon Before the Text

Use `icon-position="left"` for an icon that leads, such as a back arrow.

```html
<mds-text typography="paragraph">
  <mds-link href="/indice" icon="mi/baseline/arrow-back" icon-position="left">Torna all'indice</mds-link>
</mds-text>
```

#### A Link on a Colored Surface

On a status or filled surface the accent loses contrast: `variant="inherit"` takes the color of the text, and the underline still marks the link.

```html
<mds-banner variant="error" headline="Pagamento rifiutato">
  <mds-text typography="detail">
    <mds-link href="/carta" variant="inherit">Aggiorna la carta</mds-link> per riprovare.
  </mds-text>
</mds-banner>
```

#### Routing in a Single-Page App

Register the router once: plain clicks on every `mds-link` (and `mds-button` with `href`) go to it, with the browser navigation already cancelled; modifier clicks, other origins, `target="blank"` and `download` stay with the browser.

```javascript
import { NavigationService } from '@maggioli-design-system/magma/services';

NavigationService.setHandler((href) => router.push(href));
```

#### Styling Customization

Change the color through `--mds-link-color`, with a semantic role, so dark mode, named themes and high contrast keep working - see [`agents/color.md`](../../../../agents/color.md).

```css
.legal-note mds-link {
  --mds-link-color: rgb(var(--magma-text-muted));
}
```
