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

Function onFocus

packages/react-aria/src/grid/useGridCell.ts:329–370  ·  view source on GitHub ↗
(e: FocusEvent)

Source from the content-addressed store, hash-verified

327 // Grid cells can have focusable elements inside them. In this case, focus should
328 // be marshalled to that element rather than focusing the cell itself.
329 let onFocus = (e: FocusEvent) => {
330 keyWhenFocused.current = node.key;
331 if (getEventTarget(e) !== ref.current) {
332 // useSelectableItem only handles setting the focused key when
333 // the focused element is the gridcell itself. We also want to
334 // set the focused key when a child element receives focus.
335 // If focus is currently visible (e.g. the user is navigating with the keyboard),
336 // then skip this. We want to restore focus to the previously focused row/cell
337 // in that case since the table should act like a single tab stop.
338
339 let target = getEventTarget(e) as FocusableElement;
340 if (ref.current && nodeContains(ref.current, target)) {
341 lastFocusedChild.current = target;
342 }
343
344 if (!isFocusVisible()) {
345 state.selectionManager.setFocusedKey(node.key);
346 }
347 return;
348 }
349
350 // if focus goes back to cell from child, make sure we don't refocus the cell if we are in focusMode=child
351 // since that would be a focus trap
352 if (
353 focusMode === 'child' &&
354 e.relatedTarget &&
355 nodeContains(ref.current, e.relatedTarget as Element)
356 ) {
357 return;
358 }
359
360 // If the cell itself is focused, wait a frame so that focus finishes propagating
361 // up to the tree, and move focus to a focusable child if possible.
362 requestAnimationFrame(() => {
363 if (
364 focusMode === 'child' &&
365 getActiveElement(getOwnerDocument(ref.current)) === ref.current
366 ) {
367 focus();
368 }
369 });
370 };
371
372 // oxlint-disable-next-line react/react-compiler
373 let gridCellProps: DOMAttributes = mergeProps(itemProps, {

Callers

nothing calls this directly

Calls 7

getEventTargetFunction · 0.90
nodeContainsFunction · 0.90
isFocusVisibleFunction · 0.90
getActiveElementFunction · 0.90
getOwnerDocumentFunction · 0.90
focusFunction · 0.70
setFocusedKeyMethod · 0.65

Tested by

no test coverage detected