Skip to content

Commit a218e11

Browse files
committed
Mainframe: Adjusted js for tabs to add home + end + space support
1 parent 21bfa13 commit a218e11

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

assets/js/tabs.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ window.addEventListener('DOMContentLoaded', () => {
88
let selectedTab =
99
tabs[0].getAttribute('aria-selected') === 'true' ? tabs[0] : null;
1010
tabList.addEventListener('keyup', (e) => {
11-
if (e.key === 'Enter') {
11+
if (e.key === 'Enter' || e.key === ' ') {
12+
e.preventDefault();
1213
// Reset existing the aria-selected that is true
1314
selectedTab.setAttribute('aria-selected', 'false');
1415
selectedTab = e.target;
@@ -19,23 +20,37 @@ window.addEventListener('DOMContentLoaded', () => {
1920
});
2021

2122
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) {
23+
// Prevent page scrolling on space key hit
24+
if (e.key === ' ') {
25+
e.preventDefault();
26+
} else {
27+
if (
28+
e.key === 'ArrowRight' ||
29+
e.key === 'ArrowLeft' ||
30+
e.key === 'Home' ||
31+
e.key === 'End'
32+
) {
33+
tabs[tabFocus].setAttribute('tabindex', -1);
34+
selectedTab =
35+
tabs[tabFocus].getAttribute('aria-selected') === 'true'
36+
? tabs[tabFocus]
37+
: selectedTab;
38+
if (e.key === 'ArrowRight') {
39+
tabFocus = (tabFocus + 1) % tabs.length;
40+
} else if (e.key === 'ArrowLeft') {
41+
tabFocus = tabFocus - 1;
42+
if (tabFocus < 0) {
43+
tabFocus = tabs.length - 1;
44+
}
45+
} else if (e.key === 'Home') {
46+
tabFocus = 0;
47+
} else if (e.key === 'End') {
3348
tabFocus = tabs.length - 1;
3449
}
35-
}
3650

37-
tabs[tabFocus].setAttribute('tabindex', 0);
38-
tabs[tabFocus].focus();
51+
tabs[tabFocus].setAttribute('tabindex', 0);
52+
tabs[tabFocus].focus();
53+
}
3954
}
4055
});
4156
});

0 commit comments

Comments
 (0)