@react-aria/selection 3.27.2 → 3.28.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/dist/import.mjs +7 -7
  2. package/dist/main.js +12 -12
  3. package/dist/main.js.map +1 -1
  4. package/dist/module.js +7 -7
  5. package/dist/module.js.map +1 -1
  6. package/dist/types/src/index.d.ts +10 -0
  7. package/package.json +15 -17
  8. package/src/index.ts +10 -10
  9. package/dist/DOMLayoutDelegate.main.js +0 -59
  10. package/dist/DOMLayoutDelegate.main.js.map +0 -1
  11. package/dist/DOMLayoutDelegate.mjs +0 -54
  12. package/dist/DOMLayoutDelegate.module.js +0 -54
  13. package/dist/DOMLayoutDelegate.module.js.map +0 -1
  14. package/dist/ListKeyboardDelegate.main.js +0 -197
  15. package/dist/ListKeyboardDelegate.main.js.map +0 -1
  16. package/dist/ListKeyboardDelegate.mjs +0 -192
  17. package/dist/ListKeyboardDelegate.module.js +0 -192
  18. package/dist/ListKeyboardDelegate.module.js.map +0 -1
  19. package/dist/types.d.ts +0 -258
  20. package/dist/types.d.ts.map +0 -1
  21. package/dist/useSelectableCollection.main.js +0 -404
  22. package/dist/useSelectableCollection.main.js.map +0 -1
  23. package/dist/useSelectableCollection.mjs +0 -399
  24. package/dist/useSelectableCollection.module.js +0 -399
  25. package/dist/useSelectableCollection.module.js.map +0 -1
  26. package/dist/useSelectableItem.main.js +0 -263
  27. package/dist/useSelectableItem.main.js.map +0 -1
  28. package/dist/useSelectableItem.mjs +0 -258
  29. package/dist/useSelectableItem.module.js +0 -258
  30. package/dist/useSelectableItem.module.js.map +0 -1
  31. package/dist/useSelectableList.main.js +0 -63
  32. package/dist/useSelectableList.main.js.map +0 -1
  33. package/dist/useSelectableList.mjs +0 -58
  34. package/dist/useSelectableList.module.js +0 -58
  35. package/dist/useSelectableList.module.js.map +0 -1
  36. package/dist/useTypeSelect.main.js +0 -77
  37. package/dist/useTypeSelect.main.js.map +0 -1
  38. package/dist/useTypeSelect.mjs +0 -72
  39. package/dist/useTypeSelect.module.js +0 -72
  40. package/dist/useTypeSelect.module.js.map +0 -1
  41. package/dist/utils.main.js +0 -46
  42. package/dist/utils.main.js.map +0 -1
  43. package/dist/utils.mjs +0 -38
  44. package/dist/utils.module.js +0 -38
  45. package/dist/utils.module.js.map +0 -1
  46. package/src/DOMLayoutDelegate.ts +0 -61
  47. package/src/ListKeyboardDelegate.ts +0 -291
  48. package/src/useSelectableCollection.ts +0 -594
  49. package/src/useSelectableItem.ts +0 -422
  50. package/src/useSelectableList.ts +0 -85
  51. package/src/useTypeSelect.ts +0 -117
  52. package/src/utils.ts +0 -47
