2025-02-27 Web Development, Productivity
Versatile Accordion Component Built with ShadCN and MDX Support
By O. Wolfson
Accordions are an essential UI component for organizing content in a compact and interactive manner. In this article, we will explore how to build an enhanced accordion component using ShadCN that supports both Markdown (MDX) components and raw JSX or plain text as content. This component is integrated into a Next.js 15 application that handles MDX via @/next/mdx
.
Example:
MDX snippets are rendered as MDX components, so they can be used in the accordion content:
Why Use ShadCN for Accordions?
ShadCN provides a set of pre-styled, accessible UI components built on top of Radix UI, making it an excellent choice for creating a sleek and functional accordion component. With minimal styling and built-in accessibility, it allows developers to focus on functionality rather than reinventing the wheel.
The Accordion Component
Our accordion component will allow each item to contain:
- A trigger, which represents the title or label of the accordion item.
- A content section that can accept either:
- Plain text
- JSX elements
- MDX components
By supporting MDX components, this accordion can seamlessly integrate interactive Markdown-based elements, making it particularly useful for documentation, blogs, and content-heavy applications.
Implementation
Below is the implementation of the AccordionComponent
, as used in a Next.js 15 application:
Integration in a Next.js 15 Application
In a Next.js 15 app that supports MDX via @/next/mdx
, we dynamically load MDX content from a structured content directory and use the accordion to present it. Here’s how the accordion component is used within an MDX-powered blog post:
Dynamic MDX Loading
The Next.js application dynamically imports MDX files based on a slug
, ensuring that the correct content is loaded into the accordion. This is accomplished using the following function:
This function ensures that our accordion seamlessly integrates with the dynamically imported MDX content, allowing for a flexible and scalable content architecture.
Key Features
1. Flexible Content Support
The component can accept different types of content:
- Plain strings or JSX elements (for regular use cases)
- MDX components (ideal for markdown-driven applications)
2. Collapsible Single Selection
The Accordion
component is configured as type="single" collapsible
, meaning only one item can be open at a time, and users can collapse an open section by clicking on its trigger.
3. Dynamic Item Rendering
Using .map()
, the component dynamically renders the list of accordion items while ensuring unique keys for React’s reconciliation process.
4. ShadCN Styling
The component uses ShadCN’s pre-styled Accordion
, AccordionItem
, AccordionTrigger
, and AccordionContent
, ensuring a polished and consistent design without extra CSS.
Conclusion
By leveraging ShadCN’s pre-built components and MDX’s dynamic capabilities, you can create a seamless and engaging user experience with minimal effort.
Looking for more UI enhancements? Check out ShadCN’s documentation for more pre-styled components.