Collapse
The Collapse component provides a smooth, animated transition for showing and hiding content. It's an essential UI pattern for managing content density and progressive disclosure of information.
Best Practices
Clear Triggers: Always use clear, descriptive labels for collapse triggers that indicate the action and content.
Progressive Disclosure: Use collapse to hide secondary content or advanced options that aren't immediately needed.
Performance: For large content, consider using
unmountOnExitto improve performance by unmounting content when collapsed.Animation Timing: Adjust the
timeoutprop to match your application's animation speed for a cohesive feel.Consistent Behavior: Maintain consistent behavior across your application - don't mix different animation patterns for similar interactions.
Accessibility Considerations
Keyboard Navigation: Ensure the collapse trigger is keyboard accessible and can be activated with both Enter and Space keys.
ARIA Attributes: The component automatically handles
aria-expandedandaria-controlsattributes.Focus Management: Consider managing focus when content is expanded/collapsed, especially for screen reader users.
Screen Reader Announcements: Use
aria-liveregions to announce state changes to screen reader users.
When to Use
- FAQs and help sections
- Forms with optional or advanced fields
- Content that can be toggled on/off
- Accordion interfaces
- Mobile navigation menus
When to Consider Something Else
- For simple show/hide without animation, consider conditional rendering
- For modal dialogs or overlays, use the Dialog component instead
- For tabbed interfaces, consider using the Tabs component
Performance Optimization
- Use
unmountOnExitfor better performance with large content - Consider code-splitting for complex content within collapsible sections
- Memoize the content if it includes expensive computations
Common Patterns
- Accordion: Multiple collapsible sections where only one can be open at a time
- Expandable Cards: Cards with a summary that can be expanded for more details
- Progressive Disclosure: Show advanced options or additional information on demand
- Mobile Navigation: Collapsible menus for mobile views