# Collapsible

A button that shows and hides a section of content, such as the answer under a question or the details of an order: the WAI-ARIA disclosure pattern. The Trigger is a native button with aria-expanded, and aria-controls naming the Panel. A closed Panel has the hidden attribute, so its content stays in the page, keeping its state, but is not shown or read out; to leave closed content out of the page, wrap it in <Show>.

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

> To add Collapsible 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
<Collapsible.Root>
  <Collapsible.Trigger />
  <Collapsible.Panel />
</Collapsible.Root>
```

## Example

### Tailwind CSS

`CollapsibleExample.tsx`:

```tsx
import { Collapsible } from "menhir/collapsible";

export default function CollapsibleExample() {
  return (
    <Collapsible.Root class="w-64 text-sm">
      <Collapsible.Trigger class="flex cursor-pointer items-center gap-2 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">
        Shipping
      </Collapsible.Trigger>
      <Collapsible.Panel class="mt-2 text-stone-600 dark:text-stone-400">
        Orders ship within two working days, and arrive three to five days
        later.
      </Collapsible.Panel>
    </Collapsible.Root>
  );
}
```

### CSS

`CollapsibleExample.tsx`:

```tsx
import { Collapsible } from "menhir/collapsible";

import "./collapsible.css";

export default function CollapsibleExample() {
  return (
    <Collapsible.Root class="collapsible">
      <Collapsible.Trigger class="collapsible-trigger">
        Shipping
      </Collapsible.Trigger>
      <Collapsible.Panel class="collapsible-panel">
        Orders ship within two working days, and arrive three to five days
        later.
      </Collapsible.Panel>
    </Collapsible.Root>
  );
}
```

`collapsible.css`:

```css
/* The least CSS that makes the Collapsible visible: a trigger with an
   arrow that turns down while open, selected by data-open. The 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). */
.collapsible {
  width: 16rem;
  font:
    0.875rem system-ui,
    sans-serif;
}

.collapsible-trigger {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

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

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

.collapsible-panel {
  margin-top: 0.5rem;
  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 the open state. Every part has data-open or data-closed, and data-disabled.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | — | Whether the Panel is shown, when controlled. Pair it with onOpenChange. |
| `defaultOpen` | `boolean` | `false` | Whether the Panel is shown at first, when uncontrolled. |
| `onOpenChange` | `(open: boolean) => void` | — | Called with the new value when the user shows or hides the Panel. A controlled Panel changes only if open does. |
| `disabled` | `boolean` | `false` | Whether the Trigger ignores the user. It cannot be focused either; the Panel stays as it is. |
| `render` | `(props) => JSX.Element` | — | Renders the root with your own component. Read once. |

### Trigger

<button type="button" aria-expanded aria-controls>. Name it with its content. 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>, 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: for a link target, give the id to an element inside it.

| 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 Panel. |
| `Enter` | Shows or hides the Panel. |
| `Tab` | Moves focus to or away from the Trigger. |
