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 #35 from hlxsites/feature/tabs
Added the tabs functionality
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.mmg-tabs { | ||
padding: 20px 0; | ||
} | ||
|
||
.tabpanel table tr:first-child td { | ||
background-color: var(--primary-color)!important; /* Specify your desired background color here */ | ||
color: var(--text-color); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
function removeActiveClasses(content) { | ||
const contentElements = content.querySelectorAll('.tabpanel'); | ||
[...contentElements].forEach((element) => { | ||
element.classList.remove('active'); | ||
}); | ||
const listElements = content.querySelectorAll('button'); | ||
[...listElements].forEach((element) => { | ||
element.classList.remove('active'); | ||
}); | ||
} | ||
|
||
function activeFirstElements(content) { | ||
const contentElement = content.querySelector('.tabpanel'); | ||
contentElement.classList.add('active'); | ||
const listElement = content.querySelector('button'); | ||
listElement.classList.add('active'); | ||
} | ||
|
||
export default function decorate(block) { | ||
const tabComponent = document.createElement('div'); | ||
tabComponent.className = 'mmg-tabs'; | ||
tabComponent.classList.add('outer'); | ||
const ul = document.createElement('div'); | ||
ul.className = 'tablist'; | ||
const tabContent = document.createElement('div'); | ||
tabContent.className = 'tabpanels'; | ||
|
||
// Iterate through block's children and create tabs | ||
[...block.children].forEach((row) => { | ||
const itemContent = row.children[1]; | ||
itemContent.className = 'tabpanel'; | ||
const li = document.createElement('button'); | ||
li.className = 'tab'; | ||
li.appendChild(row.children[0]); | ||
|
||
li.addEventListener('click', () => { | ||
removeActiveClasses(tabComponent); | ||
li.classList.add('active'); | ||
itemContent.classList.add('active'); | ||
}); | ||
ul.appendChild(li); | ||
tabContent.appendChild(itemContent); | ||
}); | ||
|
||
// Set the first tab as active by default | ||
block.textContent = ''; | ||
tabComponent.appendChild(ul); | ||
tabComponent.appendChild(tabContent); | ||
block.appendChild(tabComponent); | ||
activeFirstElements(tabComponent); | ||
} |