2021-03-31 15:07:28 +00:00
|
|
|
|
/* exported TabGroup */
|
|
|
|
|
|
2021-03-19 17:47:32 +00:00
|
|
|
|
/*
|
2022-02-25 20:52:35 +00:00
|
|
|
|
* The content below is licensed according to the W3C Software License at
|
|
|
|
|
* https://www.w3.org/Consortium/Legal/2015/copyright-software-and-document
|
|
|
|
|
* Heavily modified to web component by Zach Leatherman
|
|
|
|
|
* Modified back to vanilla JavaScript with support for Bulma markup and nested tabs by Ned Zimmerman
|
|
|
|
|
*/
|
2021-03-19 17:47:32 +00:00
|
|
|
|
class TabGroup {
|
|
|
|
|
constructor(container) {
|
|
|
|
|
this.container = container;
|
|
|
|
|
|
|
|
|
|
this.tablist = this.container.querySelector('[role="tablist"]');
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.tabs = this.tablist.querySelectorAll('[role="tab"]');
|
2021-03-19 17:47:32 +00:00
|
|
|
|
this.panels = this.container.querySelectorAll(':scope > [role="tabpanel"]');
|
|
|
|
|
this.delay = this.determineDelay();
|
|
|
|
|
|
2022-02-25 20:52:35 +00:00
|
|
|
|
if (!this.tablist || !this.tabs.length || !this.panels.length) {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.keys = this.keys();
|
|
|
|
|
this.direction = this.direction();
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.initTabs();
|
2021-03-19 17:47:32 +00:00
|
|
|
|
this.initPanels();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keys() {
|
|
|
|
|
return {
|
|
|
|
|
end: 35,
|
|
|
|
|
home: 36,
|
|
|
|
|
left: 37,
|
|
|
|
|
up: 38,
|
|
|
|
|
right: 39,
|
2022-02-25 20:52:35 +00:00
|
|
|
|
down: 40,
|
2021-03-19 17:47:32 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add or substract depending on key pressed
|
|
|
|
|
direction() {
|
|
|
|
|
return {
|
|
|
|
|
37: -1,
|
|
|
|
|
38: -1,
|
|
|
|
|
39: 1,
|
2022-02-25 20:52:35 +00:00
|
|
|
|
40: 1,
|
2021-03-19 17:47:32 +00:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-12 15:14:35 +00:00
|
|
|
|
initTabs() {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
let count = 0;
|
2022-02-25 20:59:49 +00:00
|
|
|
|
|
2022-02-25 20:52:35 +00:00
|
|
|
|
for (let tab of this.tabs) {
|
2022-02-12 15:14:35 +00:00
|
|
|
|
let isSelected = tab.getAttribute("aria-selected") === "true";
|
2022-02-25 20:59:49 +00:00
|
|
|
|
|
2022-02-12 15:14:35 +00:00
|
|
|
|
tab.setAttribute("tabindex", isSelected ? "0" : "-1");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
2022-02-25 20:52:35 +00:00
|
|
|
|
tab.addEventListener("click", this.clickEventListener.bind(this));
|
|
|
|
|
tab.addEventListener("keydown", this.keydownEventListener.bind(this));
|
|
|
|
|
tab.addEventListener("keyup", this.keyupEventListener.bind(this));
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
2022-02-12 15:14:35 +00:00
|
|
|
|
tab.index = count++;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initPanels() {
|
2021-03-20 15:33:18 +00:00
|
|
|
|
let selectedPanelId = this.tablist
|
|
|
|
|
.querySelector('[role="tab"][aria-selected="true"]')
|
|
|
|
|
.getAttribute("aria-controls");
|
2022-02-25 20:59:49 +00:00
|
|
|
|
|
2022-02-25 20:52:35 +00:00
|
|
|
|
for (let panel of this.panels) {
|
|
|
|
|
if (panel.getAttribute("id") !== selectedPanelId) {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
panel.setAttribute("hidden", "");
|
|
|
|
|
}
|
|
|
|
|
panel.setAttribute("tabindex", "0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clickEventListener(event) {
|
2022-02-12 15:14:35 +00:00
|
|
|
|
let tab = event.target.closest('[role="tab"]');
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.activateTab(tab, false);
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle keydown on tabs
|
|
|
|
|
keydownEventListener(event) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const key = event.keyCode;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
|
case this.keys.end:
|
|
|
|
|
event.preventDefault();
|
2022-02-25 20:59:49 +00:00
|
|
|
|
|
2021-03-19 17:47:32 +00:00
|
|
|
|
// Activate last tab
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.activateTab(this.tabs[this.tabs.length - 1]);
|
2021-03-19 17:47:32 +00:00
|
|
|
|
break;
|
|
|
|
|
case this.keys.home:
|
|
|
|
|
event.preventDefault();
|
2022-02-25 20:59:49 +00:00
|
|
|
|
|
2021-03-19 17:47:32 +00:00
|
|
|
|
// Activate first tab
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.activateTab(this.tabs[0]);
|
2021-03-19 17:47:32 +00:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
// Up and down are in keydown
|
|
|
|
|
// because we need to prevent page scroll >:)
|
|
|
|
|
case this.keys.up:
|
|
|
|
|
case this.keys.down:
|
|
|
|
|
this.determineOrientation(event);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle keyup on tabs
|
|
|
|
|
keyupEventListener(event) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const key = event.keyCode;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
|
case this.keys.left:
|
|
|
|
|
case this.keys.right:
|
|
|
|
|
this.determineOrientation(event);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// When a tablist’s aria-orientation is set to vertical,
|
|
|
|
|
// only up and down arrow should function.
|
|
|
|
|
// In all other cases only left and right arrow function.
|
|
|
|
|
determineOrientation(event) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const key = event.keyCode;
|
|
|
|
|
const vertical = this.tablist.getAttribute("aria-orientation") == "vertical";
|
|
|
|
|
let proceed = false;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
if (vertical) {
|
|
|
|
|
if (key === this.keys.up || key === this.keys.down) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
proceed = true;
|
|
|
|
|
}
|
2022-02-25 20:52:35 +00:00
|
|
|
|
} else {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
if (key === this.keys.left || key === this.keys.right) {
|
|
|
|
|
proceed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (proceed) {
|
|
|
|
|
this.switchTabOnArrowPress(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Either focus the next, previous, first, or last tab
|
|
|
|
|
// depending on key pressed
|
|
|
|
|
switchTabOnArrowPress(event) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const pressed = event.keyCode;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
2022-02-12 15:14:35 +00:00
|
|
|
|
for (let tab of this.tabs) {
|
2022-02-25 20:52:35 +00:00
|
|
|
|
tab.addEventListener("focus", this.focusEventHandler.bind(this));
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.direction[pressed]) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const target = event.target;
|
|
|
|
|
|
2021-03-19 17:47:32 +00:00
|
|
|
|
if (target.index !== undefined) {
|
2022-02-12 15:14:35 +00:00
|
|
|
|
if (this.tabs[target.index + this.direction[pressed]]) {
|
|
|
|
|
this.tabs[target.index + this.direction[pressed]].focus();
|
2022-02-25 20:52:35 +00:00
|
|
|
|
} else if (pressed === this.keys.left || pressed === this.keys.up) {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
this.focusLastTab();
|
2022-02-25 20:52:35 +00:00
|
|
|
|
} else if (pressed === this.keys.right || pressed == this.keys.down) {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
this.focusFirstTab();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Activates any given tab panel
|
2022-02-25 20:52:35 +00:00
|
|
|
|
activateTab(tab, setFocus) {
|
|
|
|
|
if (tab.getAttribute("role") !== "tab") {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
tab = tab.closest('[role="tab"]');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setFocus = setFocus || true;
|
|
|
|
|
|
|
|
|
|
// Deactivate all other tabs
|
|
|
|
|
this.deactivateTabs();
|
|
|
|
|
|
|
|
|
|
// Remove tabindex attribute
|
2022-02-25 20:52:35 +00:00
|
|
|
|
tab.removeAttribute("tabindex");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
// Set the tab as selected
|
2022-02-25 20:52:35 +00:00
|
|
|
|
tab.setAttribute("aria-selected", "true");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
2022-02-12 15:14:35 +00:00
|
|
|
|
// Give the tab is-active class
|
2022-02-25 20:52:35 +00:00
|
|
|
|
tab.classList.add("is-active");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
// Get the value of aria-controls (which is an ID)
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const controls = tab.getAttribute("aria-controls");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
// Remove hidden attribute from tab panel to make it visible
|
2022-02-25 20:52:35 +00:00
|
|
|
|
document.getElementById(controls).removeAttribute("hidden");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
// Set focus when required
|
|
|
|
|
if (setFocus) {
|
|
|
|
|
tab.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Deactivate all tabs and tab panels
|
|
|
|
|
deactivateTabs() {
|
2022-02-12 15:14:35 +00:00
|
|
|
|
for (let tab of this.tabs) {
|
2022-02-25 20:52:35 +00:00
|
|
|
|
tab.classList.remove("is-active");
|
|
|
|
|
tab.setAttribute("tabindex", "-1");
|
|
|
|
|
tab.setAttribute("aria-selected", "false");
|
|
|
|
|
tab.removeEventListener("focus", this.focusEventHandler.bind(this));
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let panel of this.panels) {
|
2022-02-25 20:52:35 +00:00
|
|
|
|
panel.setAttribute("hidden", "hidden");
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focusFirstTab() {
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.tabs[0].focus();
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focusLastTab() {
|
2022-02-12 15:14:35 +00:00
|
|
|
|
this.tabs[this.tabs.length - 1].focus();
|
2021-03-19 17:47:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine whether there should be a delay
|
|
|
|
|
// when user navigates with the arrow keys
|
|
|
|
|
determineDelay() {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const hasDelay = this.tablist.hasAttribute("data-delay");
|
|
|
|
|
let delay = 0;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
if (hasDelay) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const delayValue = this.tablist.getAttribute("data-delay");
|
|
|
|
|
|
2021-03-19 17:47:32 +00:00
|
|
|
|
if (delayValue) {
|
|
|
|
|
delay = delayValue;
|
2022-02-25 20:52:35 +00:00
|
|
|
|
} else {
|
2021-03-19 17:47:32 +00:00
|
|
|
|
// If no value is specified, default to 300ms
|
|
|
|
|
delay = 300;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return delay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
focusEventHandler(event) {
|
2022-02-25 20:59:49 +00:00
|
|
|
|
const target = event.target;
|
2021-03-19 17:47:32 +00:00
|
|
|
|
|
|
|
|
|
setTimeout(this.checkTabFocus.bind(this), this.delay, target);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Only activate tab on focus if it still has focus after the delay
|
|
|
|
|
checkTabFocus(target) {
|
|
|
|
|
let focused = document.activeElement;
|
|
|
|
|
|
|
|
|
|
if (target === focused) {
|
|
|
|
|
this.activateTab(target, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|