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

Function focus

packages/react-aria/src/gridlist/useGridListItem.ts:115–146  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

113 // focus to go to the item when the DOM node is reused for a different item in a virtualizer.
114 let keyWhenFocused = useRef<Key | null>(null);
115 let focus = () => {
116 if (ref.current === null) {
117 return;
118 }
119
120 if (focusMode === 'child') {
121 // If focus is already on a focusable child within the row, early return so we don't shift focus
122 if (
123 isFocusWithin(ref.current) &&
124 ref.current !== getActiveElement(getOwnerDocument(ref.current))
125 ) {
126 return;
127 }
128
129 let treeWalker = getFocusableTreeWalker(ref.current, {tabbable: true});
130 let focusable = treeWalker.firstChild() as FocusableElement;
131 if (focusable) {
132 focusSafely(focusable);
133 scrollIntoViewport(focusable, {containingElement: getScrollParent(focusable)});
134 return;
135 }
136 }
137
138 // Don't shift focus to the row if the active element is a element within the row already
139 // (e.g. clicking on a row button)
140 if (
141 (keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
142 !isFocusWithin(ref.current)
143 ) {
144 focusSafely(ref.current);
145 }
146 };
147
148 let treeGridRowProps: HTMLAttributes<HTMLElement> = {};
149 let hasChildRows = props.hasChildItems;

Callers 1

onFocusFunction · 0.70

Calls 8

isFocusWithinFunction · 0.90
getActiveElementFunction · 0.90
getOwnerDocumentFunction · 0.90
getFocusableTreeWalkerFunction · 0.90
focusSafelyFunction · 0.90
scrollIntoViewportFunction · 0.90
getScrollParentFunction · 0.90
firstChildMethod · 0.45

Tested by

no test coverage detected