Accordion Section
An interactive accordion component for displaying frequently asked questions or any collapsible content. Features smooth animations, accessibility support, and flexible data loading from JSON files.
An interactive accordion component for displaying frequently asked questions or any collapsible content. Features smooth animations, accessibility support, and flexible data loading from JSON files.
Example 1
This example shows all available FAQs from the data source with a background color. The first accordion id open by default. Open a new accordion will close the old one.
Start by cloning the repository and running npm install to install dependencies. Then use npm start to launch the development server. The component library will be available at http://localhost:3000, where you can explore all available components and their documentation.
Each component contains several key files: a .yml file with frontmatter examples, a .njk Nunjucks template for markup, optional .css for component-specific styles, optional .js for interactive behavior, a manifest.json for dependencies and metadata, and an optional README.md for documentation.
The metalsmith-bundled-components plugin automatically scans pages to identify which components are used, then bundles only the required CSS and JavaScript for optimal performance. It applies PostCSS processing for autoprefixing and minification, generating per-page assets with no unused code.
Yes! Follow the established component structure: create a new folder in either lib/layouts/components/_partials/ for small UI elements or lib/layouts/components/sections/ for large page sections. Include the necessary files (template, manifest, optional styles and scripts) and ensure your component follows the project's conventions for semantic HTML and accessibility.
Use the comprehensive Mocha test suite by running npm test. When adding new components, ensure you add test cases for the component's manifest, test with various configuration options, verify the component renders without errors, test responsive behavior and accessibility, and validate against the Metalsmith2025 Starter structure.
Example 2
Curated Content
This example shows only selected FAQs, allows multiple items to be expanded, and has no background color. Note the OPEN/CLOSE ALL link above the first accordion.
Start by cloning the repository and running npm install to install dependencies. Then use npm start to launch the development server. The component library will be available at http://localhost:3000, where you can explore all available components and their documentation.
Each component contains several key files: a .yml file with frontmatter examples, a .njk Nunjucks template for markup, optional .css for component-specific styles, optional .js for interactive behavior, a manifest.json for dependencies and metadata, and an optional README.md for documentation.
Yes! Follow the established component structure: create a new folder in either lib/layouts/components/_partials/ for small UI elements or lib/layouts/components/sections/ for large page sections. Include the necessary files (template, manifest, optional styles and scripts) and ensure your component follows the project's conventions for semantic HTML and accessibility.
Example 3
Visual Enhancement
This example demonstrates fancy accordion items with full-width background images. Each accordion header displays a striking visual that enhances the content presentation. Perfect for feature showcases, testimonials, or visually-driven content.
Great accordions combine smooth animations, full accessibility support, and flexible styling options. They should work seamlessly across devices and provide clear visual feedback for user interactions.
Background images add visual interest and can help communicate the content theme before users even expand the accordion. They create a more engaging and memorable interface.
Yes! You can mix fancy accordion items with background images alongside simple text-only items in the same accordion. Just omit the isFancy flag and background properties for standard styling.
For our examples, FAQ data files are stored in lib/data/ as JSON files. Each FAQ file should have the following structure:
Basic FAQ structure:
{
"id": "unique-id",
"title": "Your question here?",
"answer": "Your detailed answer here."
}
Fancy FAQ with background image:
{
"id": "unique-id",
"title": "Your question here?",
"answer": "Your detailed answer here.",
"isFancy": true,
"background": {
"image": "/assets/images/sample9.jpg"
}
}
Fancy FAQ with background color:
{
"id": "unique-id",
"title": "Your question here?",
"answer": "Your detailed answer here.",
"isFancy": true,
"background": {
"color": "#3498db"
}
}
The accordion uses requestAnimationFrame for animations rather than CSS transitions. This approach enables simultaneous animation of multiple panels, which is essential for smooth visual transitions when one panel closes while another opens (in single-panel mode) or when using the "toggle all" functionality.
CSS transitions would schedule these animations sequentially, resulting in janky, staggered motion. By using requestAnimationFrame with a batched animations array, all height changes occur frame-perfect synchronized, creating a polished user experience.
The JavaScript handles:
The component includes responsive styles with:
faqs:
scope: "all"
source: "faqs"
selections:
- "getting-started"
- "component-structure"
expandIndex: 0
allowMultiple: false
| Property | Type | Description |
|---|---|---|
faqs.scope | string | "all" or "selections" - determines data loading |
faqs.source | string | data source in lib/data/ (e.g., "faqs") |
faqs.selections | array | array of IDs when scope is "selections" |
expandIndex | number | index of item to expand by default (0-based) |
allowMultiple | boolean | allow multiple expanded items |