MCPcopy Create free account
hub / github.com/adobe/react-spectrum / onKeyDown

Function onKeyDown

packages/react-aria/src/toolbar/useToolbar.ts:65–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 7

nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
getActiveElementFunction · 0.90
focusFirstMethod · 0.80
focusLastMethod · 0.80
focusPreviousMethod · 0.65
focusNextMethod · 0.65

Tested by

no test coverage detected