# Accordion

A stack of headings, each a button that shows and hides its own section, such as the questions of an FAQ: the WAI-ARIA accordion pattern. Each Trigger is a native button with aria-expanded, inside a heading, and each Panel is a region named by its Trigger. By default one section is open at a time; with multiple, each opens on its own. A closed Panel has the hidden attribute, so its content stays in the page, keeping its state, but is not shown or read out.

- Entry point: `menhir/accordion`
- Size: 1.5 kB, minified and gzipped, Solid left out

> To add Accordion to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<Accordion.Root>
  <Accordion.Item>
    <Accordion.Header>
      <Accordion.Trigger />
    </Accordion.Header>
    <Accordion.Panel />
  </Accordion.Item>
</Accordion.Root>
```

## Example

### Tailwind CSS

`AccordionExample.tsx`:

```tsx
import { Accordion } from "menhir/accordion";
import { For } from "solid-js";

const questions = [
  {
    value: "shipping",
    question: "When will my order ship?",
    answer: "Within two working days, and it arrives three to five days later.",
  },
  {
    value: "returns",
    question: "Can I return it?",
    answer: "Yes, for free, within 30 days of delivery.",
  },
  {
    value: "warranty",
    question: "Is there a warranty?",
    answer: "Two years, covering any fault that is not wear and tear.",
  },
];

export default function AccordionExample() {
  return (
    <Accordion.Root class="w-80 text-sm">
      <For each={questions}>
        {(item) => (
          <Accordion.Item
            value={item.value}
            class="border-b border-stone-300 dark:border-stone-700"
          >
            <Accordion.Header class="m-0 text-sm">
              <Accordion.Trigger class="flex w-full cursor-pointer items-center gap-2 py-3 text-start font-semibold before:transition-[rotate] before:content-['▸'] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-open:before:rotate-90">
                {item.question}
              </Accordion.Trigger>
            </Accordion.Header>
            <Accordion.Panel class="pb-3 text-stone-600 dark:text-stone-400">
              {item.answer}
            </Accordion.Panel>
          </Accordion.Item>
        )}
      </For>
    </Accordion.Root>
  );
}
```

### CSS

`AccordionExample.tsx`:

```tsx
import { Accordion } from "menhir/accordion";
import { For } from "solid-js";

import "./accordion.css";

const questions = [
  {
    value: "shipping",
    question: "When will my order ship?",
    answer: "Within two working days, and it arrives three to five days later.",
  },
  {
    value: "returns",
    question: "Can I return it?",
    answer: "Yes, for free, within 30 days of delivery.",
  },
  {
    value: "warranty",
    question: "Is there a warranty?",
    answer: "Two years, covering any fault that is not wear and tear.",
  },
];

export default function AccordionExample() {
  return (
    <Accordion.Root class="accordion">
      <For each={questions}>
        {(item) => (
          <Accordion.Item value={item.value} class="accordion-item">
            <Accordion.Header class="accordion-header">
              <Accordion.Trigger class="accordion-trigger">
                {item.question}
              </Accordion.Trigger>
            </Accordion.Header>
            <Accordion.Panel class="accordion-panel">
              {item.answer}
            </Accordion.Panel>
          </Accordion.Item>
        )}
      </For>
    </Accordion.Root>
  );
}
```

`accordion.css`:

```css
/* The least CSS that makes the Accordion visible: sections divided by a
   line, and triggers with an arrow that turns down while open, selected by
   data-open. A closed Panel is hidden by its hidden attribute. Everything
   else is the browser's. Colours are Tailwind's stone palette; light-dark()
   picks the second one in dark mode (color-scheme: dark). */
.accordion {
  width: 20rem;
  font:
    0.875rem system-ui,
    sans-serif;
}

.accordion-item {
  border-bottom: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(37.4% 0.01 67.558) /* stone-700 */
    );
}

.accordion-header {
  margin: 0;
  font: inherit;
}

.accordion-trigger {
  display: flex;
  width: 100%;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  text-align: start;
  cursor: pointer;
}

.accordion-trigger::before {
  content: "▸";
  transition: rotate 150ms;
}

.accordion-trigger[data-open]::before {
  rotate: 90deg;
}

.accordion-panel {
  padding-bottom: 0.75rem;
  color: light-dark(
    oklch(44.4% 0.011 73.639) /* stone-600 */,
    oklch(70.9% 0.01 56.259) /* stone-400 */
  );
}
```

## API reference

### Root

<div> that holds which sections are open. It has data-disabled when disabled.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string[]` | — | The values of the open Items, when controlled. Pair it with onValueChange. |
| `defaultValue` | `string[]` | `[]` | The values of the Items open at first, when uncontrolled. |
| `onValueChange` | `(value: string[]) => void` | — | Called with the new values when the user opens or closes a section. A controlled accordion changes only if value does. |
| `multiple` | `boolean` | `false` | Whether several sections can be open at once. Otherwise opening one closes the one open before. |
| `disabled` | `boolean` | `false` | Whether every Trigger ignores the user. They cannot be focused either; the Panels stay as they are. |
| `render` | `(props) => JSX.Element` | — | Renders the root with your own component. Read once. |

### Item

<div> around one section. Each part of an Item has data-open or data-closed, and data-disabled.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | Identifies the section in the Root's value. Without one, the Item uses a generated value, enough for an uncontrolled accordion that starts closed. |
| `disabled` | `boolean` | `false` | Whether this section's Trigger ignores the user. It cannot be focused either. |
| `render` | `(props) => JSX.Element` | — | Renders the item with your own component. Read once. |

### Header

<h3> that holds only the Trigger. For the heading level that fits your page, render another heading: render={(props) => <h2 {...props} />}.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the header with your own heading. Read once. |

### Trigger

<button type="button" aria-expanded aria-controls>. Name it with its content. Its id is generated unless you give one. Its render function must render a <button>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the trigger with your own button component. Read once. |

### Panel

<div role="region"> named by its Trigger, hidden while closed. Its id is generated, so that the Trigger can name it in the server's HTML too, and it takes no id of its own. Where many sections can be open at once, about six or more, leave the regions out: render={(props) => <div {...props} role={undefined} aria-labelledby={undefined} />}.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the panel with your own component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Space` | Shows or hides the focused Trigger's section. |
| `Enter` | Shows or hides the focused Trigger's section. |
| `Tab` | Moves focus to the next Trigger, or the next focusable element. |
| `Shift + Tab` | Moves focus to the previous Trigger, or the previous focusable element. |
