# Accordion

Hand-ruled sections with a drawn chevron that turns over as they open.

Docs: https://ballpoint.st9wd.com/docs/accordion

```bash
npx shadcn@latest add @ballpoint/accordion
```

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

<Accordion>
  <AccordionItem value="a">
    <AccordionTrigger>Is it accessible?</AccordionTrigger>
    <AccordionContent>Yes.</AccordionContent>
  </AccordionItem>
</Accordion>
```

Props:

- `multiple`: `boolean` (default `false`). On Accordion: lets more than one item stay open.
- `defaultValue`: `string[]`. On Accordion: the items open to begin with.
- `AccordionTrigger header`: `Accordion.Header props`. Reaches the heading row, an h3 by default. Pass { render: <div /> } to keep it out of the page outline.
- `AccordionTrigger seed`: `string | number`. Pins the chevron's drawing.

Every other prop goes to Base UI's Accordion: https://base-ui.com/react/components/accordion

## Example

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

const faq = [
  ["Is it accessible?", "Yes. Every component is built on Base UI, checked with axe in both themes, and keyboard-tested. The drawing is decoration; the semantics are the primitive's."],
  ["Does it work without JavaScript?", "The page renders with every stroke already drawn. Scripts add the measuring and the drawing in."],
  ["Can I change the pen?", "Per component, for a section, or for the whole app: see the customizer."],
];

export default function AccordionDemo() {
  return (
    <Accordion defaultValue={["item-0"]} className="max-w-lg">
      {faq.map(([question, answer], i) => (
        <AccordionItem key={i} value={`item-${i}`}>
          <AccordionTrigger seed={`acc-${i}`}>{question}</AccordionTrigger>
          <AccordionContent>{answer}</AccordionContent>
        </AccordionItem>
      ))}
    </Accordion>
  );
}
```

## Several open

With multiple, items open independently.

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

const sections = [
  ["Shipping", "Orders leave the studio on Tuesdays and Fridays, wrapped in brown paper."],
  ["Returns", "Send anything back within thirty days; unused ink is always welcome."],
  ["Care", "Cap the pen, keep it out of the sun, and never lend it."],
];

// `multiple` lets several items stay open at once; by default opening one closes the others.
export default function AccordionMultiple() {
  return (
    <Accordion multiple defaultValue={["item-0", "item-2"]} className="max-w-lg">
      {sections.map(([title, text], i) => (
        <AccordionItem key={i} value={`item-${i}`}>
          <AccordionTrigger seed={`am-${i}`}>{title}</AccordionTrigger>
          <AccordionContent>{text}</AccordionContent>
        </AccordionItem>
      ))}
    </Accordion>
  );
}
```

## With icons

A glyph in front of each title.

```tsx
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { InkIcon, type IconName } from "@/components/ui/ink-icons";

const items: { icon: IconName; title: string; body: string }[] = [
  { icon: "pen", title: "Pens", body: "Four to start with, each with a night colour that clears 4.5:1 on every paper." },
  { icon: "notebook", title: "Papers", body: "Cream, white and legal pad, ruled, squared or dotted, with coffee rings if you like." },
  { icon: "moon", title: "Night", body: "Every drawing has a night side. The lamp comes on over the top of the page." },
];

export default function AccordionIcons() {
  return (
    <Accordion defaultValue={["Pens"]} className="max-w-lg">
      {items.map(({ icon, title, body }) => (
        <AccordionItem key={title} value={title}>
          <AccordionTrigger seed={`ai-${title}`}>
            <span className="flex items-center gap-3">
              <InkIcon name={icon} className="size-5 text-ink-2" />
              {title}
            </span>
          </AccordionTrigger>
          <AccordionContent>{body}</AccordionContent>
        </AccordionItem>
      ))}
    </Accordion>
  );
}
```

## Open them all

Own the open items to expand and collapse everything at once.

```tsx
"use client";

import { useState } from "react";
import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion";
import { Button } from "@/components/ui/button";

const steps = [
  ["Install", "Run init once, then add what you need."],
  ["Draw", "Every component draws itself in the first time it is seen."],
  ["Customize", "Pick a pen and a paper, and the whole page follows."],
];

export default function AccordionControlled() {
  const [open, setOpen] = useState<string[]>([]);
  return (
    <div className="flex w-full max-w-lg flex-col gap-4">
      <div className="flex gap-3">
        <Button variant="outline" size="sm" seed="ac-all" onClick={() => setOpen(steps.map(([title]) => title))}>
          Open all
        </Button>
        <Button variant="ghost" size="sm" seed="ac-none" onClick={() => setOpen([])}>
          Close all
        </Button>
      </div>
      <Accordion multiple value={open} onValueChange={(value) => setOpen(value as string[])}>
        {steps.map(([title, body]) => (
          <AccordionItem key={title} value={title}>
            <AccordionTrigger seed={`ac-${title}`}>{title}</AccordionTrigger>
            <AccordionContent>{body}</AccordionContent>
          </AccordionItem>
        ))}
      </Accordion>
    </div>
  );
}
```
