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

Function onFocus

packages/react-aria/src/grid/useGridCell.ts:312–347  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

310 // Grid cells can have focusable elements inside them. In this case, focus should
311 // be marshalled to that element rather than focusing the cell itself.
312 let onFocus = e => {
313 keyWhenFocused.current = node.key;
314 if (getEventTarget(e) !== ref.current) {
315 // useSelectableItem only handles setting the focused key when
316 // the focused element is the gridcell itself. We also want to
317 // set the focused key when a child element receives focus.
318 // If focus is currently visible (e.g. the user is navigating with the keyboard),
319 // then skip this. We want to restore focus to the previously focused row/cell
320 // in that case since the table should act like a single tab stop.
321 if (!isFocusVisible()) {
322 state.selectionManager.setFocusedKey(node.key);
323 }
324 return;
325 }
326
327 // if focus goes back to cell from child, make sure we don't refocus the cell if we are in focusMode=child
328 // since that would be a focus trap
329 if (
330 focusMode === 'child' &&
331 e.relatedTarget &&
332 nodeContains(ref.current, e.relatedTarget as Element)
333 ) {
334 return;
335 }
336
337 // If the cell itself is focused, wait a frame so that focus finishes propagating
338 // up to the tree, and move focus to a focusable child if possible.
339 requestAnimationFrame(() => {
340 if (
341 focusMode === 'child' &&
342 getActiveElement(getOwnerDocument(ref.current)) === ref.current
343 ) {
344 focus();
345 }
346 });
347 };
348
349 // oxlint-disable-next-line react/react-compiler
350 let gridCellProps: DOMAttributes = mergeProps(itemProps, {

Callers

nothing calls this directly

Calls 7

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

Tested by

no test coverage detected