Simple Accordion With Vue JS

Simple Accordion With Vue JS

 

HTML:

<div id="app">
    <h1>Simple Accordion w/ Vue JS</h1>
    <div class="accordions">
        <dl v-for="(row, id) in rows">
            <dt v-on:click="row.open = !row.open">{{ row.term }}</dt>
            <dd v-if="row.open">{{ row.details }}</dd>
        </dl>
    </div>
</div>


JS:

new Vue({
    el: '#app',
    data: {
        rows: {
            q1: {
                term: 'Term 1',
                details: 'Some text here...',
                open: true
            },
            q2: {
                term: 'Term 2',
                details: 'Some text here...',
                open: false
            },
            q3: {
                term: 'Term 3',
                details: 'Some text here...',
                open: false
            }
        }
    }
})


 

CSS:

* {
    box-sizing: border-box;
}

body {
    margin: 0;
}

dl {
    margin: 0 0 0.1em;;
    
    dt {
        background-color: #ccc;
        padding: 1em;
        font-weight: bold;
    }
    
    dd {
        padding: 0;
        margin: 0;
        border: 1px solid #ccc;
        border-top: 0;
        padding: 1em;
    }
}

#app {
    width: 100vw;
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    padding: 16px;
}


.accordions {
    width: 100%;
    max-width: 500px;
}