MCPcopy
hub / github.com/adobe/react-spectrum / focus

Function focus

packages/react-aria/src/grid/useGridCell.ts:94–120  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

92 // Handles focusing the cell. If there is a focusable child,
93 // it is focused, otherwise the cell itself is focused.
94 let focus = () => {
95 if (ref.current) {
96 let treeWalker = getFocusableTreeWalker(ref.current);
97 if (focusMode === 'child') {
98 // If focus is already on a focusable child within the cell, early return so we don't shift focus
99 if (isFocusWithin(ref.current) && ref.current !== getActiveElement()) {
100 return;
101 }
102
103 let focusable =
104 state.selectionManager.childFocusStrategy === 'last'
105 ? last(treeWalker)
106 : (treeWalker.firstChild() as FocusableElement);
107 if (focusable) {
108 focusSafely(focusable);
109 return;
110 }
111 }
112
113 if (
114 (keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
115 !isFocusWithin(ref.current)
116 ) {
117 focusSafely(ref.current);
118 }
119 }
120 };
121
122 let {itemProps, isPressed} = useSelectableItem({
123 selectionManager: state.selectionManager,

Callers 3

onFocusFunction · 0.70
useSelectableItemFunction · 0.50
useFormValidationFunction · 0.50

Calls 6

getFocusableTreeWalkerFunction · 0.90
isFocusWithinFunction · 0.90
getActiveElementFunction · 0.90
focusSafelyFunction · 0.90
lastFunction · 0.70
firstChildMethod · 0.45

Tested by

no test coverage detected