Skip to content

Commit de990b3

Browse files
committed
Mainframe: Added accessibility to tabs
1 parent 42dca5b commit de990b3

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

assets/css/v2/style.css

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,12 @@ li:has(> div > blockquote) {
14291429
.tab-labels {
14301430
display: flex;
14311431
position: relative;
1432-
flex-wrap: wrap;
1433-
flex-direction: row;
1432+
overflow-x: scroll;
1433+
overflow-y: hidden;
1434+
scrollbar-width: none;
1435+
white-space: nowrap;
14341436
margin: 0;
1437+
padding-left: 2rem;
14351438

14361439
> :not(:last-child) {
14371440
border-right: none;
@@ -1444,6 +1447,7 @@ li:has(> div > blockquote) {
14441447
color: oklch(var(--color-brand));
14451448
padding: 10px;
14461449
margin: 0;
1450+
position: relative;
14471451

14481452
label {
14491453
cursor: pointer;
@@ -1465,19 +1469,23 @@ li:has(> div > blockquote) {
14651469
}
14661470
}
14671471

1468-
.tab-labels:before {
1472+
.tab-labels::before {
14691473
content: "";
1470-
position: relative;
1474+
position: absolute;
1475+
display: block;
1476+
bottom: 0;
1477+
left: 0;
14711478
width: 2rem;
14721479
border-bottom: 1px solid oklch(var(--color-foreground));
14731480
}
14741481

1475-
.tab-labels:after {
1482+
.tab-labels::after {
14761483
content: "";
14771484
position: relative;
14781485
bottom: 0;
14791486
left: 0;
1480-
flex-grow: 1;
1487+
right: 0;
1488+
width: 100%;
14811489
border-bottom: 1px solid oklch(var(--color-foreground));
14821490
}
14831491
}

assets/js/tabs.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
window.addEventListener('DOMContentLoaded', () => {
2+
const tabLists = document.querySelectorAll('.tab-labels');
3+
4+
tabLists.forEach((tabList) => {
5+
const tabs = tabList.querySelectorAll(':scope > li > [role="tab"]');
6+
7+
let tabFocus = 0;
8+
let selectedTab =
9+
tabs[0].getAttribute('aria-selected') === 'true' ? tabs[0] : null;
10+
tabList.addEventListener('keyup', (e) => {
11+
if (e.key === 'Enter') {
12+
// Reset existing the aria-selected that is true
13+
selectedTab.setAttribute('aria-selected', 'false');
14+
selectedTab = e.target;
15+
16+
// Change the tabs
17+
changeTabs(e);
18+
}
19+
});
20+
21+
tabList.addEventListener('keydown', (e) => {
22+
if (e.key === 'ArrowRight' || e.key === 'ArrowLeft') {
23+
tabs[tabFocus].setAttribute('tabindex', -1);
24+
selectedTab =
25+
tabs[tabFocus].getAttribute('aria-selected') === 'true'
26+
? tabs[tabFocus]
27+
: selectedTab;
28+
if (e.key === 'ArrowRight') {
29+
tabFocus = (tabFocus + 1) % tabs.length;
30+
} else if (e.key === 'ArrowLeft') {
31+
tabFocus = tabFocus - 1;
32+
if (tabFocus < 0) {
33+
tabFocus = tabs.length - 1;
34+
}
35+
}
36+
37+
tabs[tabFocus].setAttribute('tabindex', 0);
38+
tabs[tabFocus].focus();
39+
}
40+
});
41+
});
42+
});
43+
44+
function changeTabs(e) {
45+
const toggle = document.getElementById(e.target.getAttribute('for'));
46+
const label = e.target;
47+
48+
// Toggle to new content + update Aria attributes
49+
if (toggle) {
50+
toggle.checked = true;
51+
}
52+
label.setAttribute('aria-selected', 'true');
53+
}

layouts/partials/scripts.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
{{ $codecopyv2 := resources.Get "/js/code-copy-v2.js" | fingerprint "sha512" }}
33
<script src="{{ $codecopyv2.RelPermalink }}" type="text/javascript"></script>
44

5+
<!-- load tabs js -->
6+
{{ $tabs := resources.Get "/js/tabs.js" | fingerprint "sha512" }}
7+
<script src="{{ $tabs.RelPermalink }}" type="text/javascript"></script>
8+
59
<!-- load mermaid.js -->
610
{{ if .Page.Store.Get "hasMermaid" }}
711
{{ $mermaid := resources.Get "js/mermaid.min.js" | fingerprint "sha512" }}

0 commit comments

Comments
 (0)