Skip to content

Commit 73eeaf1

Browse files
committed
Added arrow navigation inside of the shortcut guide. Fixed wrong CustomTranslations type import
1 parent 5a8f7d1 commit 73eeaf1

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

src/AutoVizuA11y.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { initToolTutorial } from "./utils/initToolTutorial";
2626
import { processData } from "./utils/processData";
2727
import { ShortcutGuideContainer } from "./components/shortcut_guide/index";
2828
import { toSafeClassName } from "./utils/toSafeClassname";
29-
import { CustomTranslations } from "./utils/customTranslations";
29+
import { CustomTranslations } from "./hooks/useIsolatedI18n";
3030
import { useIsolatedI18n } from "./hooks/useIsolatedI18n";
3131

3232
type AutoDescriptionsProps = {

src/components/navigation/GuideKeyHandler.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,72 @@
55
* Other licensing options may be available, please reach out to [email protected] for more information.
66
*/
77

8+
import * as constants from "../../constants";
9+
810
export interface ExtendedHTMLElement extends HTMLElement {
911
pastFocus?: HTMLElement | null;
1012
}
1113

14+
/**
15+
* Handles the pressing of right arrow key inside the Shortcut Guide
16+
*/
17+
function handleArrowRight(
18+
event: React.KeyboardEvent,
19+
shortcutGuideRef: React.RefObject<HTMLDialogElement>,
20+
): void {
21+
event.preventDefault();
22+
23+
const shortcutGuide = shortcutGuideRef.current;
24+
if (!shortcutGuide) return;
25+
26+
const rows = Array.from(
27+
shortcutGuide.querySelectorAll(`.${constants.SHORTCUTGUIDE_CLASSES.shortcutGuideRow}`),
28+
);
29+
if (rows.length === 0) return;
30+
31+
const activeElement = document.activeElement as HTMLElement | null;
32+
const currentRowIndex = rows.findIndex((row) => row === activeElement);
33+
34+
let nextIndex = currentRowIndex + 1;
35+
if (currentRowIndex === -1) {
36+
nextIndex = 0;
37+
}
38+
39+
if (nextIndex < rows.length) {
40+
(rows[nextIndex] as HTMLElement).focus();
41+
}
42+
}
43+
44+
/**
45+
* Handles the pressing of left arrow key inside the Shortcut Guide
46+
*/
47+
function handleArrowLeft(
48+
event: React.KeyboardEvent,
49+
shortcutGuideRef: React.RefObject<HTMLDialogElement>,
50+
): void {
51+
event.preventDefault();
52+
53+
const shortcutGuide = shortcutGuideRef.current;
54+
if (!shortcutGuide) return;
55+
56+
const rows = Array.from(
57+
shortcutGuide.querySelectorAll(`.${constants.SHORTCUTGUIDE_CLASSES.shortcutGuideRow}`),
58+
);
59+
if (rows.length === 0) return;
60+
61+
const activeElement = document.activeElement as HTMLElement | null;
62+
const currentRowIndex = rows.findIndex((row) => row === activeElement);
63+
64+
let prevIndex = currentRowIndex - 1;
65+
if (currentRowIndex === -1) {
66+
prevIndex = rows.length - 1;
67+
}
68+
69+
if (prevIndex >= 0) {
70+
(rows[prevIndex] as HTMLElement).focus();
71+
}
72+
}
73+
1274
/**
1375
* Listens for shortcutGuide related keypresses and handles the outcomes.
1476
*
@@ -36,6 +98,12 @@ export function guideKeyHandler({
3698
returnGuide(shortcutGuideRef, setIsShortcutGuideOpen);
3799
}
38100
break;
101+
case "ArrowLeft":
102+
handleArrowLeft(event, shortcutGuideRef);
103+
break;
104+
case "ArrowRight":
105+
handleArrowRight(event, shortcutGuideRef);
106+
break;
39107
default:
40108
break;
41109
}

0 commit comments

Comments
 (0)