Collapsible
Shows or hides one block of content from a single trigger.
3 deliveries this week
Installation
pnpm dlx shadcn@latest add @silkware/collapsibleTo copy the component by hand, install its dependency:
pnpm add react-aria-componentsThen copy the source into components/ui/collapsible.tsx:
"use client"
import { AnimatePresence } from "motion/react"import { DisclosurePanel as CollapsibleContentPrimitive, Disclosure as CollapsiblePrimitive, Button as CollapsibleTriggerPrimitive, type ButtonProps, type DisclosurePanelProps, type DisclosureProps,} from "@animate-ui/react/primitives/react-aria"
function Collapsible({ ...props }: DisclosureProps) { return ( <AnimatePresence initial={false}> <CollapsiblePrimitive data-slot="collapsible" {...props} /> </AnimatePresence> )}
function CollapsibleTrigger({ ...props }: ButtonProps) { return ( <CollapsibleTriggerPrimitive slot="trigger" data-slot="collapsible-trigger" {...props} /> )}
function CollapsibleContent({ ...props }: DisclosurePanelProps) { return ( <CollapsibleContentPrimitive data-slot="collapsible-content" {...props} /> )}
export { Collapsible, CollapsibleTrigger, CollapsibleContent }Usage
import { Collapsible, CollapsibleContent, CollapsibleTrigger,} from "@/components/ui/collapsible"<Collapsible> <CollapsibleTrigger>Delivery notes</CollapsibleTrigger> <CollapsibleContent>Leave pallets at gate 2.</CollapsibleContent></Collapsible>CollapsibleTrigger has no styles of its own. Style it with className, or use a Button with slot="trigger" to get the button styles and keep the connection to the panel.
Examples
Default
Collapsed at first. The trigger here is a ghost icon Button with slot="trigger", and the first item stays visible.
3 deliveries this week
Expanded at first
defaultExpanded opens the block on first render. The user can still close it.
Leave pallets at gate 2. Marco signs for anything delivered after four in the afternoon.
Controlled
Use isExpanded and onExpandedChange to read or set the state, for example to change the label of the trigger.
Accessibility
| Key | Action |
|---|---|
| Space | Expands or collapses the content. |
| Enter | Expands or collapses the content. |
| Tab | Moves focus to the next focusable element. |
React Aria provides:
- A trigger button with
aria-expandedandaria-controlspointing to the content. - Content hidden from the accessibility tree and the tab order while collapsed.
- Keyboard and press handling on the trigger.
You provide:
- A trigger label that says what it reveals. An icon-only trigger needs a text label, as in the first example.
- Content that is not required to finish the task, because some users never open it.
Guidelines
When to use
- To hide secondary detail behind one control: extra filters, delivery notes, a long list past the first few rows.
- When the page should load short and the user can ask for more.
When not to use
- For several sections that open and close together. Use an Accordion.
- For views that replace each other. Use Tabs.
- For a menu of actions. Use a Dropdown Menu.
- For information the user must see. Put it in the page.
Content
- Name what is hidden: "Show delivery notes", "Show more filters".
- When the label changes with the state, change the verb and keep the noun: "Hide filters".
- Keep what stays visible useful on its own.
Order PO-2041 is waiting for approval.
Hide detail the user can do without, and name it on the trigger.
Don’t hide a deadline or a warning behind a trigger.
API reference
Collapsible
The root. Renders a div through the React Aria Disclosure. It accepts every Disclosure prop.
| Prop | Type | Default | Description |
|---|---|---|---|
isExpanded? | boolean | None | Whether the content is visible, for a controlled collapsible. |
defaultExpanded? | boolean | false | Whether the content is visible at first. |
onExpandedChange? | (isExpanded: boolean) => void | None | Called when the content is shown or hidden. |
isDisabled? | boolean | false | Ignores presses on the trigger. |
CollapsibleTrigger
Renders a React Aria Button with slot="trigger", which connects it to the nearest Collapsible. It accepts every Button prop and has no default styles.
| Prop | Type | Default | Description |
|---|---|---|---|
children? | ReactNode | None | The label of the trigger. |
className? | string | None | The only styling: there are no base classes. |
CollapsibleContent
The hidden block. Renders a React Aria DisclosurePanel. It accepts every DisclosurePanel prop.
| Prop | Type | Default | Description |
|---|---|---|---|
children | ReactNode | None | The content shown when the collapsible is open. |
className? | string | None | Classes for the panel. |