Skip to content

Collapse

The Collapse component provides a way to toggle the visibility of content with smooth animations. It’s commonly used for accordion-style interfaces, expandable panels, and other UI elements that need to show or hide content.

<!-- Basic collapse structure -->
<div class="collapse-toggle">Toggle Content</div>
<div data-role="collapse">
<p>This content can be collapsed or expanded.</p>
</div>
<!-- Using a custom toggle element -->
<button id="customToggle">Toggle Content</button>
<div data-role="collapse" data-toggle-element="#customToggle">
<p>This content is toggled by a custom element.</p>
</div>
<!-- Initially collapsed content -->
<div class="collapse-toggle">Toggle Content</div>
<div data-role="collapse" data-collapsed="true">
<p>This content is initially hidden.</p>
</div>

The Collapse component can be configured with the following options:

ParameterTypeDefaultDescription
collapseDeferrednumber0Delay in milliseconds before the collapse action starts
collapsedbooleanfalseWhether the element should be initially collapsed
toggleElementstring/booleanfalseSelector for a custom toggle element (if not using the default adjacent toggle)
durationnumber100Duration of the collapse/expand animation in milliseconds
CallbackDescription
onExpandTriggered when the element is expanded
onCollapseTriggered when the element is collapsed
onCollapseCreateTriggered when the collapse component is created
// Global setup
Metro.collapseSetup({
duration: 300,
collapsed: true
});
// Individual element setup via JavaScript
const collapseElement = Metro.makePlugin("#myCollapse", "collapse", {
duration: 500,
collapsed: true
});

The Collapse component provides the following API methods:

Collapses the element. If immediate is true, collapses without animation.

// Get the collapse component
const collapse = Metro.getPlugin('#myCollapse', 'collapse');
// Collapse with animation
collapse.collapse();
// Collapse immediately without animation
collapse.collapse(true);

Expands the element. If immediate is true, expands without animation.

// Expand with animation
collapse.expand();
// Expand immediately without animation
collapse.expand(true);

Alias for collapse().

Alias for expand().

Returns true if the element is currently collapsed.

// Check if element is collapsed
if (collapse.isCollapsed()) {
console.log("Element is collapsed");
}

Toggles between collapsed and expanded states.

// Toggle collapse state
collapse.toggleState();

The Collapse component uses CSS transitions for its animations. The collapse effect is achieved by manipulating the max-height property with a transition duration of 0.3 seconds.

  • .collapse - The main class for the collapsible content
  • .collapse-toggle - Class for the toggle element
  • .active-toggle - Applied to the toggle element when the content is expanded
  • .collapsed - Applied to the collapse element when it’s collapsed

For better accessibility:

  • Use semantic HTML elements for your toggle (like <button>)
  • Add appropriate ARIA attributes to indicate the expanded/collapsed state
  • Ensure keyboard navigation works properly with your toggle elements
  1. Use clear visual indicators for the toggle element to show it’s interactive
  2. Consider using icons (like arrows or plus/minus) to indicate the current state
  3. Keep animations short for better user experience
  4. Group related collapse elements in accordion-style interfaces when appropriate
  5. Ensure the toggle element is easily clickable on mobile devices