Skip to content

Collapsible

Shows or hides one block of content from a single trigger.

3 deliveries this week

Monday: steel brackets from Tornival

Installation

pnpm dlx shadcn@latest add @silkware/collapsible

To copy the component by hand, install its dependency:

pnpm add react-aria-components

Then copy the source into components/ui/collapsible.tsx:

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

Monday: steel brackets from Tornival

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

KeyAction
SpaceExpands or collapses the content.
EnterExpands or collapses the content.
TabMoves focus to the next focusable element.

React Aria provides:

  • A trigger button with aria-expanded and aria-controls pointing 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.

Do

Hide detail the user can do without, and name it on the trigger.

Don’t

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.

PropTypeDefaultDescription
isExpanded?booleanNoneWhether the content is visible, for a controlled collapsible.
defaultExpanded?booleanfalseWhether the content is visible at first.
onExpandedChange?(isExpanded: boolean) => voidNoneCalled when the content is shown or hidden.
isDisabled?booleanfalseIgnores 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.

PropTypeDefaultDescription
children?ReactNodeNoneThe label of the trigger.
className?stringNoneThe only styling: there are no base classes.

CollapsibleContent

The hidden block. Renders a React Aria DisclosurePanel. It accepts every DisclosurePanel prop.

PropTypeDefaultDescription
childrenReactNodeNoneThe content shown when the collapsible is open.
className?stringNoneClasses for the panel.