Skip to content

Commit b4f8d53

Browse files
committed
add key binding for switching panels and enable autofocus on mount
1 parent 1470b37 commit b4f8d53

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/features/debugger/components/debugger-toolbar/debugger-toolbar.component.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useEffect, useRef } from "react";
22
import styles from "./debugger-toolbar.module.scss";
33
import { BoxComponent } from "@/features/common/components/box/box.component";
44
import { useDebuggerStore } from "@/features/debugger/services/debugger.store";
@@ -18,9 +18,26 @@ interface DebuggerToolbarComponentProps {
1818
export const DebuggerToolbarComponent: React.FC<
1919
DebuggerToolbarComponentProps
2020
> = ({ decoderDictionary, encoderDictionary, mode }) => {
21+
const tabRefs = useRef<Array<HTMLLIElement | null>>([]);
2122
const activeWidget$ = useDebuggerStore((state) => state.activeWidget$);
22-
2323
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+
};
2441

2542
if (mode === DebuggerModeValues.UNIFIED) {
2643
return (
@@ -40,8 +57,15 @@ export const DebuggerToolbarComponent: React.FC<
4057
onClick={() => {
4158
setActiveWidget$(DebuggerWidgetValues.DECODER);
4259
}}
60+
onKeyDown={handleKeyDown}
4361
data-active={activeWidget$ === DebuggerWidgetValues.DECODER}
4462
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}
4569
>
4670
<span className={styles.titleTab__compactLabel}>
4771
{decoderDictionary.compactTitle}
@@ -53,11 +77,18 @@ export const DebuggerToolbarComponent: React.FC<
5377
<li
5478
role="tab"
5579
className={styles.titleTab}
80+
onKeyDown={handleKeyDown}
5681
onClick={() => {
5782
setActiveWidget$(DebuggerWidgetValues.ENCODER);
5883
}}
5984
data-active={activeWidget$ === DebuggerWidgetValues.ENCODER}
6085
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}
6192
>
6293
<span className={styles.titleTab__compactLabel}>
6394
{encoderDictionary.compactTitle}

0 commit comments

Comments
 (0)