-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathuseGridCell.ts
More file actions
300 lines (274 loc) · 11.7 KB
/
useGridCell.ts
File metadata and controls
300 lines (274 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright 2020 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import {DOMAttributes, FocusableElement, Key, RefObject} from '@react-types/shared';
import {focusSafely, isFocusVisible} from '@react-aria/interactions';
import {getActiveElement, getScrollParent, isFocusWithin, mergeProps, nodeContains, scrollIntoViewport} from '@react-aria/utils';
import {getFocusableTreeWalker} from '@react-aria/focus';
import {GridCollection, GridNode} from '@react-types/grid';
import {gridMap} from './utils';
import {GridState} from '@react-stately/grid';
import {KeyboardEvent as ReactKeyboardEvent, useRef} from 'react';
import {useLocale} from '@react-aria/i18n';
import {useSelectableItem} from '@react-aria/selection';
export interface GridCellProps {
/** An object representing the grid cell. Contains all the relevant information that makes up the grid cell. */
node: GridNode<unknown>,
/** Whether the grid cell is contained in a virtual scroller. */
isVirtualized?: boolean,
/** Whether the cell or its first focusable child element should be focused when the grid cell is focused. */
focusMode?: 'child' | 'cell',
/** Whether selection should occur on press up instead of press down. */
shouldSelectOnPressUp?: boolean,
/** Indicates how many columns the data cell spans. */
colSpan?: number,
/**
* Handler that is called when a user performs an action on the cell.
* Please use onCellAction at the collection level instead.
* @deprecated
**/
onAction?: () => void
}
export interface GridCellAria {
/** Props for the grid cell element. */
gridCellProps: DOMAttributes,
/** Whether the cell is currently in a pressed state. */
isPressed: boolean
}
/**
* Provides the behavior and accessibility implementation for a cell in a grid.
* @param props - Props for the cell.
* @param state - State of the parent grid, as returned by `useGridState`.
*/
export function useGridCell<T, C extends GridCollection<T>>(props: GridCellProps, state: GridState<T, C>, ref: RefObject<FocusableElement | null>): GridCellAria {
let {
node,
isVirtualized,
focusMode = 'child',
shouldSelectOnPressUp,
onAction
} = props;
let {direction} = useLocale();
let {keyboardDelegate, actions: {onCellAction}} = gridMap.get(state)!;
// We need to track the key of the item at the time it was last focused so that we force
// focus to go to the item when the DOM node is reused for a different item in a virtualizer.
let keyWhenFocused = useRef<Key | null>(null);
// Handles focusing the cell. If there is a focusable child,
// it is focused, otherwise the cell itself is focused.
let focus = () => {
if (ref.current) {
let treeWalker = getFocusableTreeWalker(ref.current);
if (focusMode === 'child') {
// If focus is already on a focusable child within the cell, early return so we don't shift focus
if (isFocusWithin(ref.current) && ref.current !== getActiveElement()) {
return;
}
let focusable = state.selectionManager.childFocusStrategy === 'last'
? last(treeWalker)
: treeWalker.firstChild() as FocusableElement;
if (focusable) {
focusSafely(focusable);
return;
}
}
if (
(keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
!isFocusWithin(ref.current)
) {
focusSafely(ref.current);
}
}
};
let {itemProps, isPressed} = useSelectableItem({
selectionManager: state.selectionManager,
key: node.key,
ref,
isVirtualized,
focus,
shouldSelectOnPressUp,
onAction: onCellAction ? () => onCellAction(node.key) : onAction,
isDisabled: state.collection.size === 0
});
let onKeyDownCapture = (e: ReactKeyboardEvent) => {
let activeElement = getActiveElement();
if (!nodeContains(e.currentTarget, e.target as Element) || state.isKeyboardNavigationDisabled || !ref.current || !activeElement) {
return;
}
let walker = getFocusableTreeWalker(ref.current);
walker.currentNode = activeElement;
switch (e.key) {
case 'ArrowLeft': {
// Find the next focusable element within the cell.
let focusable: FocusableElement | null = direction === 'rtl'
? walker.nextNode() as FocusableElement
: walker.previousNode() as FocusableElement;
// Don't focus the cell itself if focusMode is "child"
if (focusMode === 'child' && focusable === ref.current) {
focusable = null;
}
e.preventDefault();
e.stopPropagation();
if (focusable) {
focusSafely(focusable);
scrollIntoViewport(focusable, {containingElement: getScrollParent(ref.current)});
} else {
// If there is no next focusable child, then move to the next cell to the left of this one.
// This will be handled by useSelectableCollection. However, if there is no cell to the left
// of this one, only one column, and the grid doesn't focus rows, then the next key will be the
// same as this one. In that case we need to handle focusing either the cell or the first/last
// child, depending on the focus mode.
let prev = keyboardDelegate.getKeyLeftOf?.(node.key);
if (prev !== node.key) {
// We prevent the capturing event from reaching children of the cell, e.g. pickers.
// We want arrow keys to navigate to the next cell instead. We need to re-dispatch
// the event from a higher parent so it still bubbles and gets handled by useSelectableCollection.
ref.current.parentElement?.dispatchEvent(
new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
);
break;
}
if (focusMode === 'cell' && direction === 'rtl') {
focusSafely(ref.current);
scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
} else {
walker.currentNode = ref.current;
focusable = direction === 'rtl'
? walker.firstChild() as FocusableElement
: last(walker);
if (focusable) {
focusSafely(focusable);
scrollIntoViewport(focusable, {containingElement: getScrollParent(ref.current)});
}
}
}
break;
}
case 'ArrowRight': {
let focusable: FocusableElement | null = direction === 'rtl'
? walker.previousNode() as FocusableElement
: walker.nextNode() as FocusableElement;
if (focusMode === 'child' && focusable === ref.current) {
focusable = null;
}
e.preventDefault();
e.stopPropagation();
if (focusable) {
focusSafely(focusable);
scrollIntoViewport(focusable, {containingElement: getScrollParent(ref.current)});
} else {
let next = keyboardDelegate.getKeyRightOf?.(node.key);
if (next !== node.key) {
// We prevent the capturing event from reaching children of the cell, e.g. pickers.
// We want arrow keys to navigate to the next cell instead. We need to re-dispatch
// the event from a higher parent so it still bubbles and gets handled by useSelectableCollection.
ref.current.parentElement?.dispatchEvent(
new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
);
break;
}
if (focusMode === 'cell' && direction === 'ltr') {
focusSafely(ref.current);
scrollIntoViewport(ref.current, {containingElement: getScrollParent(ref.current)});
} else {
walker.currentNode = ref.current;
focusable = direction === 'rtl'
? last(walker)
: walker.firstChild() as FocusableElement;
if (focusable) {
focusSafely(focusable);
scrollIntoViewport(focusable, {containingElement: getScrollParent(ref.current)});
}
}
}
break;
}
case 'ArrowUp':
case 'ArrowDown':
// Prevent this event from reaching cell children, e.g. menu buttons. We want arrow keys to navigate
// to the cell above/below instead. We need to re-dispatch the event from a higher parent so it still
// bubbles and gets handled by useSelectableCollection.
if (!e.altKey && nodeContains(ref.current, e.target as Element)) {
e.stopPropagation();
e.preventDefault();
ref.current.parentElement?.dispatchEvent(
new KeyboardEvent(e.nativeEvent.type, e.nativeEvent)
);
}
break;
}
};
// Grid cells can have focusable elements inside them. In this case, focus should
// be marshalled to that element rather than focusing the cell itself.
let onFocus = (e) => {
keyWhenFocused.current = node.key;
if (e.target !== ref.current) {
// useSelectableItem only handles setting the focused key when
// the focused element is the gridcell itself. We also want to
// set the focused key when a child element receives focus.
// If focus is currently visible (e.g. the user is navigating with the keyboard),
// then skip this. We want to restore focus to the previously focused row/cell
// in that case since the table should act like a single tab stop.
if (!isFocusVisible()) {
state.selectionManager.setFocusedKey(node.key);
}
return;
}
// If the cell itself is focused, wait a frame so that focus finishes propagatating
// up to the tree, and move focus to a focusable child if possible.
requestAnimationFrame(() => {
if (focusMode === 'child' && getActiveElement() === ref.current) {
focus();
}
});
};
let gridCellProps: DOMAttributes = mergeProps(itemProps, {
role: 'gridcell',
onKeyDownCapture,
'aria-colspan': node.colSpan,
'aria-colindex': node.colIndex != null ? node.colIndex + 1 : undefined, // aria-colindex is 1-based
colSpan: isVirtualized ? undefined : node.colSpan,
onFocus
});
if (isVirtualized) {
gridCellProps['aria-colindex'] = (node.colIndex ?? node.index) + 1; // aria-colindex is 1-based
}
// When pressing with a pointer and cell selection is not enabled, usePress will be applied to the
// row rather than the cell. However, when the row is draggable, usePress cannot preventDefault
// on pointer down, so the browser will try to focus the cell which has a tabIndex applied.
// To avoid this, remove the tabIndex from the cell briefly on pointer down.
if (shouldSelectOnPressUp && gridCellProps.tabIndex != null && gridCellProps.onPointerDown == null) {
gridCellProps.onPointerDown = (e) => {
let el = e.currentTarget;
let tabindex = el.getAttribute('tabindex');
el.removeAttribute('tabindex');
requestAnimationFrame(() => {
if (tabindex != null) {
el.setAttribute('tabindex', tabindex);
}
});
};
}
return {
gridCellProps,
isPressed
};
}
function last(walker: TreeWalker) {
let next: FocusableElement | null = null;
let last: FocusableElement | null = null;
do {
last = walker.lastChild() as FocusableElement | null;
if (last) {
next = last;
}
} while (last);
return next;
}