# Accordion

> Shows and hides sections of content one at a time.

An accordion stacks sections under their titles and opens them one at a time, so a long page stays short.

## When to use

- Use it for questions and answers, settings groups, and details that only some people need.
- Use [Tabs](https://design.sosialoka.tech/components/tabs/) to switch between views of the same thing, and a page when every section matters.
- Don't hide required information in a closed section: people may never open it.
- On phones, an accordion also turns table rows into sections; see the [Responsive table](https://design.sosialoka.tech/patterns/responsive-table/) pattern.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/accordion/_

## Anatomy

1. Open section header
2. Content
3. Closed section
4. Chevron

Items separated by a `border-b` on `border`. Each item has a trigger (`py-4 text-sm font-medium`, with a chevron that turns 180° when open) and its content (`pb-4 text-sm`).

## Variants

| Mode | Use it for |
| --- | --- |
| One open at a time (default) | Questions and answers, where one answer is enough to read. |
| Several open (`multiple`) | Settings groups that people compare. |

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `multiple` | `type="single"` / `"multiple"` | `ShadAccordion` / `ShadAccordion.multiple` | mode | one open | How many sections can be open. |
| always closable | `collapsible` | always closable | boolean | `false` in Vue | Lets the open section close again. |
| `value` / `defaultValue` | `v-model` / `default-value` | `initialValue` | value | none | The open section. |
| `AccordionItem value` | `AccordionItem value` | `ShadAccordionItem value` | value | none | Names a section. |

## Code

**React**
```tsx
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"

const questions = [
  { q: "Berapa lama clearance selesai?", a: "Biasanya 5–7 hari kerja setelah dokumenmu lengkap. Kamu dapat kabar di setiap langkah." },
  { q: "Lagu apa saja yang bisa kamu cover?", a: "Lagu yang ada di katalog kami. Cari judulnya di menu Katalog." },
  { q: "Kapan royalti dibayar?", a: "Setiap bulan, paling lambat tanggal 25, ke rekening yang terdaftar di akunmu." },
]

export default function AccordionDemo() {
  return (
    <Accordion className="w-full max-w-md">
      {questions.map(({ q, a }) => (
        <AccordionItem key={q} value={q}>
          <AccordionTrigger>{q}</AccordionTrigger>
          <AccordionContent>{a}</AccordionContent>
        </AccordionItem>
      ))}
    </Accordion>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"

const questions = [
  { q: "Berapa lama clearance selesai?", a: "Biasanya 5–7 hari kerja setelah dokumenmu lengkap. Kamu dapat kabar di setiap langkah." },
  { q: "Lagu apa saja yang bisa kamu cover?", a: "Lagu yang ada di katalog kami. Cari judulnya di menu Katalog." },
  { q: "Kapan royalti dibayar?", a: "Setiap bulan, paling lambat tanggal 25, ke rekening yang terdaftar di akunmu." },
]
</script>

<template>
  <Accordion type="single" collapsible class="w-full max-w-md">
    <AccordionItem v-for="item in questions" :key="item.q" :value="item.q">
      <AccordionTrigger>{{ item.q }}</AccordionTrigger>
      <AccordionContent>{{ item.a }}</AccordionContent>
    </AccordionItem>
  </Accordion>
</template>
```

**Flutter**
```dart
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';

const questions = [
  ('Berapa lama clearance selesai?', 'Biasanya 5–7 hari kerja setelah dokumenmu lengkap. Kamu dapat kabar di setiap langkah.'),
  ('Lagu apa saja yang bisa kamu cover?', 'Lagu yang ada di katalog kami. Cari judulnya di menu Katalog.'),
  ('Kapan royalti dibayar?', 'Setiap bulan, paling lambat tanggal 25, ke rekening yang terdaftar di akunmu.'),
];

class AccordionDemo extends StatelessWidget {
  const AccordionDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
      constraints: const BoxConstraints(maxWidth: 448),
      child: ShadAccordion<int>(
        children: [
          for (var i = 0; i < questions.length; i++)
            ShadAccordionItem(value: i, title: Text(questions[i].$1), child: Text(questions[i].$2)),
        ],
      ),
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/accordion.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/accordion.json
```

## Accessibility

- Each trigger is a button with `aria-expanded` and `aria-controls` pointing at its content.
- Enter and Space open and close a section; Tab moves between triggers.
- Keep the trigger text a full question or title, so it makes sense on its own.
