create accordion in shopify liquid sections
- User
create accordion in shopify liquid sections
- Assistant
create accordion in shopify liquid sections
Creating an accordion in Shopify using Liquid sections involves a combination of Liquid code, HTML, and some CSS/JavaScript for the toggle functionality. Below is a step-by-step guide to creating a simple accordion in a Shopify section.
accordion.Add the following code to your newly created accordion.liquid file:
{% assign accordion_id = section.id %}
<div class="accordion" id="accordion-{{ accordion_id }}">
{% for block in section.blocks %}
<div class="accordion-item">
<h2 class="accordion-header" id="heading{{ forloop.index }}">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapse{{ forloop.index }}" aria-expanded="false" aria-controls="collapse{{ forloop.index }}">
{{ block.settings.title }}
</button>
</h2>
<div id="collapse{{ forloop.index }}" class="accordion-collapse collapse" aria-labelledby="heading{{ forloop.index }}" data-bs-parent="#accordion-{{ accordion_id }}">
<div class="accordion-body">
{{ block.settings.content }}
</div>
</div>
</div>
{% endfor %}
</div>
{% schema %}
{
"name": "Accordion",
"settings": [],
"blocks": [
{
"type": "content",
"name": "Accordion Item",
"settings": [
{
"type": "text",
"id": "title",
"label": "Title"
},
{
"type": "textarea",
"id": "content",
"label": "Content"
}
]
}
],
"presets": [
{
"name": "Accordion",
"category": "Custom"
}
]
}
{% endschema %}
{% stylesheet %}
<style>
.accordion-button {
background-color: #f8f9fa;
color: #333;
font-weight: bold;
border: none;
text-align: left;
width: 100%;
padding: 0.75rem 1.25rem;
cursor: pointer;
transition: background-color 0.2s ease;
}
.accordion-button:hover,
.accordion-button:focus {
background-color: #e2e6ea;
}
.accordion-body {
border-top: 1px solid #ddd;
padding: 1rem;
}
</style>
{% endstylesheet %}
For this accordion to function correctly, you might need some JavaScript if you're not using a framework like Bootstrap which already supports accordion behavior. Below is an example of how you might handle this:
If you prefer to write your own custom JavaScript:
<script>
document.querySelectorAll('.accordion-button').forEach(button => {
button.addEventListener('click', () => {
const accordionCollapse = button.nextElementSibling;
button.classList.toggle('active');
if (button.classList.contains('active')) {
accordionCollapse.style.maxHeight = accordionCollapse.scrollHeight + 'px';
} else {
accordionCollapse.style.maxHeight = 0;
}
});
});
</script>
Now, you can customize your accordion using the theme editor by adding blocks for each accordion item. Each block will have a title and content field that you can fill with your desired data.
By following these steps, you should have a fully functional accordion section within your Shopify store. Feel free to adjust the styling and JavaScript as needed to fit your store's design.
Not yet included in a published release.