Skip to content

Commit a174f59

Browse files
committed
provide dynamic tabindexes for reveal tabs
1 parent da84ef9 commit a174f59

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

  • src/resources/formats/html/tabby/js

‎src/resources/formats/html/tabby/js/tabby.js‎

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@
7474
toggle(event.target);
7575
};
7676

77+
var getKeyboardFocusableElements = function (element) {
78+
return [
79+
...element.querySelectorAll(
80+
'a[href], button, input, textarea, select, details,[tabindex]:not([tabindex="-1"])'
81+
),
82+
].filter(
83+
(el) => !el.hasAttribute("disabled") && !el.getAttribute("aria-hidden")
84+
);
85+
};
86+
7787
/**
7888
* Remove roles and attributes from a tab and its content
7989
* @param {Node} tab The tab
@@ -93,6 +103,7 @@
93103
tab.removeAttribute("role");
94104
tab.removeAttribute("aria-controls");
95105
tab.removeAttribute("aria-selected");
106+
tab.removeAttribute("tabindex");
96107
tab.closest("li").removeAttribute("role");
97108
content.removeAttribute("role");
98109
content.removeAttribute("aria-labelledby");
@@ -114,7 +125,6 @@
114125
// Add roles
115126
tab.setAttribute("role", "tab");
116127
tab.setAttribute("aria-controls", content.id);
117-
tab.setAttribute("tabindex", "0");
118128
tab.closest("li").setAttribute("role", "presentation");
119129
content.setAttribute("role", "tabpanel");
120130
content.setAttribute("aria-labelledby", tab.id);
@@ -302,12 +312,28 @@
302312
// Add role to wrapper
303313
tabWrapper.setAttribute("role", "tablist");
304314

305-
// Add roles to tabs
315+
// Add roles to tabs. provide dynanmic tab indexes if we are within reveal
316+
var contentTabindexes =
317+
window.document.body.classList.contains("reveal-viewport");
318+
var nextTabindex = 1;
306319
Array.prototype.forEach.call(tabs, function (tab) {
320+
if (contentTabindexes) {
321+
tab.setAttribute("tabindex", "" + nextTabindex++);
322+
} else {
323+
tab.setAttribute("tabindex", "0");
324+
}
325+
307326
// Get the tab content
308327
var content = document.querySelector(tab.hash);
309328
if (!content) return;
310329

330+
// set tab indexes for content
331+
if (contentTabindexes) {
332+
getKeyboardFocusableElements(content).forEach(function (el) {
333+
el.setAttribute("tabindex", "" + nextTabindex++);
334+
});
335+
}
336+
311337
// Setup the tab
312338
setupTab(tab, content, settings);
313339
});

0 commit comments

Comments
 (0)