Last active
March 2, 2023 15:27
-
-
Save LordZombi/613854d1e8a50412dd82dccf1ec51667 to your computer and use it in GitHub Desktop.
Simple Vanilla.js tabs implementation
This file contains hidden or 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
.tabs__header-tab.is-active { | |
// Some active styles for header | |
} | |
.tabs__content { | |
display: none; // Hide tab content | |
} | |
.tabs__content.is-active { | |
display: block; // And show only active | |
} |
This file contains hidden or 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
<div | |
data-tabs | |
class="tabs" | |
> | |
<div class="tabs__header"> | |
<div | |
data-tab="first" | |
data-tab-active | |
class="tabs__header-tab" | |
> | |
First | |
</div> | |
<div | |
data-tab="second" | |
class="tabs__header-tab" | |
> | |
Second | |
</div> | |
<div | |
data-tab="third" | |
class="tabs__header-tab" | |
> | |
Third | |
</div> | |
</div> | |
<div | |
data-tab-content="first" | |
class="tabs__content" | |
> | |
<h2>First</h2> | |
<p>First tab content</p> | |
</div> | |
<div | |
data-tab-content="second" | |
class="tabs__content" | |
> | |
<h2>Second</h2> | |
<p>Second tab content</p> | |
</div> | |
<div | |
data-tab-content="third" | |
class="tabs__content" | |
> | |
<h2>Third</h2> | |
<p>Third tab content</p> | |
</div> | |
</div> |
This file contains hidden or 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
function initTabs() { | |
const tabGroups = document.querySelectorAll('[data-tabs]'); | |
tabGroups.forEach(tabGroup => { | |
const headers = tabGroup.querySelectorAll('[data-tab]'); | |
const contents = tabGroup.querySelectorAll('[data-tab-content]'); | |
contents.forEach(content => { | |
content.classList.remove('is-active'); | |
}); | |
headers.forEach(header => { | |
header.classList.remove('is-active'); | |
header.addEventListener('click', onTabClick.bind(header)); | |
}); | |
function onTabClick() { | |
setActiveTab(this); | |
} | |
function setActiveTab(tab) { | |
if (!tab) return; | |
headers.forEach(header => { | |
header.classList.remove('is-active'); | |
}); | |
contents.forEach(content => { | |
content.classList.remove('is-active'); | |
}); | |
const name = tab.dataset.tab; | |
const tabContent = tabGroup.querySelector(`[data-tab-content=${name}]`); | |
if (!tabContent) return; | |
tab.classList.add('is-active'); | |
tabContent.classList.add('is-active'); | |
} | |
const activeTab = tabGroup.querySelector('[data-tab-active]'); | |
if (activeTab) { | |
setActiveTab(activeTab); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment