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

Function useGridCell

packages/react-aria/src/grid/useGridCell.ts:83–416  ·  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 // Tracks the specific focusable child that was last focused within this cell.
110 let lastFocusedChild = useRef<FocusableElement | null>(null);
111
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 =

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