Collapsible
A block of content that one trigger shows or hides, with no styling of its own so you design the trigger.
Select the button to show the details.
When to use
- To hide secondary detail that most people do not need on every visit, such as the technical data of a process or a long log.
- To keep a page short and scannable, so the main content stays in view. This follows Structure you can scan in the manifesto.
- For one block with its own trigger, placed anywhere in a layout.
When not to use
- Several related sections that behave together. Use
Accordion, which manages which item is open. - Content people need every time. Show it. Hidden content is skipped.
- A short message or help text. Use a Tooltip or a Popover.
- A task that interrupts the flow. Use Dialog.
Anatomy
| Part | Role |
|---|---|
Collapsible |
Holds the open state. Controlled with open or uncontrolled with defaultOpen. |
CollapsibleTrigger |
The control that toggles. Use asChild with a Button. |
CollapsibleContent |
The block that shows and hides. It animates its height. |
Collapsible has no visuals. You design the trigger and the layout around it. Put the trigger and the content under the same Collapsible.
Examples
Closed by default
Open by default
Use defaultOpen when most people need the content, but should be able to hide it.
Número: 0001234-56.2024.8.26.0100
Vara: 3ª Vara Cível
Distribuição: 14/03/2024
Controlled
Control open when something else depends on it, such as the trigger label or analytics.
Disabled
Content
- The trigger names what it shows: “Mostrar 3 comentários”, “Histórico de revisões”.
- Add a count or a state when the hidden content has one.
- Change the label or rotate a chevron when open, so the state is visible and not only in the layout.
Do and don’t
Accessibility
- Built on Radix Collapsible. The trigger is a
buttonwitharia-expandedandaria-controlspointing to the content, anddata-stateset toopenorclosed. - Keyboard:
SpaceandEntertoggle it. Tab order is the trigger, then the content when it is open. - When closed, the content is hidden (
hidden), so it is not read and its controls are not reachable. - With
asChild, the child must be a realbutton, so keyboard and role come for free. AdivwithonClickloses them. - With
disabled, the trigger hasdisabledand cannot be toggled. - The height animation is
animate-collapsible-downand-up, and it does not stop forprefers-reduced-motion(see Known issues).
Code
import {
Collapsible,
CollapsibleContent,
CollapsibleTrigger,
} from "@repo/ui/collapsible";
import { Button } from "@repo/ui/button";
<Collapsible>
<CollapsibleTrigger asChild>
<Button size="xs" variant="ghost">
Mostrar 3 comentários
</Button>
</CollapsibleTrigger>
<CollapsibleContent>
<Comments />
</CollapsibleContent>
</Collapsible>;
Props
Collapsible| Prop | Type | Default | Description |
|---|---|---|---|
defaultOpen | boolean | false | Initial open state when the collapsible is uncontrolled |
disabled | boolean | false | Prevents the user from toggling the content |
onOpenChange | (open: boolean) => void | — | Called when the open state changes |
open | boolean | — | Controlled open state |
CollapsibleTrigger| Prop | Type | Default | Description |
|---|---|---|---|
asChild | boolean | false | Renders the child element instead of a button. Use it with a `Button`. |
CollapsibleContent| Prop | Type | Default | Description |
|---|---|---|---|
forceMount | boolean | — | Keeps the content in the DOM when closed, for animation libraries or search by text. |
Known issues
| Issue | Effect |
|---|---|
CollapsibleContent uses animate-collapsible-down and animate-collapsible-up with no motion-reduce variant. |
The height animation runs for people who set reduced motion, against the repository rule to honor prefers-reduced-motion. |
CollapsibleTrigger has no styles. |
Without asChild and a Button, the trigger is an unstyled button with no focus or hover feedback. Always style it. |