generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from hlxsites/accordion
Accordion
- Loading branch information
Showing
2 changed files
with
72 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,66 @@ | ||
export default function decorate(block) { | ||
const divCta = document.querySelector('div .cta'); | ||
if (divCta) { | ||
const expandCollapse = document.createElement('div'); | ||
expandCollapse.classList.add('expand_collapse'); | ||
const expandBtn = document.createElement('a'); | ||
expandBtn.classList.add('expand-btn'); | ||
expandBtn.setAttribute('href', '#'); | ||
expandBtn.textContent = 'Expand All'; | ||
const collapseBtn = document.createElement('a'); | ||
collapseBtn.classList.add('collapse-btn'); | ||
collapseBtn.setAttribute('href', '#'); | ||
collapseBtn.textContent = 'Collapse All'; | ||
expandCollapse.appendChild(expandBtn); | ||
expandCollapse.appendChild(collapseBtn); | ||
const parent = block.parentNode; | ||
parent.prepend(expandCollapse); | ||
// event listeners for expand, collapse buttons | ||
expandCollapse.addEventListener('click', (event) => { | ||
if (event.target.classList.contains('expand-btn')) { | ||
document.querySelector('.expand_collapse').classList.add('expanded'); | ||
document.querySelector('.collapse-btn').style.display = 'inline-block'; | ||
const allQuestions = document.querySelectorAll('.faq-question'); | ||
allQuestions.forEach((ele) => { | ||
ele.classList.add('active'); | ||
ele.nextElementSibling.classList.add('active'); | ||
ele.nextElementSibling.style.maxHeight = `${ele.nextElementSibling.scrollHeight}px`; | ||
}); | ||
} else if (event.target.classList.contains('collapse-btn')) { | ||
document.querySelector('.expand_collapse').classList.remove('expanded'); | ||
document.querySelector('.collapse-btn').style.display = 'none'; | ||
const allQuestions = document.querySelectorAll('.faq-question'); | ||
allQuestions.forEach((ele) => { | ||
ele.classList.remove('active'); | ||
ele.nextElementSibling.classList.remove('active'); | ||
ele.nextElementSibling.style.removeProperty('max-height'); | ||
}); | ||
} | ||
}); | ||
} | ||
const faqRows = [...block.children]; | ||
block.classList.add('faq-accordion'); | ||
|
||
faqRows.forEach((row) => { | ||
const faqQuestion = [...row.children][0]; | ||
const faqAnswer = [...row.children][1]; | ||
faqQuestion.classList.add('faq-question'); | ||
faqAnswer.classList.add('faq-answer'); | ||
|
||
faqQuestion.addEventListener('click', () => { | ||
const isActive = faqQuestion.classList.contains('active'); | ||
|
||
if (isActive) { | ||
faqQuestion.classList.remove('active'); | ||
faqAnswer.classList.remove('active'); | ||
// Set max-height to 0 for smooth closing | ||
faqAnswer.style.maxHeight = '0'; | ||
faqQuestion.addEventListener('click', (e) => { | ||
const currentFaq = e.currentTarget.classList.contains('active'); | ||
const openfaq = block.querySelector('.faq-question.active'); | ||
if (openfaq && !currentFaq) { | ||
openfaq.classList.toggle('active'); | ||
openfaq.nextElementSibling.classList.toggle('active'); | ||
} | ||
faqQuestion.nextElementSibling.style.removeProperty('max-height'); | ||
e.currentTarget.classList.toggle('active'); | ||
e.currentTarget.nextElementSibling.classList.toggle('active'); | ||
const faqAnswer = e.currentTarget.nextElementSibling; | ||
if (faqAnswer.style.maxHeight) { | ||
faqAnswer.style.removeProperty('max-height'); | ||
} else { | ||
faqQuestion.classList.add('active'); | ||
faqAnswer.classList.add('active'); | ||
// Set max-height to scrollHeight for smooth opening | ||
faqAnswer.style.maxHeight = `${faqAnswer.scrollHeight}px`; | ||
} | ||
}); | ||
|
||
// Close all accordions initially | ||
faqQuestion.classList.remove('active'); | ||
faqAnswer.classList.remove('active'); | ||
faqAnswer.style.maxHeight = '0'; | ||
const faqAnswer = [...row.children][1]; | ||
faqAnswer.classList.add('faq-answer'); | ||
}); | ||
} |