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
Section titled “Basic Collapse”<!-- Basic collapse structure --><div class="collapse-toggle">Toggle Content</div><div data-role="collapse"> <p>This content can be collapsed or expanded.</p></div>
Custom Toggle Element
Section titled “Custom Toggle Element”<!-- 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
Section titled “Initially Collapsed”<!-- 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>
Plugin Parameters
Section titled “Plugin Parameters”The Collapse component can be configured with the following options:
Parameter | Type | Default | Description |
---|---|---|---|
collapseDeferred | number | 0 | Delay in milliseconds before the collapse action starts |
collapsed | boolean | false | Whether the element should be initially collapsed |
toggleElement | string/boolean | false | Selector for a custom toggle element (if not using the default adjacent toggle) |
duration | number | 100 | Duration of the collapse/expand animation in milliseconds |
Event Callbacks
Section titled “Event Callbacks”Callback | Description |
---|---|
onExpand | Triggered when the element is expanded |
onCollapse | Triggered when the element is collapsed |
onCollapseCreate | Triggered when the collapse component is created |
Example of Configuration
Section titled “Example of Configuration”// Global setupMetro.collapseSetup({ duration: 300, collapsed: true});
// Individual element setup via JavaScriptconst collapseElement = Metro.makePlugin("#myCollapse", "collapse", { duration: 500, collapsed: true});
API Methods
Section titled “API Methods”The Collapse component provides the following API methods:
collapse(immediate)
Section titled “collapse(immediate)”Collapses the element. If immediate
is true, collapses without animation.
// Get the collapse componentconst collapse = Metro.getPlugin('#myCollapse', 'collapse');
// Collapse with animationcollapse.collapse();
// Collapse immediately without animationcollapse.collapse(true);
expand(immediate)
Section titled “expand(immediate)”Expands the element. If immediate
is true, expands without animation.
// Expand with animationcollapse.expand();
// Expand immediately without animationcollapse.expand(true);
close(immediate)
Section titled “close(immediate)”Alias for collapse()
.
open(immediate)
Section titled “open(immediate)”Alias for expand()
.
isCollapsed()
Section titled “isCollapsed()”Returns true if the element is currently collapsed.
// Check if element is collapsedif (collapse.isCollapsed()) { console.log("Element is collapsed");}
toggleState()
Section titled “toggleState()”Toggles between collapsed and expanded states.
// Toggle collapse statecollapse.toggleState();
Styling
Section titled “Styling”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.
Available CSS Classes
Section titled “Available CSS Classes”.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
Accessibility
Section titled “Accessibility”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
Best Practices
Section titled “Best Practices”- Use clear visual indicators for the toggle element to show it’s interactive
- Consider using icons (like arrows or plus/minus) to indicate the current state
- Keep animations short for better user experience
- Group related collapse elements in accordion-style interfaces when appropriate
- Ensure the toggle element is easily clickable on mobile devices