Accordion

Accordions organize content into collapsible sections, perfect for FAQs, product details, and navigation menus.

Key Features

  • Single/Multi Expand - Control how many panels can be open
  • Smooth Animations - Configurable slide transitions
  • Programmatic Control - Open, close, toggle panels via API

const accordion = Domma.elements.accordion('#accordion', {
    multiExpand: false,
    activeIndex: 0
});

Quick Start

Section 1
Content for section 1
Section 2
Content for section 2

Examples

FAQ Accordion (Single Expand)

What is Domma?
A lightweight JavaScript framework with DOM, utilities, and UI components.
How big is it?
Approximately 125KB minified.

Multi-Expand Mode

Panel 1
Multiple panels can be open simultaneously
Panel 2
Click headers to toggle independently

Best Practices

  • Accessibility - Use semantic HTML, ARIA attributes for screen readers
  • Clear Headers - Make accordion headers descriptive and actionable
  • Initial State - Consider opening first panel by default for discoverability

API Reference

Option Type Default Description
multiExpand Boolean false Allow multiple panels open
animation Boolean true Enable animations
activeIndex Number/Array null Initially open panel(s)
onChange Function null Panel change callback

Methods

Method Description
open(index) Open panel by index
close(index) Close panel by index
toggle(index) Toggle panel
openAll() Open all panels
closeAll() Close all panels

Declarative Configuration

Use Domma's config engine for declarative accordion setup with $.setup(). This approach is cleaner, more maintainable, and allows you to configure multiple components at once.

Single Accordion Setup

$.setup({
    '#my-accordion': {
        component: 'accordion',
        options: {
            multiExpand: false,
            activeIndex: 0,
            animation: 'slide'
        }
    }
});

Multiple Accordions at Once

// Configure all FAQ accordions site-wide
$.setup({
    '.faq-accordion': {
        component: 'accordion',
        options: {
            multiExpand: false,
            activeIndex: 0
        },
        events: {
            // Add click tracking
            change: (index, $item, instance) => {
                console.log(`FAQ ${index + 1} opened`);
            }
        }
    },
    '#settings-accordion': {
        component: 'accordion',
        options: {
            multiExpand: true  // Allow multiple sections open
        }
    }
});

With Initial State & Events

$.setup({
    '#product-details': {
        component: 'accordion',
        options: {
            activeIndex: 0,
            onChange: (index) => {
                // Track which section users view
                analytics.track('Product Section Viewed', {
                    section: ['specs', 'reviews', 'shipping'][index]
                });
            }
        },
        initial: {
            // Apply additional styling
            css: {
                border: '1px solid #e0e0e0',
                borderRadius: '8px'
            }
        }
    }
});

Benefits of Declarative Config

  • Centralized Configuration - All component setup in one place
  • Easier Maintenance - Update options without touching HTML
  • Batch Operations - Configure multiple components at once
  • Mutable Config - Update with $.update(), query with $.config(), reset with $.reset()

Programmatic vs Declarative

Programmatic (Traditional)

// Scattered throughout code
const acc1 = Domma.elements.accordion(
    '#accordion-1',
    {multiExpand: false}
);

const acc2 = Domma.elements.accordion(
    '#accordion-2',
    {multiExpand: true}
);

Declarative (Recommended)

// Clean, centralized config
$.setup({
    '#accordion-1': {
        component: 'accordion',
        options: {multiExpand: false}
    },
    '#accordion-2': {
        component: 'accordion',
        options: {multiExpand: true}
    }
});

Related Elements