accordion.tsx
Overview
The accordion.tsx file defines a set of React components that wrap and extend the functionality of the @radix-ui/react-accordion primitives to provide a customizable, accessible accordion UI element. This file exports four main components—Accordion, AccordionItem, AccordionTrigger, and AccordionContent—which correspond to the typical parts of an accordion interface:
Accordion: The root container managing accordion state and behavior.
AccordionItem: A single collapsible item within the accordion.
AccordionTrigger: The clickable header that toggles the visibility of the associated content.
AccordionContent: The panel that shows or hides content based on the trigger state.
These components enhance the base Radix UI primitives by incorporating styling utilities, additional props for customization (e.g., hiding the down arrow icon), and integration with an icon library for visual affordances.
Components and Their APIs
1. Accordion
function Accordion(props: React.ComponentProps<typeof AccordionPrimitive.Root>): JSX.Element
Purpose: Acts as the root container for the accordion, managing the expanded/collapsed state of its children items.
Props: Accepts all props supported by Radix UI's
AccordionPrimitive.Rootcomponent.Returns: A
AccordionPrimitive.Rootelement with adata-slot="accordion"attribute and forwarded props.
Usage Example
<Accordion type="single" collapsible>
{/* Accordion items here */}
</Accordion>
2. AccordionItem
function AccordionItem({
className,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Item> & { className?: string }): JSX.Element
Purpose: Represents an individual accordion item that can be expanded or collapsed.
Props:
className(optional): Additional CSS classes to apply.Other props are forwarded to the underlying
AccordionPrimitive.Item.
Returns: A
AccordionPrimitive.Itemelement with default bottom border styling and any additional classes provided, plus adata-slot="accordion-item"attribute.
Implementation Detail
Adds a bottom border (
border-b) to visually separate items, except for the last item (last:border-b-0).Uses the utility function
cnto merge class names conditionally.
Usage Example
<AccordionItem value="item-1">
{/* AccordionTrigger and AccordionContent */}
</AccordionItem>
3. AccordionTrigger
function AccordionTrigger({
className,
children,
hideDownIcon = false,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Trigger> & { hideDownIcon?: boolean }): JSX.Element
Purpose: The clickable header section that toggles the visibility of the associated
AccordionContent.Props:
className(optional): Additional CSS classes.children: The content to display inside the trigger (usually a label or title).hideDownIcon(optional, defaultfalse): Iftrue, hides the default down chevron icon.Other props forwarded to
AccordionPrimitive.Trigger.
Returns: An
AccordionPrimitive.Triggerwrapped inside anAccordionPrimitive.Headerwith enhanced styling, accessibility, and optional icon.
Implementation Details
The trigger is styled to be flex container with spacing and transitions.
When open, the chevron icon rotates 180 degrees using CSS transitions.
Accessible focus outlines and hover effects are enabled.
The icon
ChevronDownIconfromlucide-reactis conditionally rendered based onhideDownIcon.Uses
cnutility to merge class names.
Usage Example
<AccordionTrigger>
Section Title
</AccordionTrigger>
{/* Or hiding the icon */}
<AccordionTrigger hideDownIcon>
Section Title
</AccordionTrigger>
4. AccordionContent
function AccordionContent({
className,
children,
...props
}: React.ComponentProps<typeof AccordionPrimitive.Content> & { className?: string }): JSX.Element
Purpose: The collapsible content panel associated with an accordion item.
Props:
className(optional): Additional CSS classes.children: Content to render inside the panel.Other props forwarded to
AccordionPrimitive.Content.
Returns: An
AccordionPrimitive.Contentelement with animation classes for open/close states, overflow handling, and internal padding.
Implementation Details
Uses Radix UI data attributes
[data-state=open]and[data-state=closed]to animate height transitions.Content padding is applied inside a wrapping
<div>.Ensures smooth open/close animations with CSS classes (
animate-accordion-up,animate-accordion-down).
Usage Example
<AccordionContent>
<p>This is the content revealed by clicking the trigger.</p>
</AccordionContent>
Implementation Details and Algorithms
The components leverage Radix UI Accordion primitives for foundational accessibility and behavior.
Styling is augmented via Tailwind CSS utility classes combined with a custom
cnfunction for className concatenation.The accordion allows multiple items but can be configured for single or multiple open items by passing props to
Accordion.The chevron icon rotates smoothly on toggle, providing a familiar visual cue.
Animations for content open/close rely on CSS keyframes triggered by Radix UI's data attributes, ensuring performant and visually appealing transitions.
The
data-slotattributes are added to assist with testing, styling, or slot-based UI frameworks.
Interaction with Other Parts of the System
Icon Library: Uses
ChevronDownIconfromlucide-reactfor consistent iconography.Utility Functions: Imports
cnfrom@/lib/utils(likely a classnames utility) for conditional class name merging.Radix UI: Depends on the
@radix-ui/react-accordionpackage for accessible accordion logic and base components.Styling: Uses Tailwind CSS classes for styling and animations.
Usage Context: Designed to be used wherever an accordion UI pattern is needed in the React application, customizable via props and class names.
Visual Diagram
componentDiagram
component Accordion {
+props: AccordionPrimitive.Root props
}
component AccordionItem {
+className?: string
+props: AccordionPrimitive.Item props
}
component AccordionTrigger {
+className?: string
+children: ReactNode
+hideDownIcon?: boolean
+props: AccordionPrimitive.Trigger props
}
component AccordionContent {
+className?: string
+children: ReactNode
+props: AccordionPrimitive.Content props
}
Accordion <.. AccordionItem : contains >
AccordionItem <.. AccordionTrigger : contains >
AccordionItem <.. AccordionContent : contains >
Diagram Explanation:
The main
Accordioncomponent wraps multipleAccordionItemcomponents.Each
AccordionItemcontains oneAccordionTrigger(header) and oneAccordionContent(collapsible panel).This structure reflects the typical accordion hierarchy and how components relate in the file.
Summary
The accordion.tsx file provides a modular, accessible, and styled accordion component suite built on Radix UI primitives and enhanced with Tailwind CSS and iconography. It supports customization through props and class names and manages common accordion behaviors like toggling and content animation with minimal developer effort. This file is a reusable UI building block within the broader React application.