Skip to content

Commit

Permalink
Support multiple tabs
Browse files Browse the repository at this point in the history
Also allows for nested tabs, cleans up some JS a bit too
  • Loading branch information
khawkins98 committed Dec 26, 2018
1 parent 1facc10 commit 521d14c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 31 deletions.
28 changes: 25 additions & 3 deletions components/vf-tabs/vf-tabs.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
<a class="vf-tabs__link" href="#vf-tabs__section--3">A Rather Long Section Sentence</a>
</li>
<li class="vf-tabs__item">
<a class="vf-tabs__link" href="#vf-tabs__section--4">Section 4</a>
<a class="vf-tabs__link" href="#vf-tabs__section--4">Nested tabs</a>
</li>
</ul>

</div>

<div class="vf-tabs-content">
<section class="vf-tabs__section" id="vf-tabs__section--1">
<h2>Section 1</h2>
Expand All @@ -29,7 +29,29 @@
<p>Phasellus ac tristique orci. Nulla maximus <a href="">justo nec dignissim consequat</a>. Sed vehicula diam sit amet mi efficitur vehicula in in nisl. Aliquam erat volutpat. Suspendisse lorem turpis, accumsan consequat consectetur gravida, <a href="#">pellentesque ac ante</a>. Aliquam in commodo ligula, sit amet mollis neque. Vestibulum at facilisis massa.</p>
</section>
<section class="vf-tabs__section" id="vf-tabs__section--4">
<h2>Section 4</h2>
<h2>Nested tabs</h2>
<p>Nam luctus, enim in interdum condimentum, nisl diam iaculis lorem, vel volutpat mi leo sit amet lectus. Praesent non odio bibendum magna bibendum accumsan. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod, tortor nec pharetra ultricies, ante erat imperdiet velit, nec laoreet enim lacus a velit. </p>

<div class="vf-tabs">
<ul class="vf-tabs__list">
<li class="vf-tabs__item">
<a class="vf-tabs__link" href="#vf-tabs__section-nested--1">Nested tab 1</a>
</li>
<li class="vf-tabs__item">
<a class="vf-tabs__link" href="#vf-tabs__section-nested--2">Nested tab 2</a>
</li>
</ul>
</div>

<div class="vf-tabs-content">
<section class="vf-tabs__section" id="vf-tabs__section-nested--1">
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam euismod, tortor nec pharetra ultricies, ante erat imperdiet velit, nec laoreet enim lacus a velit. <a href="#">Nam luctus</a>, enim in interdum condimentum, nisl diam iaculis lorem, vel volutpat mi leo sit amet lectus. Praesent non odio bibendum magna bibendum accumsan.</p>
</section>
<section class="vf-tabs__section" id="vf-tabs__section-nested--2">
<p>Nullam at diam nec arcu suscipit auctor non a erat. Sed et magna semper, eleifend magna non, facilisis nisl. Proin et est et lorem dictum finibus ut nec turpis. Aenean nisi tortor, euismod a mauris a, mattis scelerisque tortor. Sed dolor risus, varius a nibh id, condimentum lacinia est. In lacinia cursus odio a aliquam. Curabitur tortor magna, laoreet ut rhoncus at, sodales consequat tellus.</p>
</section>
</div>


