Accordion
Expandable/collapsible content sections.
Live Demo
▲▼
Source
HTML + JavaScript
<div data-component="my-accordion">
<div data-list="items">
<template>
<div class="accordion-item">
<div class="accordion-header" data-action="toggle">
<span data-bind="title"></span>
<span data-show="open">▲</span><span data-show="!open">▼</span>
</div>
<div class="accordion-body" data-show="open">
<span data-bind="content"></span>
</div>
</div>
</template>
</div>
</div>
<script>
wildflower.component('my-accordion', {
state: {
items: [
{ title: 'Section One', content: 'Content for section one.', open: true },
{ title: 'Section Two', content: 'Content for section two.', open: false },
{ title: 'Section Three', content: 'Content for section three.', open: false }
]
},
toggle(e, el, { index }) {
this.items[index].open = !this.items[index].open;
}
});
</script>
Key Points
data-listrenders each section from the items array- Each item's
openproperty controls its own visibility viadata-show - The action method receives
(event, element, { index }); use the index to access the item - Nested property mutation (
this.items[index].open) is fully reactive