Collapsible
One trigger that shows and hides one region: "show more" lists, advanced settings, details under a summary. The trigger and the content can sit anywhere inside the root.
4 repositories
Installation
Add Collapsible to your project with the CK-UI CLI. It copies the source into your project, so the code is yours to change.
- Adds
Collapsible.tsxto your CK-UI folder (src/components/ck-ui by default)- Installs
@radix-ui/react-slotif your project does not have them yet
First time with CK-UI in this project? Run npx ck-ui-cli@latest init once before adding components. The installation guide explains what it sets up.
Manual installationInstall the packages and copy the files yourself.
Install the following dependencies
Run these commands in your terminal to install the required packages
Create or update your Collapsible component
Copy this code to create your Collapsible component
Test Your Component
- 1. Add the component to a test page
- 2. Check all props and variants work as expected
- 3. Verify responsive behavior
- 4. Test with different content/data
- 5. Ensure accessibility features work correctly
Controlled
Pass open and onOpenChange to own the state; use defaultOpen otherwise.
State lives in the parent: closed.
Disabled
disabled on the root disables the trigger.
Accordion vs Collapsible
- Collapsible: one independent region, often a "show more" or an optional settings block. Free layout: the trigger can be any element (via asChild) and does not need a heading.
- Accordion: a set of related sections with headings, where you may want only one open at a time (type="single") and arrow-key navigation between headers.
- If you are repeating several Collapsibles in a list with headings, use an Accordion instead.
Accessibility
- The trigger is a button with
aria-expandedandaria-controls. Enter and Space toggle it. - The content stays in the DOM with the
hiddenattribute while closed, so aria-controls always points at an element and closed content is not focusable or read. - With asChild, the child must be a focusable, operable element such as a button.
- The opening animation (fade and short slide) is removed under reduced motion.
- Built with React and Radix Slot only. The native
<details>element was considered, but it cannot place the trigger outside its container or render it through asChild.
API Reference
Import with import * as Collapsible from "@/components/ui/Collapsible".
Collapsible.Root
Holds the open state. Renders a div with data-state.
| Prop | Type | Default | Description |
|---|---|---|---|
open | "boolean" | "undefined" | Controlled open state. |
defaultOpen | "boolean" | "false" | Initial state when uncontrolled. |
onOpenChange | "(open: boolean) => void" | "undefined" | Called when the trigger toggles. |
disabled | "boolean" | "false" | Disables the trigger. |
asChild | "boolean" | "false" | Render the single child element instead, merging this part's props and behaviour onto it. |
className | "string" | "undefined" | Additional classes, merged last. |
Collapsible.Trigger
A button that toggles the content. Exposes data-state="open" | "closed" for styling.
| Prop | Type | Default | Description |
|---|---|---|---|
asChild | "boolean" | "false" | Render your own button (e.g. Button.Root). |
className | "string" | "undefined" | Additional classes, merged last. |
Collapsible.Content
The region. Hidden while closed.
| Prop | Type | Default | Description |
|---|---|---|---|
forceMount | "boolean" | "false" | Never set hidden; style data-state yourself (e.g. for a height animation). |
id | "string" | "generated" | Content id; the trigger's aria-controls follows it. |
className | "string" | "undefined" | Additional classes, merged last. |
Used in these blocks
Block families with at least one block built on this component. Each page previews its blocks and lists the components and packages they use.