Collapsible
An interactive component which expands/collapses a panel.
Collapsible is a single block that expands and collapses (an accordion, but standalone, with no siblings). Useful for a "show more" toggle or an optional advanced panel.
For several stacked sections, use Accordion instead.
Installation
$ bext ui add collapsible
Copy and paste the following into src/components/ui/collapsible.tsx.
This component is interactive. Make sure the shared island is loaded (see Islands). It reacts to data-ui="collapsible" hooks.
Usage
Anatomy
<Collapsible open?> ├── <CollapsibleTrigger>Show more</CollapsibleTrigger> └── <CollapsibleContent>Hidden content...</CollapsibleContent>
API Reference
Collapsible
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | false | Renders the open state in the HTML: the content is visible and the trigger carries data-state="open" and aria-expanded="true". |
| children | ReactNode | — | A CollapsibleTrigger and a CollapsibleContent. |
| className | string | — | Extra classes on the wrapper <div>, which has no classes of its own. |
CollapsibleTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | — | Content of the <button> that opens and closes the panel. |
| open | boolean | false | Open state of the trigger alone. Not needed when open is set on Collapsible. |
| id | string | — | id of the button. |
| controls | string | — | id of the content, written to aria-controls. Set by the island when omitted. |
CollapsibleContent
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | false | Renders the panel visible on load. Not needed when open is set on Collapsible. |
| id | string | — | id of the panel. Generated by the island when omitted. |
| children | ReactNode | — | The collapsible content. |
Data Attributes
Attributes you can target in custom CSS to override the style for each state.
| Attribute | On | Values | Description |
|---|---|---|---|
| data-ui="accordion-item" | Collapsible root | — | Collapsible reuses the accordion hooks of the island. |
| data-ui="accordion-trigger" | CollapsibleTrigger | — | Clicking toggles the panel. |
| data-state | Collapsible root, CollapsibleTrigger | "open" | "closed" | Current state, rendered from open and updated by the island. |
| aria-expanded | CollapsibleTrigger | "true" | "false" | Same state, for assistive technologies. |
| data-ui="accordion-content" | CollapsibleContent | hidden attribute present or absent | The island shows the panel by removing hidden. |
| data-slot | every sub-component | "collapsible" | "collapsible-trigger" | "collapsible-content" | Stable selector for custom styling. |
Keyboard
| Key | Action |
|---|---|
| Enter / Space | Opens or closes. |
Accessibility
The trigger is a native <button type="button"> with aria-expanded, rendered from open and kept in sync by the island, and aria-controls pointing at the content. The island generates the id of the content when you do not pass one. The content carries the hidden attribute when closed, which keeps it out of the accessibility tree.
Examples
Open on load
Edit this component live in the bext playground. The PRISM + signals version is the bext-native idiom: fine-grained reactivity, no virtual DOM.
On This Page