Accordion
Toggle the visibility of content across your project with a few classes and out additional Javascript plugin (only for accessibility).
How it works#
A radio or checkbox is used to show and hide content. A label with an additional role
attribute is used to trigger a toggle of the div with class .accordion-collapse
. Collapsing an element will animate the max-height
from it’s current value to 0
.
Accordion example#
Using the following syntax, you can use a collapse behavior to create the accordion.
Multiple#
<div class="accordion" id="accordion1">
<div class="accordion-item">
<input type="checkbox" data-controller="accordion/input" class="accordion-check sr-only"
id="a5c4198b" name="accordion1_first" checked />
<label type="button" class="accordion-label" for="a5c4198b" id="71ca3963"
data-target="#62aec359" aria-expanded="false" aria-controls="62aec359">
<h5 class="accordion-item-header">Collapsible Group Item #1</h5>
<svg class="icon accordion-icon" xmlns="http://www.w3.org/2000/svg" role="img"
focusable="false">
<title>Expand</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#arrow-down">
</use>
</svg>
</label>
<div id="62aec359" class="accordion-collapse" aria-labelledby="71ca3963"
data-parent="#accordion1">Lorem ipsum</div>
</div>
<div class="accordion-item">
<input type="checkbox" data-controller="accordion/input" class="accordion-check sr-only"
id="8c8a9406" name="accordion1_second" />
<label type="button" class="accordion-label" for="8c8a9406" id="1c642de8"
data-target="#36728f37" aria-expanded="false" aria-controls="36728f37">
<h5 class="accordion-item-header">Collapsible Group Item #2</h5>
<svg class="icon accordion-icon" xmlns="http://www.w3.org/2000/svg" role="img"
focusable="false">
<title>Expand</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#arrow-down">
</use>
</svg>
</label>
<div id="36728f37" class="accordion-collapse" aria-labelledby="1c642de8"
data-parent="#accordion1">Lorem ipsum</div>
</div>
<div class="accordion-item">
<input type="checkbox" data-controller="accordion/input" class="accordion-check sr-only"
id="d1dc5829" name="accordion1_third" disabled />
<label type="button" class="accordion-label" for="d1dc5829" id="23532e44"
data-target="#109e9f42" aria-expanded="false" aria-controls="109e9f42">
<h5 class="accordion-item-header">Collapsible Group Item #3</h5>
<svg class="icon accordion-icon" xmlns="http://www.w3.org/2000/svg" role="img"
focusable="false">
<title>Expand</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#arrow-down">
</use>
</svg>
</label>
<div id="109e9f42" class="accordion-collapse" aria-labelledby="23532e44"
data-parent="#accordion1">Lorem ipsum</div>
</div>
</div>
Instead of a type="checkbox"
you can simply use the type="radio"
to make only a single accordion-item extensible at once.
If you wantan accordion-item to be extended on page load, simply set the input
to checked
.
Single#
<div class="accordion" id="accordion2">
<div class="accordion-item">
<input type="radio" data-controller="accordion/input" class="accordion-check sr-only"
id="27015bbf" name="accordion2_input" checked />
<label type="button" class="accordion-label" for="27015bbf" id="39c4768b"
data-target="#7303a59f" aria-expanded="false" aria-controls="7303a59f">
<h5 class="accordion-item-header">Collapsible Group Item #1</h5>
<svg class="icon accordion-icon" xmlns="http://www.w3.org/2000/svg" role="img"
focusable="false">
<title>Expand</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#arrow-down">
</use>
</svg>
</label>
<div id="7303a59f" class="accordion-collapse" aria-labelledby="39c4768b"
data-parent="#accordion2">Lorem ipsum</div>
</div>
<div class="accordion-item">
<input type="radio" data-controller="accordion/input" class="accordion-check sr-only"
id="f63880ba" name="accordion2_input" />
<label type="button" class="accordion-label" for="f63880ba" id="dfcdf850"
data-target="#509dc98e" aria-expanded="false" aria-controls="509dc98e">
<h5 class="accordion-item-header">Collapsible Group Item #2</h5>
<svg class="icon accordion-icon" xmlns="http://www.w3.org/2000/svg" role="img"
focusable="false">
<title>Expand</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#arrow-down">
</use>
</svg>
</label>
<div id="509dc98e" class="accordion-collapse" aria-labelledby="dfcdf850"
data-parent="#accordion2">Lorem ipsum</div>
</div>
<div class="accordion-item">
<input type="radio" data-controller="accordion/input" class="accordion-check sr-only"
id="a9302e00" name="accordion2_input" disabled />
<label type="button" class="accordion-label" for="a9302e00" id="64f3b190"
data-target="#7f12ddd1" aria-expanded="false" aria-controls="7f12ddd1">
<h5 class="accordion-item-header">Collapsible Group Item #3</h5>
<svg class="icon accordion-icon" xmlns="http://www.w3.org/2000/svg" role="img"
focusable="false">
<title>Expand</title>
<use xlink:href="../../.././_assets/images/icon__sprite.svg#arrow-down">
</use>
</svg>
</label>
<div id="7f12ddd1" class="accordion-collapse" aria-labelledby="64f3b190"
data-parent="#accordion2">Lorem ipsum</div>
</div>
</div>
Accessibility#
Be sure to add aria-expanded
to the label element. This attribute explicitly conveys the current state of the collapsible element tied to the control to screen readers and similar assistive technologies. If the collapsible element is closed by default, the attribute on the control element should have a value of aria-expanded="false"
. If you’ve set the collapsible element to be open by default set aria-expanded="true"
on the control instead. The plugin will automatically toggle this attribute on the control based on whether or not the collapsible element has been opened or closed (via Javascript).
If your control element is targeting a single collapsible element – i.e. the data-target
attribute is pointing to an id
selector – you should add the aria-controls
attribute to the control element, containing the id
of the collapsible element. Modern screen readers and similar assistive technologies make use of this attribute to provide users with additional shortcuts to navigate directly to the collapsible element itself.
JavaScript#
The accordion does work without javascript. But to ensure best accessibility we've created a small script to set the aria-expanded="true"
to it's correct state on user interaction.
You can import our javascript bundle cake.js
to automatically use this feature. You only must ensure that the property data-controller="accrodion/collapse"
is set on the input element of the accordion.
Initialization#
To initialize the javascript with default configuration you could simply run the following code:
document.addEventListener ('DOMContentLoaded', () => {
window.cake.accordion ();
});
Customization#
To customize the default behavior you can only embedd the accordion.js
file into your mockups. Then you can initialize the functionality by calling:
document.addEventListener ('DOMContentLoaded', () => {
window.cake.accordion (options = {
elements: [],
querySelector: '*[data-controller="accordion/input"]'
});
});
elements [Array]
- provide the specific input elements of your accordion (optional).
querySelector [String]
- provide a query-selector to select all accordion input elements in your DOM. (optional, default:
*[data-controller="accordion/input"]
).
- provide a query-selector to select all accordion input elements in your DOM. (optional, default:
If you do provide the options.elements
the options.querySelector
option gets ignored. If you do not provide any options.elements
always the options.querySelector
is used!