suiss/ui · Component
Collapsible
A section that people can expand and collapse to show or hide its content.
Collapsible is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Collapsible wraps the collapsible primitive of the chosen base. The Collapsible root holds the open state, a CollapsibleTrigger toggles it, and CollapsibleContent holds the content that is shown or hidden. The parts are unstyled, so you control layout and appearance with className, or render the trigger as another component such as Button.
Use a collapsible for a single disclosure region. For a set of related, stacked sections, use an accordion instead.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Composition
Use the following composition to build a Collapsible:
Examples
By default
Set defaultOpen to start the collapsible expanded.
Set defaultExpanded to start the collapsible expanded.
Disabled
Set disabled on the root so the trigger does not toggle the content.
Set isDisabled on the root so the trigger does not toggle the content.
Accessibility
- The trigger renders as a button and sets
aria-expandedto reflect the open state. - The trigger toggles on click, and with Enter or Space when focused.
- Give an icon-only trigger an accessible name with
aria-label.
API Reference
Collapsible
The root that holds the open state and groups the trigger and content.
| Prop | Type | Default |
|---|---|---|
open | boolean | — |
defaultOpen | boolean | false |
onOpenChange | (open: boolean) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
isExpanded | boolean | — |
defaultExpanded | boolean | false |
onExpandedChange | (isExpanded: boolean) => void | — |
isDisabled | boolean | false |
Also accepts the props of the primitive part it wraps.
The root is uncontrolled by default; set defaultOpen to start expanded, or control it with open and onOpenChange.
The root is uncontrolled by default; set defaultExpanded to start expanded, or control it with isExpanded and onExpandedChange.
CollapsibleTrigger
The button that toggles the content open and closed.
Also accepts the props of the primitive part it wraps.
Render the trigger as another component, such as Button, with the render prop.
Set asChild to render another component, such as Button, as the trigger.
Any Button with slot="trigger" inside Collapsible toggles it.
CollapsibleContent
The panel that is shown when the collapsible is open.
Also accepts the props of the primitive part it wraps.
While collapsed, the content is removed from the page.
While collapsed, the content's children are not rendered.
No styles are applied by default. Each part sets a data-slot, such as collapsible-trigger, and the primitive sets state attributes you can use for styling and animation.
Web preview
Collapsible is an unstyled root with a CollapsibleTrigger and a CollapsibleContent panel. It handles the open state and accessibility, and leaves the look to you.
Use it for a single show-more section. For a list of sections where opening one can close another, use Accordion.
Installation
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/collapsible.tsx:
- Update the import paths to match your project setup.
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/collapsible.tsx:
- Update the import paths to match your project setup.
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/collapsible.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Default
Web preview
Disabled
A disabled collapsible whose trigger cannot toggle it.
Web preview
Accessibility
- The trigger has the
buttonrole withexpandedanddisabledstate. - Closed content is hidden from VoiceOver and TalkBack.
API Reference
Collapsible
Root that holds the open state.
| Prop | Type | Default |
|---|---|---|
open | boolean | - |
defaultOpen | boolean | false |
onOpenChange | (open: boolean, details) => void | - |
disabled | boolean | false |
render | React.ReactElement | ((props, state) => React.ReactElement) | - |
Also accepts the props of View.
Use open with onOpenChange for controlled state, or defaultOpen for uncontrolled. render replaces the default View.
CollapsibleTrigger
Pressable that opens and closes the content.
| Prop | Type | Default |
|---|---|---|
render | React.ReactElement | ((props, state) => React.ReactElement) | - |
Also accepts the props of Pressable.
Put any content in the trigger, for example a Text and a caret Icon. Use render to render it as another element, such as a Button, in place of the default Pressable.
CollapsibleContent
The content shown while open.
| Prop | Type | Default |
|---|---|---|
keepMounted | boolean | false |
render | React.ReactElement | ((props, state) => React.ReactElement) | - |
Also accepts the props of View.
Set keepMounted to keep the content mounted while closed. render replaces the default View.
Collapsible
Root that holds the open state.
| Parameter | Type | Default |
|---|---|---|
isExpanded | Binding<Bool> | - |
spacing | CGFloat | 0 |
content | () -> Content | - |
Overloads: init(isExpanded:spacing:content:), init(spacing:content:).
CollapsibleTrigger
The button that toggles the content open and closed.
| Parameter | Type | Default |
|---|---|---|
label | () -> Label | - |
_Â title | LocalizedStringKey | - |
Overloads: init(label:), init(_:).
CollapsibleContent
The content shown while open.
| Parameter | Type | Default |
|---|---|---|
content | () -> Content | - |
Collapsible
Root that holds the open state.
| Parameter | Type | Default |
|---|---|---|
expanded | Boolean | - |
onExpandedChange | (Boolean) -> Unit | - |
modifier | Modifier | Modifier |
enabled | Boolean | true |
content | @Composable ColumnScope.() -> Unit | - |
CollapsibleTrigger
The button that toggles the content open and closed.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable RowScope.() -> Unit | - |
CollapsibleContent
The content shown while open.
| Parameter | Type | Default |
|---|---|---|
modifier | Modifier | Modifier |
content | @Composable ColumnScope.() -> Unit | - |