|
5 | 5 | * Other licensing options may be available, please reach out to [email protected] for more information.
|
6 | 6 | */
|
7 | 7 |
|
| 8 | +import * as constants from "../../constants"; |
| 9 | + |
8 | 10 | export interface ExtendedHTMLElement extends HTMLElement {
|
9 | 11 | pastFocus?: HTMLElement | null;
|
10 | 12 | }
|
11 | 13 |
|
| 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 | + |
12 | 74 | /**
|
13 | 75 | * Listens for shortcutGuide related keypresses and handles the outcomes.
|
14 | 76 | *
|
@@ -36,6 +98,12 @@ export function guideKeyHandler({
|
36 | 98 | returnGuide(shortcutGuideRef, setIsShortcutGuideOpen);
|
37 | 99 | }
|
38 | 100 | break;
|
| 101 | + case "ArrowLeft": |
| 102 | + handleArrowLeft(event, shortcutGuideRef); |
| 103 | + break; |
| 104 | + case "ArrowRight": |
| 105 | + handleArrowRight(event, shortcutGuideRef); |
| 106 | + break; |
39 | 107 | default:
|
40 | 108 | break;
|
41 | 109 | }
|
|
0 commit comments