Skip to content

Accordion

The Accordion component provides a way to organize content into collapsible sections, allowing users to expand or collapse each section individually. It’s useful for presenting information in a compact and organized manner.

See the Example by Serhii Pimenov on CodePen.

<div data-role="accordion">
<div class="frame">
<div class="heading">Section 1</div>
<div class="content">Content for section 1</div>
</div>
<div class="frame">
<div class="heading">Section 2</div>
<div class="content">Content for section 2</div>
</div>
<div class="frame active">
<div class="heading">Section 3 (Initially Open)</div>
<div class="content">Content for section 3</div>
</div>
</div>
// Initialize accordion on an existing element
Metro.makePlugin("#myAccordion", "accordion");
// Access the accordion object
const accordion = Metro.getPlugin("#myAccordion", "accordion");
ParameterTypeDefaultDescription
accordionDeferrednumber0Deferred initialization time in milliseconds
showMarkerbooleantrueShow/hide marker (arrow) in the heading
materialbooleanfalseUse material design style
durationnumber100Animation duration for expanding/collapsing
oneFramebooleantrueAllow only one frame to be open at a time
showActivebooleantrueShow active frames on initialization
clsFramestring""Additional CSS class for frames
clsHeadingstring""Additional CSS class for headings
clsContentstring""Additional CSS class for content
clsAccordionstring""Additional CSS class for the accordion container
clsActiveFramestring""Additional CSS class for active frames
clsActiveFrameHeadingstring""Additional CSS class for active frame headings
clsActiveFrameContentstring""Additional CSS class for active frame content
EventDescription
onFrameOpenTriggered when a frame is opened
onFrameBeforeOpenTriggered before a frame is opened (return false to cancel)
onFrameCloseTriggered when a frame is closed
onFrameBeforeCloseTriggered before a frame is closed (return false to cancel)
onAccordionCreateTriggered when the accordion is created
  • open(index) - Opens the frame at the specified index.
  • close(index) - Closes the frame at the specified index.
  • toggle(index) - Toggles the state of the frame at the specified index.
  • getActive() - Returns an array of indices of currently active (open) frames.
  • destroy() - Destroys the accordion component and removes all event listeners.
// Opens the third frame (index 2)
const accordion = Metro.getPlugin('#myAccordion', 'accordion')
accordion.open(2);
VariableDefault (Light)Dark ModeDescription
--accordion-heading-background#f8f8f8#2b2d30Background color of the accordion headings
--accordion-heading-color#191919#dfe1e5Text color of the accordion headings
/* Custom styling for a specific accordion */
#myCustomAccordion {
--accordion-heading-background: #e0f7fa;
--accordion-heading-color: #006064;
}

By default, the accordion shows markers (arrows) in the headings. You can control this with the showMarker option or by adding/removing the marker-on class.

Enable material design styling by setting the material option to true or by adding the material class to the accordion.

For right-to-left languages, add the rtl class or dir="rtl" attribute to the accordion.

The Accordion component includes ARIA attributes for improved accessibility:

  • Headings have role="button" and aria-expanded attributes
  • Content sections have role="region" and aria-labelledby attributes
  • Keyboard navigation is supported (Enter/Space to toggle, Up/Down arrows to navigate)