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

Function useGridCell

packages/react-aria/src/grid/useGridCell.ts:83–393  ·  view source on GitHub ↗
(
  props: GridCellProps,
  state: GridState<T, C>,
  ref: RefObject<FocusableElement | null>
)

Source from the content-addressed store, hash-verified

81 * @param state - State of the parent grid, as returned by `useGridState`.
82 */
83export function useGridCell<T, C extends GridCollection<T>>(
84 props: GridCellProps,
85 state: GridState<T, C>,
86 ref: RefObject<FocusableElement | null>
87): GridCellAria {
88 let {
89 node,
90 isVirtualized,
91 focusMode: focusModeProp,
92 allowsArrowNavigation,
93 shouldSelectOnPressUp,
94 onAction
95 } = props;
96
97 let {direction} = useLocale();
98 let {
99 keyboardDelegate,
100 actions: {onCellAction},
101 keyboardNavigationBehavior
102 } = gridMap.get(state)!;
103 let focusMode = focusModeProp ?? (keyboardNavigationBehavior === 'tab' ? 'cell' : 'child');
104
105 // We need to track the key of the item at the time it was last focused so that we force
106 // focus to go to the item when the DOM node is reused for a different item in a virtualizer.
107 let keyWhenFocused = useRef<Key | null>(null);
108
109 // Handles focusing the cell. If there is a focusable child,
110 // it is focused, otherwise the cell itself is focused.
111 let focus = () => {
112 if (ref.current) {
113 let treeWalker = getFocusableTreeWalker(ref.current);
114 if (focusMode === 'child') {
115 // If focus is already on a focusable child within the cell, early return so we don't shift focus
116 if (
117 isFocusWithin(ref.current) &&
118 ref.current !== getActiveElement(getOwnerDocument(ref.current))
119 ) {
120 return;
121 }
122
123 let focusable =
124 state.selectionManager.childFocusStrategy === 'last'
125 ? last(treeWalker)
126 : (treeWalker.firstChild() as FocusableElement);
127 if (focusable) {
128 focusSafely(focusable);
129 return;
130 }
131 }
132
133 if (
134 (keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
135 !isFocusWithin(ref.current)
136 ) {
137 focusSafely(ref.current);
138 }
139 }
140 };

Callers 8

useTableCellFunction · 0.90
useTableColumnHeaderFunction · 0.90
RowFunction · 0.90
DraggableCollectionItemFunction · 0.90
CollectionItemFunction · 0.90
CollectionItemFunction · 0.90
DraggableCollectionItemFunction · 0.90
InternalCardFunction · 0.90

Calls 5

useLocaleFunction · 0.90
useSelectableItemFunction · 0.90
mergePropsFunction · 0.90
removeAttributeMethod · 0.80
setAttributeMethod · 0.80

Tested by

no test coverage detected