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

Function onKeyDownCapture

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 14

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

Tested by

no test coverage detected