Welcome, in this post we will learn you about how to create an custom accordion in LWC. The accordion will be animated, and we will have full control over it, The LWC default accordion is lack of the open one accordion at a time, So, I also, included that feature into it, and by customizing a little bit it will also be able to keep more than one accordion open.
Demo
For Demo how the actual Custom Accordion Section Will work, please click to see the demo
Table of Contents
First of all create a component named “customAccordion”. Now in this component there should be three files HTML, CSS, and JavaScript. Now place the below code in respective file.
HTML Code
Copy this HTML code and past it into your “customAccordion.html” file.
{item.title}
{item.description}
JavaScript Code
Copy this JavaScript code and past it into your “customAccordion.js” file.
import { LightningElement } from 'lwc';
export default class CustomAccordion extends LightningElement {
// Data For Accordion
customAccordionData = [
{
id: 1,
title: 'This is the Accordion One',
description: 'This is the content of the accordion one. This accordion is build with LWC(Lightening web components). The accordion is fully custom, and you can also enhance it features.',
},
{
id: 2,
title: 'This is the Accordion Two',
description: 'This is the content of the accordion two. This accordion is build with LWC(Lightening web components). The accordion is fully custom, and you can also enhance it features.',
},
{
id: 3,
title: 'This is the Accordion Three',
description: 'This is the content of the accordion three. This accordion is build with LWC(Lightening web components). The accordion is fully custom, and you can also enhance it features.',
},
{
id: 4,
title: 'This is the Accordion Four',
description: 'This is the content of the accordion four. This accordion is build with LWC(Lightening web components). The accordion is fully custom, and you can also enhance it features.',
}
]
// To Open the first Accordion open by default, we will use connected callback
connectedCallback() {
// Open the first accordion by default
const firstAccordionId = this.customAccordionData[0].id;
setTimeout(() => {
const firstSection = this.template.querySelector('[data-section-id="' + firstAccordionId + '"]');
const firstButton = this.template.querySelector('[data-buttonid="' + firstAccordionId + '"]');
if (firstSection && firstButton) {
firstSection.classList.add('slds-is-open');
firstSection.style.maxHeight = firstSection.scrollHeight + "px";
firstButton.classList.add('title-active');
firstButton.querySelector('i').classList.add('fa-angle-up');
firstButton.querySelector('i').classList.remove('fa-angle-down');
}
}, 0); // Ensure the DOM is fully loaded before applying the styles
}
// To toggle the Accordion Section
toggleAccordionSection(event) {
let buttonid = event.currentTarget.dataset.buttonid;
let currentSection = this.template.querySelector('[data-section-id="' + buttonid + '"]');
let currentButton = this.template.querySelector('[data-buttonid="' + buttonid + '"]');
// Check if currentButton is not null before applying styles
let allButtons = this.template.querySelectorAll('.custom-accordian_title');
if (!currentSection.classList.contains('slds-is-open')) {
let allSections = this.template.querySelectorAll('.custom-accordian_content');
allSections.forEach(section => {
section.classList.remove('slds-is-open');
section.style.maxHeight = '0px';
});
allButtons.forEach(section => {
section.classList.remove('title-active');
});
allButtons.forEach(button => {
if (button.dataset.buttonid == buttonid) {
button.querySelector('i').classList.add('fa-angle-up');
button.querySelector('i').classList.remove('fa-angle-down');
}
else {
button.querySelector('i').classList.remove('fa-angle-up');
button.querySelector('i').classList.add('fa-angle-down');
}
});
currentSection.classList.add('slds-is-open');
currentSection.style.maxHeight = currentSection.scrollHeight + "px";
currentButton.classList.add('title-active');
} else {
currentSection.classList.remove('slds-is-open');
currentButton.classList.remove('title-active');
currentSection.style.maxHeight = '0px';
allButtons.forEach(button => {
if (button.dataset.buttonid == buttonid) {
button.querySelector('i').classList.remove('fa-angle-up');
button.querySelector('i').classList.add('fa-angle-down');
}
});
}
}
}
CSS Code
Copy this CSS code and past it into your “customAccordion.css” file.
Please make sure that your “customAccordion.js-meta.xml” is exposed and have availability access to the respective enviornement where you are using the your compoennt. for reference you can check below code :
Create a Custom Accordion in LWC
Welcome, in this post we will learn you about how to create an custom accordion in LWC. The accordion will be animated, and we will have full control over it, The LWC default accordion is lack of the open one accordion at a time, So, I also, included that feature into it, and by customizing a little bit it will also be able to keep more than one accordion open.
Demo
For Demo how the actual Custom Accordion Section Will work, please click to see the demo
Table of Contents
First of all create a component named “customAccordion”. Now in this component there should be three files HTML, CSS, and JavaScript.
Now place the below code in respective file.
HTML Code
Copy this HTML code and past it into your “customAccordion.html” file.
JavaScript Code
Copy this JavaScript code and past it into your “customAccordion.js” file.
CSS Code
Copy this CSS code and past it into your “customAccordion.css” file.
customAccordion.js-meta.xml file
Please make sure that your “customAccordion.js-meta.xml” is exposed and have availability access to the respective enviornement where you are using the your compoennt. for reference you can check below code :
Conclusion
That’s it now your custom accordion will work like a magic. please check and leave a feedback in comment section. Thanks. Have fun.
Recent Post
Create a Custom Accordion in LWC
September 2, 2024Create circular progress bar in LWC
September 2, 2024Create a autoplay banner carousel in LWC
July 15, 2024Categories