Last active
April 17, 2023 03:27
-
-
Save ImKubass/9df6e3f2e167a47a26780ae5a26101ea to your computer and use it in GitHub Desktop.
Abstract tabs handle
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
const tabsGroup = document.querySelectorAll(".js-tabs") | |
tabsGroup.forEach(tabGroup => { | |
const tabItems = tabGroup.querySelectorAll(".js-tabs__tab-item") | |
const panels = tabGroup.querySelectorAll(".js-tabs__panel") | |
const closeAllPanels = () => panels.forEach(content => content.classList.remove("--active")) | |
const resetAllTabItems = () => tabItems.forEach(link => link.classList.remove("--active")) | |
tabItems.forEach(tabItem => { | |
tabItem.addEventListener("click", (event) => { | |
event.preventDefault() | |
if (tabItem.classList.contains("--active")) return | |
const targetId = tabItem.getAttribute("data-tab-target") ?? "" | |
const targetPanel = tabGroup.querySelector(`[data-panel-id="${targetId}"]`) | |
if (targetPanel === null) return | |
closeAllPanels() | |
resetAllTabItems() | |
targetPanel.classList.add("--active") | |
tabItem.classList.add("--active") | |
}) | |
}) | |
}) |
Author
ImKubass
commented
Apr 17, 2023
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment