(activeElement: HTMLElement, event: KeyboardEvent)
| 65 | // DOM utilities used for focus management |
| 66 | |
| 67 | export function getNextFocusableElement(activeElement: HTMLElement, event: KeyboardEvent): HTMLElement | undefined { |
| 68 | const elementState = getElementState(activeElement) |
| 69 | |
| 70 | // Reference: https://www.w3.org/WAI/ARIA/apg/patterns/treeview/#keyboard-interaction-24 |
| 71 | switch (`${elementState} ${event.key}`) { |
| 72 | case 'open ArrowRight': |
| 73 | // Focus first child node |
| 74 | return getFirstChildElement(activeElement) |
| 75 | |
| 76 | case 'open ArrowLeft': |
| 77 | // Close node; don't change focus |
| 78 | return |
| 79 | |
| 80 | case 'closed ArrowRight': |
| 81 | // Open node; don't change focus |
| 82 | return |
| 83 | |
| 84 | case 'closed ArrowLeft': |
| 85 | // Focus parent element |
| 86 | return getParentElement(activeElement) |
| 87 | |
| 88 | case 'end ArrowRight': |
| 89 | // Do nothing |
| 90 | return |
| 91 | |
| 92 | case 'end ArrowLeft': |
| 93 | // Focus parent element |
| 94 | return getParentElement(activeElement) |
| 95 | } |
| 96 | |
| 97 | // ArrowUp, ArrowDown, Home, and End behavior are the same regarless of element state |
| 98 | switch (event.key) { |
| 99 | case 'ArrowUp': |
| 100 | // Focus previous visible element |
| 101 | return getVisibleElement(activeElement, 'previous') |
| 102 | |
| 103 | case 'ArrowDown': |
| 104 | // Focus next visible element |
| 105 | return getVisibleElement(activeElement, 'next') |
| 106 | |
| 107 | case 'Backspace': |
| 108 | return getParentElement(activeElement) |
| 109 | |
| 110 | case 'Home': |
| 111 | // Focus first visible element |
| 112 | return getFirstElement(activeElement) |
| 113 | |
| 114 | case 'End': |
| 115 | // Focus last visible element |
| 116 | return getLastElement(activeElement) |
| 117 | |
| 118 | case 'PageUp': |
| 119 | return getPreviousPageElement(activeElement) |
| 120 | |
| 121 | case 'PageDown': |
| 122 | return getNextPageElement(activeElement) |
| 123 | } |
| 124 | } |
no test coverage detected