# Collapsible

> Content that a button shows and hides, on the browser's details element; for advanced options, extra details or a Show more.

Source: https://shadleaf.dev/1.2.0/components/collapsible/

A trigger and a piece of content that the trigger shows and hides: one section, without an accordion's styling.

**Example: Collapsible.**

```html
<sl:collapsible>
  <sl:collapsible-trigger>@peduarte starred 3 repositories <sl:icon name="chevrons-up-down"/></sl:collapsible-trigger>
  <sl:collapsible-content>
    <p>@radix-ui/primitives</p>
    <p>@radix-ui/colors</p>
    <p>@stitches/react</p>
  </sl:collapsible-content>
</sl:collapsible>
```

## When to use

- Advanced options below a form.
- Extra details, or the rest of a list behind a Show more.

Use something else for:

- Several sections of which the user opens one or more: an [accordion](https://shadleaf.dev/1.2.0/components/accordion.md).
- Content that floats over the page next to its button: a [popover](https://shadleaf.dev/1.2.0/components/popover.md).

## Built from parts

A collapsible is `<sl:collapsible>` with an `<sl:collapsible-trigger>` and an `<sl:collapsible-content>`:

```html
<sl:collapsible>
  <sl:collapsible-trigger>@peduarte starred 3 repositories <sl:icon name="chevrons-up-down"/></sl:collapsible-trigger>
  <sl:collapsible-content>
    <p>@radix-ui/primitives</p>
    <p>@radix-ui/colors</p>
  </sl:collapsible-content>
</sl:collapsible>
```

It is a [`<details>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/details) element with the
trigger as its `<summary>`, so it needs no JavaScript: the browser opens and closes it, from the keyboard too, and
tells a screen reader whether it is expanded. Two consequences:

- **The trigger comes first**, and the content after it, both inside `<sl:collapsible>`: a `summary` must be the first
  child.
- **Everything else goes in the trigger or the content.** Text next to the trigger, such as a heading, can be part of
  the trigger.

`open` renders it open. As with the [accordion](https://shadleaf.dev/1.2.0/components/accordion.md), find-in-page and a link to an element inside open a
closed collapsible on a match (Chrome and Edge 97, Firefox 148, Safari 26.2). It does not animate.

## Drawn as a button

The trigger is plain text by default. `variant` or `size` draws it as a [button](https://shadleaf.dev/1.2.0/components/button.md), with the button's variants
and sizes. An icon-only trigger (`size="icon"`) needs an `aria-label`.

**Example: Drawn as a button.** variant and size draw the trigger as a button.

```html
<sl:collapsible open>
  <sl:collapsible-trigger variant="outline" size="sm"><sl:icon name="sliders-horizontal"/> Filters</sl:collapsible-trigger>
  <sl:collapsible-content>
    <sl:field orientation="horizontal" for="collapsible-in-stock">
      <sl:checkbox checked/>
      <sl:field-label>In stock only</sl:field-label>
    </sl:field>
  </sl:collapsible-content>
</sl:collapsible>
```

## Panel

`variant="panel"` draws shadcn/ui's
[basic example](https://ui.shadcn.com/docs/components/base/collapsible#basic) without utility classes: the trigger is a
full-width ghost button with a chevron at its end that turns over while the collapsible is open, and the open
collapsible a muted panel with its content padded inside. Give the trigger a `variant` or `size` of its own to draw it
as another button; the chevron stays.

**Example: Panel.** variant="panel" draws shadcn/ui's basic example.

```html
<sl:collapsible variant="panel" open>
  <sl:collapsible-trigger>Product details</sl:collapsible-trigger>
  <sl:collapsible-content>
    <p>This panel can be expanded or collapsed to reveal additional content.</p>
    <sl:button size="xs">Learn More</sl:button>
  </sl:collapsible-content>
</sl:collapsible>
```

## Disabled

`disabled` keeps it closed (or open, with `open`): the trigger fades, leaves the tab order and ignores the pointer, and
a closed disabled collapsible renders no content, so find-in-page cannot open it.

**Example: Disabled.**

```html
<sl:collapsible disabled>
  <sl:collapsible-trigger variant="ghost" size="sm">Order history</sl:collapsible-trigger>
  <sl:collapsible-content>Your past orders.</sl:collapsible-content>
</sl:collapsible>
```

## Left out

- **A trigger outside the collapsible**, or after the content: a `details` element has its `summary` first.
- **Styling the trigger's icon by state** with utility classes: the bundle has none. The [panel](#panel) turns its
  chevron; for another icon, `.collapsible[open] > .collapsible-trigger svg { rotate: 90deg; }` in your own CSS does
  it.

## Attributes

### sl:collapsible

Attributes of `<sl:collapsible>`: Content that a button shows and hides. Built on the browser's `details` element, so it needs no JavaScript. Holds a `sl:collapsible-trigger` (first) and a `sl:collapsible-content`.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `variant` | `default`, `panel` | `default` | `panel` draws shadcn/ui's basic example: the trigger a full-width ghost button with a chevron at its end that turns over while open, and the open collapsible a muted panel with its content padded inside. |
| `open` | *boolean* | — | Render it open. |
| `disabled` | *boolean* | — | It cannot be opened or closed: its trigger is out of the tab order and ignores the pointer, and closed it renders no content, so find-in-page cannot open it. |

Every prop can also be set from an expression: `th:variant="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended.

### sl:collapsible-trigger

Attributes of `<sl:collapsible-trigger>`: What the user clicks to open and close a `sl:collapsible`: its `details` element's `summary`, so it must come first. Plain text by default; `variant` or `size` draws it as a `sl:button`. In a `panel` collapsible it is a ghost button by default and ends in a chevron.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `variant` | `default`, `outline`, `ghost`, `destructive`, `secondary`, `link` | — | Draw the trigger as a button of this variant, as on `sl:button`. |
| `size` | `xs`, `sm`, `default`, `lg`, `icon`, `icon-xs`, `icon-sm`, `icon-lg` | — | Draw the trigger as a button of this size, as on `sl:button`. The `icon` sizes need an `aria-label`. |

Every prop can also be set from an expression: `th:variant="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended. An `aria-label` or `aria-labelledby` is required when `size` is `icon` or `icon-xs` or `icon-sm` or `icon-lg`; rendering fails without one.

### sl:collapsible-content

Attributes of `<sl:collapsible-content>`: The content of a `sl:collapsible`, shown while it is open. Find-in-page searches it while it is closed, and opens it on a match.

`sl:collapsible-content` declares no props; every attribute passes through.

## Styling hooks

The classes and attributes your own CSS can select to restyle the component, as explained in
[your own CSS rules](https://shadleaf.dev/1.2.0/guides/adjustability.md#5-your-own-css-rules).

### sl:collapsible

Styling hooks of `<sl:collapsible>`.

**Parts**

| Class | Element |
| --- | --- |
| `.collapsible` | `<details>` |

**Props**

| Prop | Selector | Values |
| --- | --- | --- |
| `disabled` | `.collapsible[data-disabled="…"]`, `.collapsible:not([data-disabled])` | `true`; `false` (default) |
| `variant` | `.collapsible[data-variant="…"]`, `.collapsible:not([data-variant])` | `panel`; `default` (default) |

### sl:collapsible-trigger

Styling hooks of `<sl:collapsible-trigger>`.

**Parts**

| Class | Element |
| --- | --- |
| `.collapsible-trigger` | `<summary>` |
| `.collapsible-trigger-icon` | `<svg>` |

### sl:collapsible-content

Styling hooks of `<sl:collapsible-content>`.

**Parts**

| Class | Element |
| --- | --- |
| `.collapsible-content` | `<div>` |
