| 62 | let focusManager = createFocusManager(ref); |
| 63 | |
| 64 | const onKeyDown: KeyboardEventHandler = e => { |
| 65 | // don't handle portalled events |
| 66 | if (!nodeContains(e.currentTarget, getEventTarget(e) as HTMLElement)) { |
| 67 | return; |
| 68 | } |
| 69 | if ( |
| 70 | (orientation === 'horizontal' && e.key === 'ArrowRight') || |
| 71 | (orientation === 'vertical' && e.key === 'ArrowDown') |
| 72 | ) { |
| 73 | if (shouldReverse) { |
| 74 | focusManager.focusPrevious(); |
| 75 | } else { |
| 76 | focusManager.focusNext(); |
| 77 | } |
| 78 | } else if ( |
| 79 | (orientation === 'horizontal' && e.key === 'ArrowLeft') || |
| 80 | (orientation === 'vertical' && e.key === 'ArrowUp') |
| 81 | ) { |
| 82 | if (shouldReverse) { |
| 83 | focusManager.focusNext(); |
| 84 | } else { |
| 85 | focusManager.focusPrevious(); |
| 86 | } |
| 87 | } else if (e.key === 'Tab') { |
| 88 | // When the tab key is pressed, we want to move focus |
| 89 | // out of the entire toolbar. To do this, move focus |
| 90 | // to the first or last focusable child, and let the |
| 91 | // browser handle the Tab key as usual from there. |
| 92 | lastFocused.current = getActiveElement() as HTMLElement; |
| 93 | if (e.shiftKey) { |
| 94 | focusManager.focusFirst(); |
| 95 | } else { |
| 96 | focusManager.focusLast(); |
| 97 | } |
| 98 | return; |
| 99 | } else { |
| 100 | // if we didn't handle anything, return early so we don't preventDefault |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | // Prevent arrow keys from being handled by nested action groups. |
| 105 | e.stopPropagation(); |
| 106 | e.preventDefault(); |
| 107 | }; |
| 108 | |
| 109 | // Record the last focused child when focus moves out of the toolbar. |
| 110 | const lastFocused = useRef<HTMLElement | null>(null); |
nothing calls this directly
no test coverage detected