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>.
- Size
- 1.2 kB
Example
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>
);
}
Anatomy
<Collapsible.Root>
<Collapsible.Trigger />
<Collapsible.Panel />
</Collapsible.Root>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. |