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

Function focus

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

Source from the content-addressed store, hash-verified

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