collapsible.tsx
Overview
The collapsible.tsx file serves as a wrapper module that re-exports key components from the @radix-ui/react-collapsible library. Its primary purpose is to provide a simplified and unified interface for implementing collapsible (expandable/collapsing) UI sections within a React application.
By importing and re-exporting the Root, CollapsibleTrigger, and CollapsibleContent components from the Radix UI Collapsible primitive, this file enables developers to easily integrate accessible and customizable collapsible behavior without directly interacting with the underlying library in other parts of the application.
Components and Exports
This file exports three React components that together form the collapsible UI pattern:
1. Collapsible
Source:
CollapsiblePrimitive.RootType: React component (Root container)
Description:
Acts as the root container managing the open/closed state of the collapsible section. It wraps both the trigger and the content components.Usage Example:
import { Collapsible, CollapsibleTrigger, CollapsibleContent } from './collapsible'; function MyCollapsibleSection() { return ( <Collapsible> <CollapsibleTrigger> Toggle Details </CollapsibleTrigger> <CollapsibleContent> <p>Here are the details that can be expanded or collapsed.</p> </CollapsibleContent> </Collapsible> ); }Props:
Accepts all standard props defined by Radix UI'sCollapsiblePrimitive.Root, including but not limited to:open?: boolean— Controlled open statedefaultOpen?: boolean— Initial open state (uncontrolled)onOpenChange?: (open: boolean) => void— Callback when open state changes
2. CollapsibleTrigger
Source:
CollapsiblePrimitive.CollapsibleTriggerType: React component (Trigger button)
Description:
Acts as the interactive element that toggles the collapsible content's visibility. Typically rendered as a button or clickable element.Usage:
Must be used within theCollapsiblecomponent to toggle the open state.Props:
Accepts all props typical for interactive elements, including event handlers and accessibility attributes.
3. CollapsibleContent
Source:
CollapsiblePrimitive.CollapsibleContentType: React component (Collapsible content)
Description:
Contains the content that is shown or hidden depending on the collapsible state. It handles the animation and accessibility roles for content expansion.Usage:
Rendered insideCollapsiblealongsideCollapsibleTrigger.
Implementation Details
This file uses ES6 module syntax to import and re-export components.
It imports the entire Radix UI Collapsible primitive namespace as
CollapsiblePrimitive, then selectively aliases:Root→CollapsibleCollapsibleTrigger→CollapsibleTriggerCollapsibleContent→CollapsibleContent
No additional logic or styling is added here; the file acts purely as a re-exporting convenience layer.
The
'use client';directive at the top indicates that this file is intended for use in a React environment that supports client-side rendering (such as Next.js app directory).
Interaction with Other Parts of the System
This file is intended to be imported wherever collapsible UI behavior is needed.
By wrapping Radix UI primitives, it standardizes collapsible usage across the application.
It depends on the
@radix-ui/react-collapsiblepackage, which must be installed in the project.This module may be extended or customized if additional behavior or styling is required in the future.
It interacts with the React rendering lifecycle and accessibility features baked into Radix UI components.
Visual Diagram
classDiagram
class CollapsiblePrimitive {
<<namespace>>
+Root
+CollapsibleTrigger
+CollapsibleContent
}
class Collapsible {
<<alias>>
}
class CollapsibleTrigger {
<<alias>>
}
class CollapsibleContent {
<<alias>>
}
Collapsible --|> CollapsiblePrimitive.Root : alias
CollapsibleTrigger --|> CollapsiblePrimitive.CollapsibleTrigger : alias
CollapsibleContent --|> CollapsiblePrimitive.CollapsibleContent : alias
Summary
The collapsible.tsx file is a lightweight adapter that exposes Radix UI's collapsible primitives under simplified and consistent names. It facilitates the creation of accessible, animated collapsible sections in React applications without requiring redundant imports or direct dependency management on Radix UI primitives in each usage context. This modular approach enhances code maintainability and clarity throughout the project.