@@ -8,7 +8,8 @@ window.addEventListener('DOMContentLoaded', () => {
8
8
let selectedTab =
9
9
tabs [ 0 ] . getAttribute ( 'aria-selected' ) === 'true' ? tabs [ 0 ] : null ;
10
10
tabList . addEventListener ( 'keyup' , ( e ) => {
11
- if ( e . key === 'Enter' ) {
11
+ if ( e . key === 'Enter' || e . key === ' ' ) {
12
+ e . preventDefault ( ) ;
12
13
// Reset existing the aria-selected that is true
13
14
selectedTab . setAttribute ( 'aria-selected' , 'false' ) ;
14
15
selectedTab = e . target ;
@@ -19,23 +20,37 @@ window.addEventListener('DOMContentLoaded', () => {
19
20
} ) ;
20
21
21
22
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' ) {
33
48
tabFocus = tabs . length - 1 ;
34
49
}
35
- }
36
50
37
- tabs [ tabFocus ] . setAttribute ( 'tabindex' , 0 ) ;
38
- tabs [ tabFocus ] . focus ( ) ;
51
+ tabs [ tabFocus ] . setAttribute ( 'tabindex' , 0 ) ;
52
+ tabs [ tabFocus ] . focus ( ) ;
53
+ }
39
54
}
40
55
} ) ;
41
56
} ) ;
0 commit comments