Menhir

Accordion

View as Markdown

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.

Size
1.5 kB

Example

Styling

AccordionExample.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>
  );
}

Anatomy

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

API reference

Root

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

PropTypeDefaultDescription
valuestring[]—The values of the open Items, when controlled. Pair it with onValueChange.
defaultValuestring[][]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.
multiplebooleanfalseWhether several sections can be open at once. Otherwise opening one closes the one open before.
disabledbooleanfalseWhether 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.

PropTypeDefaultDescription
valuestring—Identifies the section in the Root's value. Without one, the Item uses a generated value, enough for an uncontrolled accordion that starts closed.
disabledbooleanfalseWhether 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} />}.

PropTypeDefaultDescription
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>.

PropTypeDefaultDescription
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} />}.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the panel with your own component. Read once.

Keyboard

KeysAction
SpaceShows or hides the focused Trigger's section.
EnterShows or hides the focused Trigger's section.
TabMoves focus to the next Trigger, or the next focusable element.
Shift + TabMoves focus to the previous Trigger, or the previous focusable element.