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.

Example 1

All FAQs

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

Selected FAQs

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.

Open All

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

Fancy Accordion with Background Images

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.

Open All

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.

Implementation Notes

Data Structure

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"
  }
}

JavaScript Functionality

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:

  • Simultaneous expand/collapse animations for multiple panels
  • Click events on accordion headers
  • Managing ARIA attributes for accessibility (aria-expanded)
  • Screen reader announcements when panels open/close
  • Single or multiple item expansion based on configuration
  • Initial state management (expand specific item if configured)
  • Full keyboard navigation (Arrow keys, Home, End, Enter, Space)
  • Toggle all functionality with dynamic button text updates

Styling

The component includes responsive styles with:

  • Height manipulation via JavaScript for smooth synchronized animations
  • Custom easing function (cubic bezier) for natural motion
  • Hover and focus states for accessibility
  • Customizable through CSS variables

Configuration

faqs:
  scope: "all"
  source: "faqs"
  selections:
    - "getting-started"
    - "component-structure"
expandIndex: 0
allowMultiple: false

Notes

  • Load all FAQs or select specific ones by ID
  • Control expand behavior and multiple item expansion
  • Full keyboard navigation and screen reader support
  • CSS-based transitions for expand/collapse
PropertyTypeDescription
faqs.scopestring"all" or "selections" - determines data loading
faqs.sourcestringdata source in lib/data/ (e.g., "faqs")
faqs.selectionsarrayarray of IDs when scope is "selections"
expandIndexnumberindex of item to expand by default (0-based)
allowMultiplebooleanallow multiple expanded items