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

# mds-header-bar - examples

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

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

#### Standard Header Bar with Logo and Navigation

The canonical form. Place branding content in the default slot (left side) and `<mds-button>` elements in the `nav` slot (right side). With the defaults (`menu="mobile"` and `nav="desktop"`) the nav is visible on desktop and the hamburger appears only on mobile.

```html
<mds-header-bar>
  <div class="flex gap-200 items-center">
    <mds-img src="./logo.svg" alt="Maggioli"></mds-img>
    <mds-text typography="h6">Portale Enti</mds-text>
  </div>
  <mds-button slot="nav" variant="dark" tone="outline">Accedi</mds-button>
  <mds-button slot="nav" variant="primary" tone="strong">Registrati</mds-button>
</mds-header-bar>
```

#### Nested Inside `mds-header`

`<mds-header-bar>` is a structural sub-component of [`mds-header`](../mds-header/AGENTS.md). Place it inside `<mds-header>` so that the hamburger click is wired to the drawer automatically; the component calls `mds-header.setOpened(true)` on its nearest `mds-header` ancestor.

```html
<mds-header>
  <mds-header-bar>
    <mds-text typography="h6">Gestione Pratiche</mds-text>
    <mds-button slot="nav" variant="dark" tone="text" icon="mi/baseline/person">Account</mds-button>
    <mds-button slot="nav" variant="primary" tone="strong">Nuovo documento</mds-button>
  </mds-header-bar>
  <!-- drawer and other header regions go here -->
</mds-header>
```

#### On the Frame of the Page

The content of the bar stands on the frame of the page. Put the `page-frame` utility on the content of the page, one element, and its edges meet the edges of the content of the bar at every width; write no width or gutter of your own.

```html
<mds-header>
  <mds-header-bar>
    <mds-text typography="h6">Gestione Pratiche</mds-text>
  </mds-header-bar>
</mds-header>
<main class="page-frame">
  <h1 class="text-title-page">Pratiche</h1>
</main>
```

#### Controlling Hamburger Visibility

Use the `menu` prop to decide when the hamburger button is shown (inside `mds-header`, set `menu` and `nav` on `mds-header`: it overwrites the bar's values when it loads). The default `mobile` is correct for most apps. Use `all` when you always want a drawer-style menu regardless of viewport, or `none` to remove the hamburger entirely (e.g. a public marketing bar with no drawer).

```html
<!-- Always show the hamburger (single-column layout, any viewport) -->
<mds-header-bar menu="all">
  <mds-text typography="h6">Area Personale</mds-text>
</mds-header-bar>

<!-- No hamburger at all (branding-only bar) -->
<mds-header-bar menu="none">
  <mds-img src="./logo.svg" alt="Maggioli"></mds-img>
</mds-header-bar>
```

#### Controlling Inline Navigation Visibility

Use the `nav` prop symmetrically with `menu`. The default `desktop` shows the nav slot only on wide viewports. Set `nav="all"` to always show inline links, `nav="mobile"` to show them only on small screens, or `nav="none"` to suppress the nav region entirely.

```html
<!-- Always show inline nav (no responsive collapsing) -->
<mds-header-bar menu="none" nav="all">
  <mds-img src="./logo.svg" alt="Maggioli"></mds-img>
  <mds-button slot="nav" variant="dark" tone="text">Chi siamo</mds-button>
  <mds-button slot="nav" variant="dark" tone="text">Servizi</mds-button>
  <mds-button slot="nav" variant="primary" tone="strong">Contatti</mds-button>
</mds-header-bar>
```

#### Listening for the Hamburger Open Event

Listen to `mdsHeaderBarOpen` (bubbles) to react when the user taps the hamburger - for example to track analytics or to open a custom side panel not managed by `mds-header`.

```html
<mds-header-bar id="main-bar">
  <mds-text typography="h6">Applicazione</mds-text>
</mds-header-bar>

<script>
  document.getElementById('main-bar').addEventListener('mdsHeaderBarOpen', () => {
    console.log('menu aperto');
  });
</script>
```

#### Programmatic Open via `setOpened`

To open or close the drawer from external code - for instance after route navigation in a single-page app - call `setOpened` on the surrounding `mds-header`, or on the bar, which hands it to its `mds-header`.

```html
<mds-header id="main-header">
  <mds-header-bar>
    <mds-text typography="h6">Dashboard</mds-text>
  </mds-header-bar>
  <div slot="menu">
    <mds-button label="Pratiche" variant="dark" tone="text"></mds-button>
  </div>
</mds-header>

<script>
  // Close the drawer after the user selects a menu item
  const header = document.getElementById('main-header');
  header.setOpened(false);
</script>
```

#### Styling the Hamburger Icon Color

`mds-header-bar` has no public CSS custom property. Inside an `mds-header`, the hamburger takes the header's public `--mds-header-color`: set it on the header. To color the hamburger of a bar used on its own, set `fill` on its documented `hamburger` part. Use a semantic color role wrapped with `rgb(var(--magma-<role>))` so dark-mode works automatically.

```css
mds-header {
  --mds-header-color: rgb(var(--magma-accent-fg));
}

mds-header-bar::part(hamburger) {
  fill: rgb(var(--magma-accent-fg));
}
```

#### Styling via Shadow Parts

The four documented shadow parts (`content`, `actions`, `nav`, `hamburger`) let you adjust layout without piercing the shadow DOM. Target only these documented parts.

```css
/* Increase the gap between the nav buttons and the hamburger */
mds-header-bar::part(actions) {
  gap: calc(var(--spacing) * 600);
}

/* Visually highlight the hamburger button in a branded theme */
mds-header-bar::part(hamburger) {
  border-radius: var(--magma-radius-md);
  background-color: rgb(var(--magma-accent-surface));
}
```
