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

Function focus

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

Source from the content-addressed store, hash-verified

113 // Handles focusing the cell. If there is a focusable child,
114 // it is focused, otherwise the cell itself is focused.
115 let focus = () => {
116 if (ref.current) {
117 let treeWalker = getFocusableTreeWalker(ref.current);
118 if (focusMode === 'child') {
119 let activeElement = getActiveElement(getOwnerDocument(ref.current));
120
121 // If focus is already on a focusable child within the cell, early return so we don't shift focus
122 if (isFocusWithin(ref.current) && ref.current !== activeElement) {
123 return;
124 }
125
126 let ownerDocument = getOwnerDocument(ref.current);
127 let shouldRestoreToLastFocused =
128 !activeElement || activeElement === ownerDocument.body || activeElement === ref.current;
129 if (shouldRestoreToLastFocused) {
130 let lastChild = lastFocusedChild.current;
131 if (
132 lastChild &&
133 keyWhenFocused.current === node.key &&
134 nodeContains(ref.current, lastChild)
135 ) {
136 focusSafely(lastChild);
137 return;
138 }
139 }
140
141 let focusable =
142 state.selectionManager.childFocusStrategy === 'last'
143 ? last(treeWalker)
144 : (treeWalker.firstChild() as FocusableElement);
145 if (focusable) {
146 focusSafely(focusable);
147 return;
148 }
149 }
150
151 if (
152 (keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
153 !isFocusWithin(ref.current)
154 ) {
155 focusSafely(ref.current);
156 }
157 }
158 };
159
160 let {itemProps, isPressed} = useSelectableItem({
161 selectionManager: state.selectionManager,

Callers 3

onFocusFunction · 0.70
useSelectableItemFunction · 0.50
useFormValidationFunction · 0.50

Calls 8

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

Tested by

no test coverage detected