Toggle the visibility of content with the collapse plugin, which animates the height or the width of an element and keeps the state of its triggers in sync.
Introduction
Collapse shows and hides an element with an animation of its height or its width. Buttons or links act as triggers for one or more collapsible elements, and the plugin keeps aria-expanded on every trigger in sync with the state of its target.
How it works
The plugin moves an element through three classes: .collapse hides it, .collapsing is applied while it animates, and .collapse.show displays it. During the animation the plugin sets an inline height (or width) and a CSS transition moves it between 0 and the size of the content. Padding on the .collapse element itself stays visible at a height of 0, so keep .collapse as a wrapper of its own and put the padding on an element inside it.
Chassis CSS respects user accessibility preferences by automatically disabling animations when the prefers-reduced-motion media query is detected. See the reduced motion guidelines in the accessibility documentation for implementation details.
Basic structure
A collapse is an element with the .collapse class and an id, and a trigger with data-cx-toggle="collapse" that points at it. A <button> names its target in data-cx-target; an <a> names it in href and takes role="button". Add .show to the element to render it expanded on load.
<p class="d-inline-flex gap-2xs">
<a class="button primary" data-cx-toggle="collapse" href="#collapseExample" role="button" aria-expanded="false" aria-controls="collapseExample">
Link with href
</a>
<button class="button primary" type="button" data-cx-toggle="collapse" data-cx-target="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Button with data-cx-target
</button>
</p>
<div class="collapse" id="collapseExample">
<div class="card card-body">
Some placeholder content for the collapse component. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>Horizontal collapse
Add .collapse-horizontal to animate the width of the element instead of its height. The immediate child needs an explicit width, set with a width utility, custom CSS, or an inline style.
The example below sets a min-height on a wrapper to keep the page from shifting as the element opens. Only the width of the child element is required.
<p>
<button class="button primary" type="button" data-cx-toggle="collapse" data-cx-target="#collapseWidthExample" aria-expanded="false" aria-controls="collapseWidthExample">
Toggle width collapse
</button>
</p>
<div style="min-height: 120px;">
<div class="collapse collapse-horizontal" id="collapseWidthExample">
<div class="card card-body" style="width: 300px;">
This is some placeholder content for a horizontal collapse. It's hidden by default and shown when triggered.
</div>
</div>
</div>Multiple targets
A trigger opens every element that its selector matches, and an element can have several triggers. Use a selector that matches more than one element in data-cx-target or href to toggle them together, and point several triggers at the same selector to toggle one element from more than one place.
<p class="d-inline-flex gap-2xs">
<button type="button" class="button primary" data-cx-toggle="collapse" data-cx-target="#multiCollapseExample1" aria-expanded="false" aria-controls="multiCollapseExample1">Toggle first element</button>
<button class="button primary" type="button" data-cx-toggle="collapse" data-cx-target="#multiCollapseExample2" aria-expanded="false" aria-controls="multiCollapseExample2">Toggle second element</button>
<button class="button primary" type="button" data-cx-toggle="collapse" data-cx-target=".multi-collapse" aria-expanded="false" aria-controls="multiCollapseExample1 multiCollapseExample2">Toggle both elements</button>
</p>
<div class="grid grid-cols-2">
<div>
<div class="collapse multi-collapse" id="multiCollapseExample1">
<div class="card card-body">
Some placeholder content for the first collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
<div>
<div class="collapse multi-collapse" id="multiCollapseExample2">
<div class="card card-body">
Some placeholder content for the second collapse component of this multi-collapse example. This panel is hidden by default but revealed when the user activates the relevant trigger.
</div>
</div>
</div>
</div>Accessibility
The state of a collapse is conveyed by attributes on its triggers, which the markup provides and the plugin updates.
ARIA attributes
Add aria-expanded to every trigger, set to "false" for a collapsed target and to "true" for one that starts with .show. The plugin updates the attribute, and toggles a .collapsed class on the trigger, each time the target opens or closes. A trigger with a single target also takes aria-controls with the id of that target, and a trigger that is not a <button> takes role="button".
Keyboard interaction
The plugin adds no keyboard handling of its own. A <button> trigger is focusable and responds to Enter and Space natively, and focus stays on the trigger after it is activated, so prefer <button> over other elements. Patterns with more keyboard support, such as arrow-key navigation between the headers of an accordion, follow the ARIA Authoring Practices Guide accordion pattern and need custom JavaScript.
JavaScript API
The Collapse plugin toggles collapsible elements, syncs the state of their triggers and fires events around each transition. Chassis JS ships as an ES module — import the Collapse class:
import { Collapse } from '@chassis-ui/css'Triggers
The data-attribute API needs no JavaScript on the page beyond the plugin. Add data-cx-toggle="collapse" to a trigger, and a selector for one or more .collapse elements in data-cx-target, or in href on a link. Add data-cx-parent with a selector to the collapsible elements of a group to close the open ones when another opens.
Initialization
For programmatic access — calling methods or listening to events — instantiate each element with the Collapse class:
const collapseElementList = document.querySelectorAll('.collapse')
const collapseList = [...collapseElementList].map(collapseEl => new Collapse(collapseEl))The constructor toggles the element right away unless the toggle option is false:
const cxCollapse = new Collapse('#myCollapse', {
toggle: false
})Methods
The plugin exposes instance methods for programmatic control, and the static methods shared by all plugins:
All Chassis CSS component methods are asynchronous and initiate CSS transitions. Methods return immediately when the transition begins, not when it completes. Calling methods on components that are already transitioning will be ignored to prevent conflicts. Learn more about Chassis JavaScript patterns.
| Method | Description |
|---|---|
dispose | Destroys the collapse instance of an element and removes the data stored on it. |
getInstance | Static — returns the collapse instance of a DOM element, or null: Collapse.getInstance(element). |
getOrCreateInstance | Static — returns the collapse instance of a DOM element, or creates one when it was not initialized: Collapse.getOrCreateInstance(element). |
hide | Hides a collapsible element. Returns to the caller before the element is hidden, that is, before the hidden.cx.collapse event. |
show | Shows a collapsible element. Returns to the caller before the element is shown, that is, before the shown.cx.collapse event. |
toggle | Shows a hidden collapsible element and hides a shown one. Returns to the caller before the transition ends, that is, before the shown.cx.collapse or hidden.cx.collapse event. |
Options
Options are set via data-cx-* attributes or passed to the constructor as an object. Attribute names use the kebab-case form of the option name — data-cx-custom-class, not data-cx-customClass. Attribute values are parsed to their native types:"true" → true, "0" → 0, and valid JSON strings to objects.
Use data-cx-config to pass multiple options as a JSON string:data-cx-config='{"delay":200}'. Individual data-cx-* attributes take precedence over data-cx-config. Individual attributes accept JSON values too, such as data-cx-delay='{"show":100,"hide":200}'.
When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-*attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.
| Name | Type | Default | Description |
|---|---|---|---|
parent | selector, DOM element | null | Closes the open collapsible elements under the given parent when this element is shown, as an accordion does. Set it on the collapsible element. |
toggle | boolean | true | Toggles the collapsible element when the instance is created. |
Events
The plugin fires its events on the collapsible element. Calling event.preventDefault() on show.cx.collapse or hide.cx.collapse cancels the transition.
| Event type | Description |
|---|---|
hide.cx.collapse | Fires immediately when the hide method is called. |
hidden.cx.collapse | Fires when the element is hidden, after its CSS transition has ended. |
show.cx.collapse | Fires immediately when the show method is called. |
shown.cx.collapse | Fires when the element is visible, after its CSS transition has ended. |
const myCollapsible = document.getElementById('myCollapsible')
myCollapsible.addEventListener('hidden.cx.collapse', event => {
document.getElementById('myCollapseTrigger').focus()
})CSS
This component can be customized through Sass variables at compile time.
Sass variables
This component uses Sass variables in scss/config/_defaults.scss to define its defaults.
$transition-collapse: height .35s ease;
$transition-collapse-width: width .35s ease;Classes
The state classes of collapse are defined in scss/_transitions.scss, next to the other transition classes.
.collapse {
&:not(.show) {
display: none;
}
}
.collapsing {
height: 0;
overflow: hidden;
@include transition($transition-collapse);
&.collapse-horizontal {
width: 0;
height: auto;
@include transition($transition-collapse-width);
}
}