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

# mds-breadcrumb - examples

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

Correct and idiomatic ways to use the `<mds-breadcrumb>` 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 Navigation Trail

The canonical form. Slot one `<mds-breadcrumb-item>` per level and mark the current depth with `selected`. The back arrow is shown by default.

```html
<mds-breadcrumb>
  <mds-breadcrumb-item label="Home"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Archivio pratiche"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Pratica 2024/001" selected></mds-breadcrumb-item>
</mds-breadcrumb>
```

#### Without the Back Arrow

Add the `hide-back` attribute for display-only or shallow breadcrumbs where reverse navigation adds no value. The back arrow is shown by default, so this is an explicit opt-out.

```html
<mds-breadcrumb hide-back>
  <mds-breadcrumb-item label="Impostazioni"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Profilo utente" selected></mds-breadcrumb-item>
</mds-breadcrumb>
```

#### Listening for Navigation Changes

`mdsBreadcrumbChange` fires whenever a child item is clicked or the back arrow is activated. The detail carries the zero-based `id` string of the newly selected item and a `caller` element: the clicked item, or for the back arrow the item that was current before the step.

```html
<mds-breadcrumb id="nav">
  <mds-breadcrumb-item label="Dashboard"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Documenti"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Fatture" selected></mds-breadcrumb-item>
</mds-breadcrumb>

<script>
  document.getElementById('nav').addEventListener('mdsBreadcrumbChange', (e) => {
    console.log('Livello selezionato:', e.detail.id);
    console.log('Elemento:', e.detail.caller);
  });
</script>
```

#### Programmatic Selection

Set `selected` in the markup on the item you want active on load: the parent reads it to set the back button state (disabled on the first item). Setting `selected` later from JavaScript emits no event, so the parent neither clears the other items nor updates the back button.

```html
<mds-breadcrumb>
  <mds-breadcrumb-item label="Progetto Alpha" selected></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Sprint 3"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Attivita"></mds-breadcrumb-item>
</mds-breadcrumb>
```

#### Item Label via `label` Prop

Always set the text with the `label` prop on `<mds-breadcrumb-item>`: the item has no slot. The prop is reflected as an HTML attribute, enabling CSS attribute selectors and framework bindings.

```html
<mds-breadcrumb>
  <mds-breadcrumb-item label="Area riservata"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Gestione utenti"></mds-breadcrumb-item>
  <mds-breadcrumb-item label="Nuovo utente" selected></mds-breadcrumb-item>
</mds-breadcrumb>
```

#### Styling Customization via CSS Custom Properties

`mds-breadcrumb` has no public CSS custom property: style it with its props, the `button` part of its items and the semantic roles. The item colors, the selected item's included, and the separator arrow follow the semantic color roles of the theme, so dark mode and high-contrast modes keep working with no override. To retheme the whole trail at once, target the `button` part of every child `<mds-breadcrumb-item>` from a parent selector: the part is the item's `<mds-button>`, so it takes the public `--mds-button-*` custom properties. Use semantic color roles wrapped in `rgb(var(--magma-<role>))`.

```css
.sidebar-nav mds-breadcrumb-item::part(button) {
  --mds-button-color: rgb(var(--magma-accent-fg));
}
.sidebar-nav mds-breadcrumb-item[selected]::part(button) {
  --mds-button-color: rgb(var(--magma-text-default));
}
```

#### Per-Item Styling via `::part(button)`

When you need to restyle a single item's inner button beyond what the CSS vars allow, target the documented `::part(button)` on `<mds-breadcrumb-item>`. This is the only supported shadow-part surface on the child component.

```css
mds-breadcrumb-item[selected]::part(button) {
  font-weight: bold;
  letter-spacing: 0.02em;
}
```
