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

Function onKeyDownCapture

packages/react-aria/src/grid/useGridCell.ts:171–291  ·  view source on GitHub ↗
(e: ReactKeyboardEvent)

Source from the content-addressed store, hash-verified

169 });
170
171 let onKeyDownCapture = (e: ReactKeyboardEvent) => {
172 let activeElement = getActiveElement(getOwnerDocument(ref.current));
173 if (
174 !nodeContains(e.currentTarget, getEventTarget(e) as Element) ||
175 state.isKeyboardNavigationDisabled ||
176 !ref.current ||
177 !activeElement
178 ) {
179 return;
180 }
181
182 let walker = getFocusableTreeWalker(ref.current);
183 walker.currentNode = activeElement;
184
185 switch (e.key) {
186 case 'ArrowLeft': {
187 // Find the next focusable element within the cell.
188 let focusable: FocusableElement | null =
189 direction === 'rtl'
190 ? (walker.nextNode() as FocusableElement)
191 : (walker.previousNode() as FocusableElement);
192
193 // Don't focus the cell itself if focusMode is "child"
194 if (focusMode === 'child' && focusable === ref.current) {
195 focusable = null;
196 }
197
198 e.preventDefault();
199 e.stopPropagation();
200 if (focusable) {
201 focusSafely(focusable);
202 scrollIntoViewport(focusable, {containingElement: getScrollParent(ref.current)});
203 } else {
204 // If there is no next focusable child, then move to the next cell to the left of this one.
205 // This will be handled by useSelectableCollection. However, if there is no cell to the left
206 // of this one, only one column, and the grid doesn't focus rows, then the next key will be the
207 // same as this one. In that case we need to handle focusing either the cell or the first/last
208 // child, depending on the focus mode.
209 let prev = keyboardDelegate.getKeyLeftOf?.(node.key);
210 if (prev !== node.key) {
211 // We prevent the capturing event from reaching children of the cell, e.g. pickers.
212 // We want arrow keys to navigate to the next cell instead. We need to re-dispatch
213 // the event from a higher parent so it still bubbles and gets handled by useSelectableCollection.
214 getParentElement(ref.current)?.dispatchEvent(
215 new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
216 );
217 break;
218 }
219
220 if (focusMode === 'cell' && direction === 'rtl') {
221 focusSafely(ref.current);
222 scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
223 } else {
224 walker.currentNode = ref.current;
225 focusable =
226 direction === 'rtl' ? (walker.firstChild() as FocusableElement) : last(walker);
227 if (focusable) {
228 focusSafely(focusable);

Callers

nothing calls this directly

Calls 15

getActiveElementFunction · 0.90
getOwnerDocumentFunction · 0.90
nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
getFocusableTreeWalkerFunction · 0.90
focusSafelyFunction · 0.90
scrollIntoViewportFunction · 0.90
getScrollParentFunction · 0.90
getParentElementFunction · 0.90
nextNodeMethod · 0.80
previousNodeMethod · 0.80
lastFunction · 0.70

Tested by

no test coverage detected