Vertically-stacked list that allows users to expand and collapse additional content
Section borders expand to full width of container.
Reduced spacing around title and content body. Also, smaller font sizes resulting in overall denser display of content.
Border aligns to the title text and expand/collapse icon.
Optionally remove content spacing (css padding) from the accordion content for applications needing more design flexibility.
In order to render a dynamic list of accordions, for example using data retrieved from a back-end API, you will need to use this.$set
or some other Vue method to make the array of accordion data reactive. This can also be used to avoid creating an individual data attribute for every accordion and instead track their state with an array of booleans.
The unwrap
property of CdrAccordionGroup
can be used to render the accordion content in an "unwrapped" state. This property accepts either a boolean toggle or a list of breakpoints.
To ensure that usage of this component complies with accessibility guidelines:
This component has compliance with WCAG guidelines by:
aria-controls
, aria-expanded
, and aria-hidden
id
name
string
type
N/A
default
Unique id required.
level
name
string, number
type
N/A
default
Set the heading that wraps the button to the appropriate level for the page. This aids in accesibility and navigaiton for keyboard users.
opened
name
boolean
type
false
default
Toggle to open/close the accordion.
compact
name
boolean
type
false
default
Sets the compact style.
borderAligned
name
boolean
type
false
default
Sets the border-aligned style.
contentSpacing
name
boolean
type
true
default
Sets the content spacing style
Find more information about using Slots in the article Installing Cedar.
label
name
Sets the readable text on the CdrAccordion button.
default
name
Slot for the CdrAccordion content.
accordion-toggle
name
event
arguments
$emit event fired on CdrAccordion toggle.
CdrAccordion emits an event when its button is clicked. Use an event listener to toggle the value of the opened prop to open or close the accordion.
<template>
<cdr-accordion
id="item"
level="3"
:compact="true"
:opened="opened"
@accordion-toggle="opened = !opened"
>
<template #label>
Click me to show content!
</template>
This content is revealed when the accordion is opened.
</cdr-accordion>
</template>
<script>
export default {
...
data() {
return {
opened: false
}
}
}
</script>
Accordion has a complementary wrapping component CdrAccordionGroup
which should be used when creating a group of accordions. CdrAccordionGroup
has no API and simply acts to enhance a11y and keyboard interactions for the group.
Creating groups can be useful if, for instance, you wanted to close the other accordions when one is opened.
<cdr-accordion-group>
<cdr-accordion
v-for="(item, index) in grouped"
:id="item.id"
level="2"
:border-aligned="true"
:opened="item.opened"
:key="item.id"
@accordion-toggle="updateGroup(index)"
>
<template #label>
{{ item.label }}
</template>
{{ item.content }}
</cdr-accordion>
</cdr-accordion-group>
<script>
export default {
...
data() {
return {
grouped: [
{
label: 'These are border-aligned',
content: 'These accordions will only allow one open at a time.',
opened: false,
id: 'linked1',
},
{
label: 'And they are also linked',
content: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ',
opened: false,
id: 'linked2',
},
{
label: 'To close others when one is opened',
content: 'These accordions will only allow one open at a time.',
opened: false,
id: 'linked3',
},
],
}
},
methods: {
updateGroup(index) {
const { opened } = this.grouped[index];
if (opened) {
// closing opened accordion
this.grouped[index].opened = false;
} else {
// open closed accordion. close all others.
for (let i = 0; i < this.grouped.length; i += 1) {
this.grouped[i].opened = index === i;
}
}
},
}
}
</script>