@@ -1,594 +0,0 @@
1
- /*
2
- * Copyright 2020 Adobe. All rights reserved.
3
- * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License. You may obtain a copy
5
- * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
- *
7
- * Unless required by applicable law or agreed to in writing, software distributed under
8
- * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
- * OF ANY KIND, either express or implied. See the License for the specific language
10
- * governing permissions and limitations under the License.
11
- */
12
-
13
- import {CLEAR_FOCUS_EVENT, FOCUS_EVENT, focusWithoutScrolling, getActiveElement, getEventTarget, isCtrlKeyPressed, isFocusWithin, isTabbable, mergeProps, nodeContains, scrollIntoView, scrollIntoViewport, useEvent, useRouter, useUpdateLayoutEffect} from '@react-aria/utils';
14
- import {dispatchVirtualFocus, getFocusableTreeWalker, moveVirtualFocus} from '@react-aria/focus';
15
- import {DOMAttributes, FocusableElement, FocusStrategy, Key, KeyboardDelegate, RefObject} from '@react-types/shared';
16
- import {flushSync} from 'react-dom';
17
- import {FocusEvent, KeyboardEvent, useEffect, useRef} from 'react';
18
- import {focusSafely, getInteractionModality} from '@react-aria/interactions';
19
- import {getItemElement, isNonContiguousSelectionModifier, useCollectionId} from './utils';
20
- import {MultipleSelectionManager} from '@react-stately/selection';
21
- import {useLocale} from '@react-aria/i18n';
22
- import {useTypeSelect} from './useTypeSelect';
23
-
24
- export interface AriaSelectableCollectionOptions {
25
- /**
26
- * An interface for reading and updating multiple selection state.
27
- */
28
- selectionManager: MultipleSelectionManager,
29
- /**
30
- * A delegate object that implements behavior for keyboard focus movement.
31
- */
32
- keyboardDelegate: KeyboardDelegate,
33
- /**
34
- * The ref attached to the element representing the collection.
35
- */
36
- ref: RefObject<HTMLElement | null>,
37
- /**
38
- * Whether the collection or one of its items should be automatically focused upon render.
39
- * @default false
40
- */
41
- autoFocus?: boolean | FocusStrategy,
42
- /**
43
- * Whether focus should wrap around when the end/start is reached.
44
- * @default false
45
- */
46
- shouldFocusWrap?: boolean,
47
- /**
48
- * Whether the collection allows empty selection.
49
- * @default false
50
- */
51
- disallowEmptySelection?: boolean,
52
- /**
53
- * Whether the collection allows the user to select all items via keyboard shortcut.
54
- * @default false
55
- */
56
- disallowSelectAll?: boolean,
57
- /**
58
- * Whether pressing the Escape should clear selection in the collection or not.
59
- * @default 'clearSelection'
60
- */
61
- escapeKeyBehavior?: 'clearSelection' | 'none',
62
- /**
63
- * Whether selection should occur automatically on focus.
64
- * @default false
65
- */
66
- selectOnFocus?: boolean,
67
- /**
68
- * Whether typeahead is disabled.
69
- * @default false
70
- */
71
- disallowTypeAhead?: boolean,
72
- /**
73
- * Whether the collection items should use virtual focus instead of being focused directly.
74
- */
75
- shouldUseVirtualFocus?: boolean,
76
- /**
77
- * Whether navigation through tab key is enabled.
78
- */
79
- allowsTabNavigation?: boolean,
80
- /**
81
- * Whether the collection items are contained in a virtual scroller.
82
- */
83
- isVirtualized?: boolean,
84
- /**
85
- * The ref attached to the scrollable body. Used to provide automatic scrolling on item focus for non-virtualized collections.
86
- * If not provided, defaults to the collection ref.
87
- */
88
- scrollRef?: RefObject<HTMLElement | null>,
89
- /**
90
- * The behavior of links in the collection.
91
- * - 'action': link behaves like onAction.
92
- * - 'selection': link follows selection interactions (e.g. if URL drives selection).
93
- * - 'override': links override all other interactions (link items are not selectable).
94
- * @default 'action'
95
- */
96
- linkBehavior?: 'action' | 'selection' | 'override'
97
- }
98
-
99
- export interface SelectableCollectionAria {
100
- /** Props for the collection element. */
101
- collectionProps: DOMAttributes
102
- }
103
-
104
- /**
105
- * Handles interactions with selectable collections.
106
- */
107
- export function useSelectableCollection(options: AriaSelectableCollectionOptions): SelectableCollectionAria {
108
- let {
109
- selectionManager: manager,
110
- keyboardDelegate: delegate,
111
- ref,
112
- autoFocus = false,
113
- shouldFocusWrap = false,
114
- disallowEmptySelection = false,
115
- disallowSelectAll = false,
116
- escapeKeyBehavior = 'clearSelection',
117
- selectOnFocus = manager.selectionBehavior === 'replace',
118
- disallowTypeAhead = false,
119
- shouldUseVirtualFocus,
120
- allowsTabNavigation = false,
121
- // If no scrollRef is provided, assume the collection ref is the scrollable region
122
- scrollRef = ref,
123
- linkBehavior = 'action'
124
- } = options;
125
- let {direction} = useLocale();
126
- let router = useRouter();
127
-
128
- let onKeyDown = (e: KeyboardEvent) => {
129
- // Prevent option + tab from doing anything since it doesn't move focus to the cells, only buttons/checkboxes
130
- if (e.altKey && e.key === 'Tab') {
131
- e.preventDefault();
132
- }
133
-
134
- // Keyboard events bubble through portals. Don't handle keyboard events
135
- // for elements outside the collection (e.g. menus).
136
- if (!ref.current || !nodeContains(ref.current, getEventTarget(e) as Element)) {
137
- return;
138
- }
139
-
140
- const navigateToKey = (key: Key | undefined, childFocus?: FocusStrategy) => {
141
- if (key != null) {
142
- if (manager.isLink(key) && linkBehavior === 'selection' && selectOnFocus && !isNonContiguousSelectionModifier(e)) {
143
- // Set focused key and re-render synchronously to bring item into view if needed.
144
- flushSync(() => {
145
- manager.setFocusedKey(key, childFocus);
146
- });
147
-
148
- let item = getItemElement(ref, key);
149
- let itemProps = manager.getItemProps(key);
150
- if (item) {
151
- router.open(item, e, itemProps.href, itemProps.routerOptions);
152
- }
153
-
154
- return;
155
- }
156
-
157
- manager.setFocusedKey(key, childFocus);
158
-
159
- if (manager.isLink(key) && linkBehavior === 'override') {
160
- return;
161
- }
162
-
163
- if (e.shiftKey && manager.selectionMode === 'multiple') {
164
- manager.extendSelection(key);
165
- } else if (selectOnFocus && !isNonContiguousSelectionModifier(e)) {
166
- manager.replaceSelection(key);
167
- }
168
- }
169
- };
170
-
171
- switch (e.key) {
172
- case 'ArrowDown': {
173
- if (delegate.getKeyBelow) {
174
- let nextKey = manager.focusedKey != null
175
- ? delegate.getKeyBelow?.(manager.focusedKey)
176
- : delegate.getFirstKey?.();
177
- if (nextKey == null && shouldFocusWrap) {
178
- nextKey = delegate.getFirstKey?.(manager.focusedKey);
179
- }
180
- if (nextKey != null) {
181
- e.preventDefault();
182
- navigateToKey(nextKey);
183
- }
184
- }
185
- break;
186
- }
187
- case 'ArrowUp': {
188
- if (delegate.getKeyAbove) {
189
- let nextKey = manager.focusedKey != null
190
- ? delegate.getKeyAbove?.(manager.focusedKey)
191
- : delegate.getLastKey?.();
192
- if (nextKey == null && shouldFocusWrap) {
193
- nextKey = delegate.getLastKey?.(manager.focusedKey);
194
- }
195
- if (nextKey != null) {
196
- e.preventDefault();
197
- navigateToKey(nextKey);
198
- }
199
- }
200
- break;
201
- }
202
- case 'ArrowLeft': {
203
- if (delegate.getKeyLeftOf) {
204
- let nextKey: Key | undefined | null = manager.focusedKey != null ? delegate.getKeyLeftOf?.(manager.focusedKey) : null;
205
- if (nextKey == null && shouldFocusWrap) {
206
- nextKey = direction === 'rtl' ? delegate.getFirstKey?.(manager.focusedKey) : delegate.getLastKey?.(manager.focusedKey);
207
- }
208
- if (nextKey != null) {
209
- e.preventDefault();
210
- navigateToKey(nextKey, direction === 'rtl' ? 'first' : 'last');
211
- }
212
- }
213
- break;
214
- }
215
- case 'ArrowRight': {
216
- if (delegate.getKeyRightOf) {
217
- let nextKey: Key | undefined | null = manager.focusedKey != null ? delegate.getKeyRightOf?.(manager.focusedKey) : null;
218
- if (nextKey == null && shouldFocusWrap) {
219
- nextKey = direction === 'rtl' ? delegate.getLastKey?.(manager.focusedKey) : delegate.getFirstKey?.(manager.focusedKey);
220
- }
221
- if (nextKey != null) {
222
- e.preventDefault();
223
- navigateToKey(nextKey, direction === 'rtl' ? 'last' : 'first');
224
- }
225
- }
226
- break;
227
- }
228
- case 'Home':
229
- if (delegate.getFirstKey) {
230
- if (manager.focusedKey === null && e.shiftKey) {
231
- return;
232
- }
233
- e.preventDefault();
234
- let firstKey: Key | null = delegate.getFirstKey(manager.focusedKey, isCtrlKeyPressed(e));
235
- manager.setFocusedKey(firstKey);
236
- if (firstKey != null) {
237
- if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {
238
- manager.extendSelection(firstKey);
239
- } else if (selectOnFocus) {
240
- manager.replaceSelection(firstKey);
241
- }
242
- }
243
- }
244
- break;
245
- case 'End':
246
- if (delegate.getLastKey) {
247
- if (manager.focusedKey === null && e.shiftKey) {
248
- return;
249
- }
250
- e.preventDefault();
251
- let lastKey = delegate.getLastKey(manager.focusedKey, isCtrlKeyPressed(e));
252
- manager.setFocusedKey(lastKey);
253
- if (lastKey != null) {
254
- if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {
255
- manager.extendSelection(lastKey);
256
- } else if (selectOnFocus) {
257
- manager.replaceSelection(lastKey);
258
- }
259
- }
260
- }
261
- break;
262
- case 'PageDown':
263
- if (delegate.getKeyPageBelow && manager.focusedKey != null) {
264
- let nextKey = delegate.getKeyPageBelow(manager.focusedKey);
265
- if (nextKey != null) {
266
- e.preventDefault();
267
- navigateToKey(nextKey);
268
- }
269
- }
270
- break;
271
- case 'PageUp':
272
- if (delegate.getKeyPageAbove && manager.focusedKey != null) {
273
- let nextKey = delegate.getKeyPageAbove(manager.focusedKey);
274
- if (nextKey != null) {
275
- e.preventDefault();
276
- navigateToKey(nextKey);
277
- }
278
- }
279
- break;
280
- case 'a':
281
- if (isCtrlKeyPressed(e) && manager.selectionMode === 'multiple' && disallowSelectAll !== true) {
282
- e.preventDefault();
283
- manager.selectAll();
284
- }
285
- break;
286
- case 'Escape':
287
- if (escapeKeyBehavior === 'clearSelection' && !disallowEmptySelection && manager.selectedKeys.size !== 0) {
288
- e.stopPropagation();
289
- e.preventDefault();
290
- manager.clearSelection();
291
- }
292
- break;
293
- case 'Tab': {
294
- if (!allowsTabNavigation) {
295
- // There may be elements that are "tabbable" inside a collection (e.g. in a grid cell).
296
- // However, collections should be treated as a single tab stop, with arrow key navigation internally.
297
- // We don't control the rendering of these, so we can't override the tabIndex to prevent tabbing.
298
- // Instead, we handle the Tab key, and move focus manually to the first/last tabbable element
299
- // in the collection, so that the browser default behavior will apply starting from that element
300
- // rather than the currently focused one.
301
- if (e.shiftKey) {
302
- ref.current.focus();
303
- } else {
304
- let walker = getFocusableTreeWalker(ref.current, {tabbable: true});
305
- let next: FocusableElement | undefined = undefined;
306
- let last: FocusableElement;
307
- do {
308
- last = walker.lastChild() as FocusableElement;
309
- if (last) {
310
- next = last;
311
- }
312
- } while (last);
313
-
314
- // If the active element is NOT tabbable but is contained by an element that IS tabbable (aka the cell), the browser will actually move focus to
315
- // the containing element. We need to special case this so that tab will move focus out of the grid instead of looping between
316
- // focusing the containing cell and back to the non-tabbable child element
317
- let activeElement = getActiveElement();
318
- if (next && (!isFocusWithin(next) || (activeElement && !isTabbable(activeElement)))) {
319
- focusWithoutScrolling(next);
320
- }
321
- }
322
- break;
323
- }
324
- }
325
- }
326
- };
327
-
328
- // Store the scroll position so we can restore it later.
329
- /// TODO: should this happen all the time??
330
- let scrollPos = useRef({top: 0, left: 0});
331
- useEvent(scrollRef, 'scroll', () => {
332
- scrollPos.current = {
333
- top: scrollRef.current?.scrollTop ?? 0,
334
- left: scrollRef.current?.scrollLeft ?? 0
335
- };
336
- });
337
-
338
- let onFocus = (e: FocusEvent) => {
339
- if (manager.isFocused) {
340
- // If a focus event bubbled through a portal, reset focus state.
341
- if (!nodeContains(e.currentTarget, getEventTarget(e))) {
342
- manager.setFocused(false);
343
- }
344
-
345
- return;
346
- }
347
-
348
- // Focus events can bubble through portals. Ignore these events.
349
- if (!nodeContains(e.currentTarget, getEventTarget(e))) {
350
- return;
351
- }
352
-
353
- manager.setFocused(true);
354
- if (manager.focusedKey == null) {
355
- let navigateToKey = (key: Key | undefined | null) => {
356
- if (key != null) {
357
- manager.setFocusedKey(key);
358
- if (selectOnFocus && !manager.isSelected(key)) {
359
- manager.replaceSelection(key);
360
- }
361
- }
362
- };
363
- // If the user hasn't yet interacted with the collection, there will be no focusedKey set.
364
- // Attempt to detect whether the user is tabbing forward or backward into the collection
365
- // and either focus the first or last item accordingly.
366
- let relatedTarget = e.relatedTarget as Element;
367
- if (relatedTarget && (e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING)) {
368
- navigateToKey(manager.lastSelectedKey ?? delegate.getLastKey?.());
369
- } else {
370
- navigateToKey(manager.firstSelectedKey ?? delegate.getFirstKey?.());
371
- }
372
- } else if (scrollRef.current) {
373
- // Restore the scroll position to what it was before.
374
- scrollRef.current.scrollTop = scrollPos.current.top;
375
- scrollRef.current.scrollLeft = scrollPos.current.left;
376
- }
377
-
378
- if (manager.focusedKey != null && scrollRef.current) {
379
- // Refocus and scroll the focused item into view if it exists within the scrollable region.
380
- let element = getItemElement(ref, manager.focusedKey);
381
- if (element instanceof HTMLElement) {
382
- // This prevents a flash of focus on the first/last element in the collection, or the collection itself.
383
- if (!isFocusWithin(element) && !shouldUseVirtualFocus) {
384
- focusWithoutScrolling(element);
385
- }
386
-
387
- let modality = getInteractionModality();
388
- if (modality === 'keyboard') {
389
- scrollIntoViewport(element, {containingElement: ref.current});
390
- }
391
- }
392
- }
393
- };
394
-
395
- let onBlur = (e) => {
396
- // Don't set blurred and then focused again if moving focus within the collection.
397
- if (!nodeContains(e.currentTarget, e.relatedTarget as HTMLElement)) {
398
- manager.setFocused(false);
399
- }
400
- };
401
-
402
- // Ref to track whether the first item in the collection should be automatically focused. Specifically used for autocomplete when user types
403
- // to focus the first key AFTER the collection updates.
404
- // TODO: potentially expand the usage of this
405
- let shouldVirtualFocusFirst = useRef(false);
406
- // Add event listeners for custom virtual events. These handle updating the focused key in response to various keyboard events
407
- // at the autocomplete level
408
- // TODO: fix type later
409
- useEvent(ref, FOCUS_EVENT, !shouldUseVirtualFocus ? undefined : (e: any) => {
410
- let {detail} = e;
411
- e.stopPropagation();
412
- manager.setFocused(true);
413
- // If the user is typing forwards, autofocus the first option in the list.
414
- if (detail?.focusStrategy === 'first') {
415
- shouldVirtualFocusFirst.current = true;
416
- }
417
- });
418
-
419
- // update active descendant
420
- useUpdateLayoutEffect(() => {
421
- if (shouldVirtualFocusFirst.current) {
422
- let keyToFocus = delegate.getFirstKey?.() ?? null;
423
-
424
- // If no focusable items exist in the list, make sure to clear any activedescendant that may still exist and move focus back to
425
- // the original active element (e.g. the autocomplete input)
426
- if (keyToFocus == null) {
427
- let previousActiveElement = getActiveElement();
428
- moveVirtualFocus(ref.current);
429
- dispatchVirtualFocus(previousActiveElement!, null);
430
-
431
- // If there wasn't a focusable key but the collection had items, then that means we aren't in an intermediate load state and all keys are disabled.
432
- // Reset shouldVirtualFocusFirst so that we don't erronously autofocus an item when the collection is filtered again.
433
- if (manager.collection.size > 0) {
434
- shouldVirtualFocusFirst.current = false;
435
- }
436
- } else {
437
- manager.setFocusedKey(keyToFocus);
438
- // Only set shouldVirtualFocusFirst to false if we've successfully set the first key as the focused key
439
- // If there wasn't a key to focus, we might be in a temporary loading state so we'll want to still focus the first key
440
- // after the collection updates after load
441
- shouldVirtualFocusFirst.current = false;
442
- }
443
- }
444
- }, [manager.collection]);
445
-
446
- // reset focus first flag
447
- useUpdateLayoutEffect(() => {
448
- // If user causes the focused key to change in any other way, clear shouldVirtualFocusFirst so we don't
449
- // accidentally move focus from under them. Skip this if the collection was empty because we might be in a load
450
- // state and will still want to focus the first item after load
451
- if (manager.collection.size > 0) {
452
- shouldVirtualFocusFirst.current = false;
453
- }
454
- }, [manager.focusedKey]);
455
-
456
- useEvent(ref, CLEAR_FOCUS_EVENT, !shouldUseVirtualFocus ? undefined : (e: any) => {
457
- e.stopPropagation();
458
- manager.setFocused(false);
459
- if (e.detail?.clearFocusKey) {
460
- manager.setFocusedKey(null);
461
- }
462
- });
463
-
464
- const autoFocusRef = useRef(autoFocus);
465
- const didAutoFocusRef = useRef(false);
466
- useEffect(() => {
467
- if (autoFocusRef.current) {
468
- let focusedKey: Key | null = null;
469
-
470
- // Check focus strategy to determine which item to focus
471
- if (autoFocus === 'first') {
472
- focusedKey = delegate.getFirstKey?.() ?? null;
473
- } if (autoFocus === 'last') {
474
- focusedKey = delegate.getLastKey?.() ?? null;
475
- }
476
-
477
- // If there are any selected keys, make the first one the new focus target
478
- let selectedKeys = manager.selectedKeys;
479
- if (selectedKeys.size) {
480
- for (let key of selectedKeys) {
481
- if (manager.canSelectItem(key)) {
482
- focusedKey = key;
483
- break;
484
- }
485
- }
486
- }
487
-
488
- manager.setFocused(true);
489
- manager.setFocusedKey(focusedKey);
490
-
491
- // If no default focus key is selected, focus the collection itself.
492
- if (focusedKey == null && !shouldUseVirtualFocus && ref.current) {
493
- focusSafely(ref.current);
494
- }
495
-
496
- // Wait until the collection has items to autofocus.
497
- if (manager.collection.size > 0) {
498
- autoFocusRef.current = false;
499
- didAutoFocusRef.current = true;
500
- }
501
- }
502
- });
503
-
504
- // Scroll the focused element into view when the focusedKey changes.
505
- let lastFocusedKey = useRef(manager.focusedKey);
506
- let raf = useRef<number | null>(null);
507
- useEffect(() => {
508
- if (manager.isFocused && manager.focusedKey != null && (manager.focusedKey !== lastFocusedKey.current || didAutoFocusRef.current) && scrollRef.current && ref.current) {
509
- let modality = getInteractionModality();
510
- let element = getItemElement(ref, manager.focusedKey);
511
- if (!(element instanceof HTMLElement)) {
512
- // If item element wasn't found, return early (don't update autoFocusRef and lastFocusedKey).
513
- // The collection may initially be empty (e.g. virtualizer), so wait until the element exists.
514
- return;
515
- }
516
-
517
- if (modality === 'keyboard' || didAutoFocusRef.current) {
518
-
519
- if (raf.current) {
520
- cancelAnimationFrame(raf.current);
521
- }
522
-
523
- raf.current = requestAnimationFrame(() => {
524
- if (scrollRef.current) {
525
- scrollIntoView(scrollRef.current, element);
526
- // Avoid scroll in iOS VO, since it may cause overlay to close (i.e. RAC submenu)
527
- if (modality !== 'virtual') {
528
- scrollIntoViewport(element, {containingElement: ref.current});
529
- }
530
- }
531
- });
532
- }
533
- }
534
-
535
- // If the focused key becomes null (e.g. the last item is deleted), focus the whole collection.
536
- if (!shouldUseVirtualFocus && manager.isFocused && manager.focusedKey == null && lastFocusedKey.current != null && ref.current) {
537
- focusSafely(ref.current);
538
- }
539
-
540
- lastFocusedKey.current = manager.focusedKey;
541
- didAutoFocusRef.current = false;
542
- });
543
-
544
- useEffect(() => {
545
- return () => {
546
- if (raf.current) {
547
- cancelAnimationFrame(raf.current);
548
- }
549
- };
550
- }, []);
551
-
552
- // Intercept FocusScope restoration since virtualized collections can reuse DOM nodes.
553
- useEvent(ref, 'react-aria-focus-scope-restore', e => {
554
- e.preventDefault();
555
- manager.setFocused(true);
556
- });
557
-
558
- let handlers = {
559
- onKeyDown,
560
- onFocus,
561
- onBlur,
562
- onMouseDown(e) {
563
- // Ignore events that bubbled through portals.
564
- if (scrollRef.current === getEventTarget(e)) {
565
- // Prevent focus going to the collection when clicking on the scrollbar.
566
- e.preventDefault();
567
- }
568
- }
569
- };
570
-
571
- let {typeSelectProps} = useTypeSelect({
572
- keyboardDelegate: delegate,
573
- selectionManager: manager
574
- });
575
-
576
- if (!disallowTypeAhead) {
577
- handlers = mergeProps(typeSelectProps, handlers);
578
- }
579
-
580
- // If nothing is focused within the collection, make the collection itself tabbable.
581
- // This will be marshalled to either the first or last item depending on where focus came from.
582
- let tabIndex: number | undefined = undefined;
583
- if (!shouldUseVirtualFocus) {
584
- tabIndex = manager.focusedKey == null ? 0 : -1;
585
- }
586
-
587
- let collectionId = useCollectionId(manager.collection);
588
- return {
589
- collectionProps: mergeProps(handlers, {
590
- tabIndex,
591
- 'data-collection': collectionId
592
- })
593
- };
594
- }