1
- import React from "react" ;
1
+ import React , { useEffect , useRef } from "react" ;
2
2
import styles from "./debugger-toolbar.module.scss" ;
3
3
import { BoxComponent } from "@/features/common/components/box/box.component" ;
4
4
import { useDebuggerStore } from "@/features/debugger/services/debugger.store" ;
@@ -18,9 +18,26 @@ interface DebuggerToolbarComponentProps {
18
18
export const DebuggerToolbarComponent : React . FC <
19
19
DebuggerToolbarComponentProps
20
20
> = ( { decoderDictionary, encoderDictionary, mode } ) => {
21
+ const tabRefs = useRef < Array < HTMLLIElement | null > > ( [ ] ) ;
21
22
const activeWidget$ = useDebuggerStore ( ( state ) => state . activeWidget$ ) ;
22
-
23
23
const setActiveWidget$ = useDebuggerStore ( ( state ) => state . setActiveWidget$ ) ;
24
+ const isDecoder = activeWidget$ === DebuggerWidgetValues . DECODER ;
25
+
26
+ useEffect ( ( ) => {
27
+ tabRefs . current [ isDecoder ? 0 : 1 ] ?. focus ( ) ;
28
+ } , [ isDecoder ] ) ;
29
+
30
+ const handleKeyDown = ( e : React . KeyboardEvent < HTMLLIElement > ) => {
31
+ const { key } = e ;
32
+
33
+ if ( key == "ArrowRight" || key == "ArrowLeft" ) {
34
+ setActiveWidget$ (
35
+ isDecoder ? DebuggerWidgetValues . ENCODER : DebuggerWidgetValues . DECODER
36
+ ) ;
37
+ e . preventDefault ( ) ;
38
+ }
39
+ tabRefs . current [ isDecoder ? 0 : 1 ] ?. focus ( ) ;
40
+ } ;
24
41
25
42
if ( mode === DebuggerModeValues . UNIFIED ) {
26
43
return (
@@ -40,8 +57,15 @@ export const DebuggerToolbarComponent: React.FC<
40
57
onClick = { ( ) => {
41
58
setActiveWidget$ ( DebuggerWidgetValues . DECODER ) ;
42
59
} }
60
+ onKeyDown = { handleKeyDown }
43
61
data-active = { activeWidget$ === DebuggerWidgetValues . DECODER }
44
62
data-testid = { dataTestidDictionary . debugger . decoderTab . id }
63
+ aria-selected = { activeWidget$ === DebuggerWidgetValues . DECODER }
64
+ aria-controls = { `${ DebuggerWidgetValues . DECODER } -panel` }
65
+ ref = { ( el ) => {
66
+ tabRefs . current [ 0 ] = el ;
67
+ } }
68
+ tabIndex = { activeWidget$ === DebuggerWidgetValues . DECODER ? 0 : - 1 }
45
69
>
46
70
< span className = { styles . titleTab__compactLabel } >
47
71
{ decoderDictionary . compactTitle }
@@ -53,11 +77,18 @@ export const DebuggerToolbarComponent: React.FC<
53
77
< li
54
78
role = "tab"
55
79
className = { styles . titleTab }
80
+ onKeyDown = { handleKeyDown }
56
81
onClick = { ( ) => {
57
82
setActiveWidget$ ( DebuggerWidgetValues . ENCODER ) ;
58
83
} }
59
84
data-active = { activeWidget$ === DebuggerWidgetValues . ENCODER }
60
85
data-testid = { dataTestidDictionary . debugger . encoderTab . id }
86
+ aria-selected = { activeWidget$ === DebuggerWidgetValues . ENCODER }
87
+ aria-controls = { `${ DebuggerWidgetValues . ENCODER } -panel` }
88
+ ref = { ( el ) => {
89
+ tabRefs . current [ 1 ] = el ;
90
+ } }
91
+ tabIndex = { activeWidget$ === DebuggerWidgetValues . ENCODER ? 0 : - 1 }
61
92
>
62
93
< span className = { styles . titleTab__compactLabel } >
63
94
{ encoderDictionary . compactTitle }
0 commit comments