# Accordion

Stacked sections that each show or hide their content. Use it to shorten long pages such as FAQs or settings, where readers only need a few sections at a time.

- Package: [@vigdx/ui](https://www.npmjs.com/package/@vigdx/ui)
- Selectors: `vig-accordion`, `vig-accordion-item`, `[vigAccordionHeader]`
- Source: [libs/ui/src/lib/organisms/accordion/accordion.ts](https://github.com/bakyurekk/vigdx/blob/main/libs/ui/src/lib/organisms/accordion/accordion.ts)

```typescript title="Import"
import { Accordion, AccordionItem, AccordionHeader } from '@vigdx/ui/accordion';
```

## Basic

Each `vig-accordion-item` needs a unique `value` and a `heading`; the projected content is its panel. By default opening one item closes the others.

```typescript title="accordion-basic.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Accordion, AccordionItem } from '@vigdx/ui/accordion';

@Component({
  selector: 'app-accordion-basic',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Accordion, AccordionItem],
  template: `
    <vig-accordion class="w-full max-w-xl">
      <vig-accordion-item value="shipping" heading="How long does shipping take?">
        Orders ship within two business days and arrive in three to five days.
      </vig-accordion-item>
      <vig-accordion-item value="returns" heading="Can I return an item?">
        Unused items can be returned within 30 days for a full refund.
      </vig-accordion-item>
      <vig-accordion-item value="warranty" heading="Is there a warranty?">
        Every product carries a two-year manufacturer warranty.
      </vig-accordion-item>
    </vig-accordion>
  `,
})
export class AccordionBasicDemo {}
```

## Multiple open items

`multiple` lets several items stay open. `[(value)]` holds the values of the open items, so you can preset them or open and close all of them from outside.

```typescript title="accordion-multiple.ts"
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { Accordion, AccordionItem } from '@vigdx/ui/accordion';
import { Button } from '@vigdx/ui/button';

@Component({
  selector: 'app-accordion-multiple',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Accordion, AccordionItem, Button],
  template: `
    <div class="flex flex-wrap gap-2">
      <vig-button label="Expand all" variant="secondary" size="sm" (click)="open.set(all)" />
      <vig-button label="Collapse all" variant="secondary" size="sm" (click)="open.set([])" />
    </div>
    <vig-accordion class="w-full max-w-xl" [multiple]="true" [(value)]="open">
      <vig-accordion-item value="season" heading="Season">
        The park opens on 1 April and closes on 31 October.
      </vig-accordion-item>
      <vig-accordion-item value="pricing" heading="Pricing">
        Day tickets cost €39; children under six enter free.
      </vig-accordion-item>
      <vig-accordion-item value="hours" heading="Opening hours">
        Weekdays 10:00–18:00, weekends 09:00–20:00.
      </vig-accordion-item>
    </vig-accordion>
    <p class="text-sm text-muted-foreground">Open: {{ open().join(', ') || 'none' }}</p>
  `,
})
export class AccordionMultipleDemo {
  protected readonly all = ['season', 'pricing', 'hours'];
  protected readonly open = signal<readonly string[]>(['season', 'hours']);
}
```

## Custom header and disabled

An `ng-template vigAccordionHeader` inside an item replaces the text header, e.g. with an icon and a badge. `disabled` items cannot be toggled and are skipped by the arrow keys. `headingLevel` (2–6, default 3) fits the headers into the page outline.

```typescript title="accordion-custom-header.ts"
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { Accordion, AccordionHeader, AccordionItem } from '@vigdx/ui/accordion';
import { Badge } from '@vigdx/ui/badge';
import { Icon } from '@vigdx/ui/icon';

@Component({
  selector: 'app-accordion-custom-header',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [Accordion, AccordionHeader, AccordionItem, Badge, Icon],
  template: `
    <vig-accordion class="w-full max-w-xl" [headingLevel]="3">
      <vig-accordion-item value="alerts">
        <ng-template vigAccordionHeader>
          <span class="flex items-center gap-2">
            <vig-icon name="warning" class="text-warning" />
            Alerts
            <vig-badge label="3" variant="warning" />
          </span>
        </ng-template>
        Two invoices are overdue and one card expires this month.
      </vig-accordion-item>
      <vig-accordion-item value="archive" heading="Archive (read-only)" [disabled]="true">
        Archived records cannot be opened.
      </vig-accordion-item>
      <vig-accordion-item value="notes" heading="Notes">No notes yet.</vig-accordion-item>
    </vig-accordion>
  `,
})
export class AccordionCustomHeaderDemo {}
```

## Styling hooks

Internal elements carry a stable `data-part` and the component's `data-scope`; both are public API, so removing one is a breaking change. Select a part by both, e.g. `[data-scope="accordion"][data-part="heading"] { … }`: the pair stays within this component when kit components nest, and also reaches parts in overlay panels, which render outside the host. State is exposed through `aria-*` and, where listed, a `data-state`; literal values are checked by the API gate, values computed from the component's state follow its documented types. Other `data-*` attributes listed below (such as `data-opening` while an overlay animates) are API too. See [Customizing](https://vigdx.com/docs/theming.md#customizing).

Scope: `[data-scope="accordion"]`

| Part | Element | data-state |
| --- | --- | --- |
| `heading` | The `h2`–`h6` wrapping the trigger. |   |
| `trigger` | The header button (`aria-expanded`). | `open, closed` |
| `label` | The header text or template. |   |
| `indicator` | The chevron. |   |
| `panel` | The collapsible panel. | `open, closed` |
| `body` | The padded panel content. |   |

## Accessibility

- Follows the WAI-ARIA APG accordion pattern: each header is a `<button>` inside a native `<h2>`–`<h6>` heading whose level `headingLevel` sets. Pick the level that fits the surrounding headings.
- Header buttons carry `aria-expanded` and `aria-controls`. Panels are not wrapped in per-item `region` landmarks, which would only add navigation noise; screen-reader users move between items by heading.
- Enter and Space toggle an item, and Tab moves between headers like any other buttons. Arrow keys, Home and End are deliberately not intercepted: they scroll the page, which long panels need.
- Closed panels stay in the DOM with `hidden="until-found"`: they are skipped by screen readers and the tab order until opened and form fields inside keep their state, but the browser's find-in-page and text-fragment links can reveal their text, which opens the item. Where `until-found` is not supported they are simply hidden; panels of disabled items are never searchable.
- A custom `vigAccordionHeader` template sits inside the button: keep it to non-interactive content (text, icons, badges).

## API

### Accordion

Selector: `vig-accordion` · Kind: component

Vertically stacked disclosure sections: each header is a button inside a heading (so screen-reader users can move between items by heading), and Tab moves between headers like any other buttons — arrow keys are left to scrolling. Closed panels stay in the DOM as `hidden="until-found"`, so find-in-page and text-fragment links can reveal them, which opens the item; browsers without `until-found` treat them as plain `hidden`.

**Example :**

```html
<vig-accordion [(value)]="open" [multiple]="true">
  <vig-accordion-item value="season" heading="Season">…panel…</vig-accordion-item>
</vig-accordion>
```

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `headingLevel` | `AccordionHeadingLevel` | `3` | Level of the `h2`–`h6` heading wrapping every header button. |
| `multiple` | `boolean` | `false` | Allow several items open at once; otherwise opening one closes the rest. |
| `value` (model) | `readonly string[]` | `[]` | Values of the open items; two-way bindable. |

#### Outputs

| Name | Payload | Description |
| --- | --- | --- |
| `valueChange` | `readonly string[]` | Values of the open items; two-way bindable. |

#### Methods

| Signature | Description |
| --- | --- |
| `expand(value: string): void` | Opens an item (no-op when already open). |
| `isOpen(value: string): boolean` | — |
| `toggle(value: string): void` | — |

### AccordionItem

Selector: `vig-accordion-item` · Kind: component

One accordion section: `value` identifies it, `heading` (or a `vigAccordionHeader` template) names it, and the projected content is the panel.

#### Inputs

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `disabled` | `boolean` | `false` | — |
| `header` | `string` | `''` | **Deprecated**: Use `heading`; still honored when `heading` is unset. |
| `heading` | `string \| undefined` | `undefined` | Header text; use a `vigAccordionHeader` template for richer content. |
| `value` (required) | `string` | — | — |

### Templates and directives

| Selector | Export | Description |
| --- | --- | --- |
| `[vigAccordionHeader]` | `AccordionHeader` | Marks an ng-template as an accordion item's custom header content. |