</section>
</div>
68 changes: 40 additions & 28 deletions components/vf-tabs/vf-tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@
*/
function vfTabs() {
// Get relevant elements and collections
const tabbed = document.querySelector('.vf-tabs');
const tabbedContent = document.querySelector('.vf-tabs-content');
if (!tabbed || !tabbedContent) {
// todo: `document` here should be a scopped passed param like #mydiv or .my-div
const tablist = document.querySelectorAll('.vf-tabs__list');
const tabsSection = "vf-tabs__section--";
const panelsList = document.querySelectorAll('.vf-tabs-content');
const panels = document.querySelectorAll('[id^="vf-tabs__section"]');
const tabs = document.querySelectorAll('.vf-tabs__link');
if (!tablist || !panels || !tabs) {
// exit: either tabs or tabbed content not found
return;
}
const tablist = tabbed.querySelector('.vf-tabs__list');
const tabs = tablist.querySelectorAll('.vf-tabs__link');
const panels = tabbedContent.querySelectorAll('[id^="vf-tabs__section"]');
const tabsSection = "vf-tabs__section--";

// The tab switching function
const switchTab = (oldTab, newTab) => {
const switchTab = (newTab) => {

// get the parent ul of the clicked tab
let parentTabSet = newTab.closest(".vf-tabs__list");
let oldTab = parentTabSet.querySelector('[aria-selected]');
if (oldTab) {
oldTab.removeAttribute('aria-selected');
oldTab.setAttribute('tabindex', '-1');
oldTab.classList.remove('is-active');
let oldIndex = Array.prototype.indexOf.call(tabs, oldTab);
panels[oldIndex].hidden = true;
}

newTab.focus();
// Make the active tab focusable by the user (Tab key)
newTab.removeAttribute('tabindex');
// Set the selected state
newTab.setAttribute('aria-selected', 'true');
newTab.classList.add('is-active');
oldTab.removeAttribute('aria-selected');
oldTab.setAttribute('tabindex', '-1');
oldTab.classList.remove('is-active');
// Get the indices of the new and old tabs to find the correct
// tab panels to show and hide
// Get the indices of the new tab to find the correct
// tab panel to show
let index = Array.prototype.indexOf.call(tabs, newTab);
let oldIndex = Array.prototype.indexOf.call(tabs, oldTab);
panels[oldIndex].hidden = true;
panels[index].hidden = false;
}

// Add the tablist role to the first <ul> in the .tabbed container
tablist.setAttribute('role', 'tablist');

// Add semantics are remove user focusability for each tab
Array.prototype.forEach.call(tabs, (tab, i) => {
tab.setAttribute('role', 'tab');
Expand All @@ -53,10 +57,7 @@ function vfTabs() {
// Handle clicking of tabs for mouse users
tab.addEventListener('click', e => {
e.preventDefault();
let currentTab = tablist.querySelector('[aria-selected]');
if (e.currentTarget !== currentTab) {
switchTab(currentTab, e.currentTarget);
}
switchTab(e.currentTarget);
});

// Handle keydown events for keyboard users
Expand All @@ -70,7 +71,7 @@ function vfTabs() {
e.preventDefault();
// If the down key is pressed, move focus to the open panel,
// otherwise switch to the adjacent tab
dir === 'down' ? panels[i].focus() : tabs[dir] ? switchTab(e.currentTarget, tabs[dir]) : void 0;
dir === 'down' ? panels[i].focus() : tabs[dir] ? switchTab(tabs[dir]) : void 0;
}
});
});
Expand All @@ -84,11 +85,22 @@ function vfTabs() {
panel.hidden = true;
});

// Initially activate the first tab and reveal the first tab panel
tabs[0].removeAttribute('tabindex');
tabs[0].setAttribute('aria-selected', 'true');
tabs[0].classList.add('is-active');
panels[0].hidden = false;
// Add the tablist role to the first <ul> in the .tabbed container
Array.prototype.forEach.call(tablist, (tablistset, i) => {
tablistset.setAttribute('role', 'tablist');
// Initially activate the first tab
let firstTab = tablistset.querySelectorAll('.vf-tabs__link')[0];
firstTab.removeAttribute('tabindex');
firstTab.setAttribute('aria-selected', 'true');
firstTab.classList.add('is-active');
});
Array.prototype.forEach.call(panelsList, (panel, i) => {
// Initially reveal the first tab panel
let firstPanel = panel.querySelectorAll('.vf-tabs__section')[0];
firstPanel.hidden = false;
});

}


vfTabs();

0 comments on commit 521d14c

Please sign in to comment.