React Collapse: Smooth Collapsible Content, Setup & Examples
Implement accessible, animated collapsible sections and accordions with react-collapse — installation, usage patterns, customization, and performance tips.
Quick summary (what you’ll get)
react-collapse is a lightweight React library for creating height-based collapsible content with smooth animations. It lets you control expanded/collapsed states declaratively while handling height transitions for you.
This guide covers installation, a concise getting-started example, patterns (accordion vs independent sections), animation/customization options, and accessibility considerations so you can ship robust collapsible UI quickly.
Want a hands-on tutorial? Follow this practical walkthrough: react-collapse tutorial.
- Quick install and example
- Core concepts and props
- Animation tuning, accessibility, and performance
Getting started — installation and setup
Install react-collapse via npm or yarn. The package is straightforward to add and pairs with your state management — local useState hooks or global stores both work. For direct package installation, see the npm page: react-collapse installation.
Typical install command:
npm install react-collapse --save
# or
yarn add react-collapse
Once installed, import the Collapse component and wrap the collapsible content. react-collapse animates by transitioning height from 0 to content height; you control when it is open via a boolean prop. This design means your collapsed content remains mounted (useful for preserving DOM state) unless you add conditional rendering around it.
Core concepts and a minimal example
The main API is intentionally minimal: you render . isOpened controls the expanded state. Under the hood, the component measures content height and animates the inline style height, avoiding “jump” layout issues.
Here is a concise example showing an independent collapsible panel and a simple expand/collapse button. This pattern is ideal for one-off collapsible sections such as details panels or FAQ items.
import React, {useState} from 'react'
import { Collapse } from 'react-collapse'
function Example() {
const [isOpened, setOpen] = useState(false)
return (
Collapsible content lives here. It remains mounted and animates height.
)
}
This code gives you immediate, smooth collapse/expand behavior with minimal wiring. For accordion behavior (only one open panel at a time), manage a single index or id as the open key and pass the boolean to each Collapse instance accordingly.
Animation, customization, and common patterns
react-collapse handles basic height transitions but lets you customize timing and CSS. For example, combine inline transition durations with CSS variables or pass style wrappers to tweak easing. If you need cross-fade or transform effects, animate an inner wrapper while letting react-collapse manage the height.
Common customizations include:
controlling transition timing to match your design system, adding enter/exit callbacks for analytics or focus management, and wrapping content in an element that applies padding and background independently of the height measurement.
If you need more complex animations (scaling, opacity), compose react-collapse with libraries like react-transition-group or use CSS transitions on child elements. Keep in mind that height-based animation is the most layout-friendly for content of variable size. For reference and deeper examples, check the project repository: react-collapse library.
Accessibility, performance, and best practices
Accessible collapsible widgets are more than animation — they need semantic markup and aria attributes. Use button elements to toggle panels, set aria-expanded on the toggle, and use aria-controls to reference the panel ID. The panel should have role=”region” and an accessible name (aria-labelledby) where appropriate.
Because react-collapse keeps content mounted by default, it’s often better for keyboard and screenreader users (they can still tab into content when expanded). For large or media-heavy content you may prefer to unmount when collapsed for performance; combine conditional rendering with react-collapse only when necessary, or lazy-load heavy content inside the panel when first opened.
Performance tips: avoid measuring-heavy DOM within the collapsible on every frame, debounce resize handlers, and prefer CSS transforms where possible for GPU-accelerated micro-interactions. Test on lower-end devices to verify the perceived smoothness of the collapse animation.
Troubleshooting: common issues and fixes
Problem: Jumping content or incorrect measured height. Fix: ensure the inner content isn’t positioned absolutely and doesn’t use CSS that removes it from normal flow (e.g., float or transform that alters measured height). Measure-related bugs often come from non-static layout inside the measured container.
Problem: Focus management when collapsing. Fix: when closing a panel, programmatically move focus to the trigger or next logical control to avoid keyboard users losing context. Add small delay hooks if you need focus to occur after the collapse animation completes.
Problem: Conflicting CSS transitions. Fix: isolate height transitions to the element controlled by react-collapse. If you animate padding/margins concurrently, you may get layout thrashing; prefer animating inner elements for visual polish while the outer height animates.
Where to go next
For an end-to-end tutorial and real-world examples, walk through this hands-on article that covers building a collapsible FAQ and an accordion using react-collapse: react-collapse tutorial. That tutorial demonstrates patterns you can drop into apps right away.
If you need to inspect the package or contribute, see the GitHub repo: react-collapse library. For package releases and install instructions, consult the npm page: react-collapse installation.
Combine these resources with the accessibility checklist above to ship collapsible components that look great and work reliably for all users.
FAQ
How do I install and import react-collapse?
Install via npm or yarn (npm install react-collapse or yarn add react-collapse). Then import the component: import { Collapse } from ‘react-collapse’. Manage isOpened state to control expansion.
Should I unmount content when collapsed?
Not necessarily. react-collapse keeps content mounted to preserve state and improve accessibility. Unmount only for large, costly children or when you explicitly need to remove DOM nodes for logic reasons — combine conditional rendering with the Collapse wrapper accordingly.
How do I make an accessible accordion with react-collapse?
Use semantic button triggers with aria-expanded and aria-controls, ensure each panel has a unique ID and role=”region”, and manage focus on open/close. Control a single open panel state to implement accordion behavior. Test with a screen reader and keyboard navigation.
Semantic core (expanded keyword set)
Primary queries: react-collapse, React collapsible content, react-collapse tutorial, React expand collapse, react-collapse installation, React accordion component, react-collapse example.
Secondary / related: React collapse animation, react-collapse setup, React collapsible section, react-collapse customization, react-collapse library, react-collapse getting started, React smooth collapse.
Clarifying / LSI & variants: collapse component for React, collapsible panels React, accordion React accessible, height transition React, animated collapse React, react collapse npm, react-collapse GitHub, collapsible section example.
