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

# mds-accordion-item - examples

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

Correct and idiomatic ways to use the `<mds-accordion-item>` 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 Accordion

The canonical form. Place one or more `<mds-accordion-item>` elements as direct children of [`<mds-accordion>`](../mds-accordion/AGENTS.md). Provide `label` for the header text and put the body content in the default slot.

```html
<mds-accordion>
  <mds-accordion-item label="Cos'e' Magma?">
    <mds-text>Magma e' il design system di Maggioli, basato su web components.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Come si installa?">
    <mds-text>Importa il loader e chiama defineCustomElements() nell'entry point.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="E' compatibile con React?">
    <mds-text>Si', usa il pacchetto @maggioli-design-system/magma-react.</mds-text>
  </mds-accordion-item>
</mds-accordion>
```

#### Panel Open by Default

Set `selected` on the item you want pre-expanded. The parent does not resolve conflicts on load, so in single mode set it on one item only, unless `multiple` is also set on the parent.

```html
<mds-accordion>
  <mds-accordion-item label="Introduzione" selected>
    <mds-text>Questa sezione e' aperta per impostazione predefinita.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Prerequisiti">
    <mds-text>Node.js 18+ e un package manager npm o pnpm.</mds-text>
  </mds-accordion-item>
</mds-accordion>
```

#### Multiple Panels Open Simultaneously

Add `multiple` to `<mds-accordion>` so the user can expand more than one panel at once. Individual items do not change - the parent mode governs the behaviour.

```html
<mds-accordion multiple>
  <mds-accordion-item label="Configurazione di base">
    <mds-text>Imposta i token di colore e il tema nell'entry point dell'applicazione.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Configurazione avanzata" selected>
    <mds-text>Personalizza i CSS custom properties per adattare il tema al brand.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Integrazione CI/CD" selected>
    <mds-text>Configura il job di build per generare documentation.json e readme.md.</mds-text>
  </mds-accordion-item>
</mds-accordion>
```

#### Non-closable Accordion

Set the parent `disable-close` attribute to prevent the user from collapsing an open panel. An open item then stays open until another item is clicked; with `multiple`, the last open item cannot be closed. Because `disableClose` defaults to `false`, just add the boolean attribute to enable mandatory selection.

```html
<mds-accordion disable-close>
  <mds-accordion-item label="Passo 1 - Installazione" selected>
    <mds-text>Esegui npm install @maggioli-design-system/magma per aggiungere il pacchetto.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Passo 2 - Configurazione">
    <mds-text>Importa defineCustomElements e registra i componenti nell'app.</mds-text>
  </mds-accordion-item>
</mds-accordion>
```

#### The Title of an Item Is a Part

An item is a part of the page, whose title is styled as `h6` (`titles/part`): write `typography="h6"`, until the default moves from `h5` with the next major (#1218). `typography` is the style only; `tag` gives the level of the title in the outline, one below the title of the section that holds the accordion (`h3` under an `h2`). Use `action` for a compact, control-like header.

```html
<mds-text typography="section" tag="h2">Domande frequenti</mds-text>
<mds-accordion>
  <mds-accordion-item label="Come gestisco gli aggiornamenti?" typography="h6" tag="h3">
    <mds-text>Ogni release e' documentata nel changelog con note di migrazione.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Posso personalizzare i colori?" typography="h6" tag="h3">
    <mds-text>Si', tramite i CSS custom properties esposti da ogni componente.</mds-text>
  </mds-accordion-item>
</mds-accordion>
```

The heading wraps the button that opens the item, as the Accordion pattern of the WAI-ARIA Authoring Practices does: a screen reader lists the questions among the headings of the page, under Domande frequenti. Left out, `tag` leaves the item a button only, out of the outline; never write the level of the style (`tag="h6"` under an `h2`), which skips the levels in between.

#### Rich Slot Content

The default slot accepts any HTML or components - not just text. Use it for structured content such as lists, forms, or nested components.

```html
<mds-accordion>
  <mds-accordion-item label="Dettagli del progetto">
    <mds-text typography="h6">Tecnologie utilizzate</mds-text>
    <mds-list>
      <mds-list-item>StencilJS</mds-list-item>
      <mds-list-item>TypeScript</mds-list-item>
      <mds-list-item>Storybook</mds-list-item>
    </mds-list>
  </mds-accordion-item>
  <mds-accordion-item label="Contatti del team">
    <mds-text>Scrivi a design-system@maggioli.it per supporto tecnico.</mds-text>
  </mds-accordion-item>
</mds-accordion>
```

#### Listening to Item Events

`mdsAccordionItemChange` fires on every toggle; `mdsAccordionItemSelect` fires only on open; `mdsAccordionItemUnselect` fires only on close. Use `mdsAccordionItemChange` when you only need to react to any state change, or the specific event when you need to handle open and close differently. The parent overwrites each item's `id` with `item-<index>` on load, and that is the `detail.id`: do not select items by an `id` of your own.

```html
<mds-accordion id="faq">
  <mds-accordion-item label="Domanda frequente 1">
    <mds-text>Risposta alla prima domanda.</mds-text>
  </mds-accordion-item>
  <mds-accordion-item label="Domanda frequente 2">
    <mds-text>Risposta alla seconda domanda.</mds-text>
  </mds-accordion-item>
</mds-accordion>

<script>
  document.querySelector('#faq').addEventListener('mdsAccordionItemChange', (e) => {
    console.log('item changed:', e.detail.id, 'selected:', e.detail.selected);
  });

  document.querySelector('#faq mds-accordion-item').addEventListener('mdsAccordionItemSelect', (e) => {
    console.log('faq-1 opened');
  });
</script>
```

#### Styling Customization

Style each item only through its public `--mds-accordion-item-*` CSS custom properties. Setting the public `--mds-accordion-*` properties on the parent `<mds-accordion>` propagates to all children via the cascade variables the items consume.

```css
/* Customize all items via the parent */
mds-accordion {
  --mds-accordion-padding-selected: calc(var(--spacing) * 600) 0 calc(var(--spacing) * 800) 0;
  --mds-accordion-padding-unselected: calc(var(--spacing) * 400) 0;
}

/* Or target a single item directly */
.sidebar mds-accordion-item {
  --mds-accordion-item-color: rgb(var(--magma-text-default));
}
```
