@octanejs/aria 0.0.46 → 0.0.47

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 (201) hide show
  1. package/README.md +7 -6
  2. package/UPSTREAM.md +58 -69
  3. package/package.json +11 -11
  4. package/src/calendar/useCalendarGrid.tsx +64 -62
  5. package/src/collections/Document.ts +3 -0
  6. package/src/color/useColorArea.tsx +95 -69
  7. package/src/color/useColorWheel.tsx +38 -37
  8. package/src/combobox/{useComboBox.ts → useComboBox.tsx} +239 -259
  9. package/src/components/ComboBox.ts +35 -2
  10. package/src/components/Dialog.ts +12 -1
  11. package/src/components/DropZone.tsx +3 -2
  12. package/src/components/FileTrigger.tsx +1 -0
  13. package/src/components/GridList.ts +11 -2
  14. package/src/components/ListBox.ts +1 -1
  15. package/src/components/Menu.ts +1 -0
  16. package/src/components/Meter.ts +3 -1
  17. package/src/components/Popover.ts +20 -8
  18. package/src/components/PreviewTrigger.tsx +101 -0
  19. package/src/components/ProgressBar.ts +10 -2
  20. package/src/components/Select.ts +1 -1
  21. package/src/components/Table.ts +26 -15
  22. package/src/components/TokenField.tsx +395 -0
  23. package/src/components/Tooltip.ts +4 -6
  24. package/src/components/Tree.ts +14 -7
  25. package/src/components/Virtualizer.tsx +30 -14
  26. package/src/components/index.ts +22 -0
  27. package/src/components/utils.ts +1 -1
  28. package/src/datepicker/useDateField.tsx +2 -11
  29. package/src/datepicker/useDatePickerGroup.tsx +44 -33
  30. package/src/datepicker/useDateSegment.tsx +31 -30
  31. package/src/dialog/useDialog.ts +8 -1
  32. package/src/dnd/DragManager.tsx +5 -1
  33. package/src/dnd/useDrag.tsx +2 -1
  34. package/src/focus/FocusScope.ts +2 -2
  35. package/src/grid/useGrid.ts +3 -0
  36. package/src/grid/{useGridCell.ts → useGridCell.tsx} +121 -64
  37. package/src/grid/utils.ts +1 -0
  38. package/src/gridlist/{useGridListItem.ts → useGridListItem.tsx} +152 -111
  39. package/src/index.ts +22 -0
  40. package/src/interactions/createEventHandler.ts +11 -8
  41. package/src/interactions/createKeyboardShortcutHandler.tsx +218 -0
  42. package/src/interactions/textSelection.ts +3 -3
  43. package/src/interactions/useContextMenu.tsx +125 -0
  44. package/src/interactions/useFocusVisible.ts +4 -4
  45. package/src/interactions/useKeyboard.ts +50 -2
  46. package/src/interactions/useLongPress.ts +33 -22
  47. package/src/interactions/useMove.ts +6 -6
  48. package/src/interactions/usePress.ts +8 -0
  49. package/src/interactions/utils.ts +10 -10
  50. package/src/intl/combobox/fi-FI.json +2 -2
  51. package/src/intl/previewtrigger/ar-AE.json +3 -0
  52. package/src/intl/previewtrigger/bg-BG.json +3 -0
  53. package/src/intl/previewtrigger/cs-CZ.json +3 -0
  54. package/src/intl/previewtrigger/da-DK.json +3 -0
  55. package/src/intl/previewtrigger/de-DE.json +3 -0
  56. package/src/intl/previewtrigger/el-GR.json +3 -0
  57. package/src/intl/previewtrigger/en-US.json +3 -0
  58. package/src/intl/previewtrigger/es-ES.json +3 -0
  59. package/src/intl/previewtrigger/et-EE.json +3 -0
  60. package/src/intl/previewtrigger/fi-FI.json +3 -0
  61. package/src/intl/previewtrigger/fr-FR.json +3 -0
  62. package/src/intl/previewtrigger/he-IL.json +3 -0
  63. package/src/intl/previewtrigger/hr-HR.json +3 -0
  64. package/src/intl/previewtrigger/hu-HU.json +3 -0
  65. package/src/intl/previewtrigger/index.ts +73 -0
  66. package/src/intl/previewtrigger/it-IT.json +3 -0
  67. package/src/intl/previewtrigger/ja-JP.json +3 -0
  68. package/src/intl/previewtrigger/ko-KR.json +3 -0
  69. package/src/intl/previewtrigger/lt-LT.json +3 -0
  70. package/src/intl/previewtrigger/lv-LV.json +3 -0
  71. package/src/intl/previewtrigger/nb-NO.json +3 -0
  72. package/src/intl/previewtrigger/nl-NL.json +3 -0
  73. package/src/intl/previewtrigger/pl-PL.json +3 -0
  74. package/src/intl/previewtrigger/pt-BR.json +3 -0
  75. package/src/intl/previewtrigger/pt-PT.json +3 -0
  76. package/src/intl/previewtrigger/ro-RO.json +3 -0
  77. package/src/intl/previewtrigger/ru-RU.json +3 -0
  78. package/src/intl/previewtrigger/sk-SK.json +3 -0
  79. package/src/intl/previewtrigger/sl-SI.json +3 -0
  80. package/src/intl/previewtrigger/sr-SP.json +3 -0
  81. package/src/intl/previewtrigger/sv-SE.json +3 -0
  82. package/src/intl/previewtrigger/tr-TR.json +3 -0
  83. package/src/intl/previewtrigger/uk-UA.json +3 -0
  84. package/src/intl/previewtrigger/zh-CN.json +3 -0
  85. package/src/intl/previewtrigger/zh-TW.json +3 -0
  86. package/src/intl/table/ar-AE.json +3 -3
  87. package/src/intl/table/bg-BG.json +3 -3
  88. package/src/intl/table/cs-CZ.json +3 -3
  89. package/src/intl/table/da-DK.json +3 -3
  90. package/src/intl/table/de-DE.json +3 -3
  91. package/src/intl/table/el-GR.json +3 -3
  92. package/src/intl/table/es-ES.json +3 -3
  93. package/src/intl/table/et-EE.json +3 -3
  94. package/src/intl/table/fi-FI.json +3 -3
  95. package/src/intl/table/fr-FR.json +3 -3
  96. package/src/intl/table/he-IL.json +3 -3
  97. package/src/intl/table/hr-HR.json +3 -3
  98. package/src/intl/table/hu-HU.json +3 -3
  99. package/src/intl/table/it-IT.json +3 -3
  100. package/src/intl/table/ja-JP.json +3 -3
  101. package/src/intl/table/ko-KR.json +3 -3
  102. package/src/intl/table/lt-LT.json +3 -3
  103. package/src/intl/table/lv-LV.json +3 -3
  104. package/src/intl/table/nb-NO.json +3 -3
  105. package/src/intl/table/nl-NL.json +3 -3
  106. package/src/intl/table/pl-PL.json +3 -3
  107. package/src/intl/table/pt-BR.json +3 -3
  108. package/src/intl/table/pt-PT.json +3 -3
  109. package/src/intl/table/ro-RO.json +3 -3
  110. package/src/intl/table/ru-RU.json +3 -3
  111. package/src/intl/table/sk-SK.json +3 -3
  112. package/src/intl/table/sl-SI.json +3 -3
  113. package/src/intl/table/sr-SP.json +3 -3
  114. package/src/intl/table/sv-SE.json +3 -3
  115. package/src/intl/table/tr-TR.json +3 -3
  116. package/src/intl/table/uk-UA.json +3 -3
  117. package/src/intl/table/zh-CN.json +4 -4
  118. package/src/intl/table/zh-TW.json +3 -3
  119. package/src/intl/tree/es-ES.json +1 -1
  120. package/src/link/useLink.ts +2 -0
  121. package/src/menu/useMenuItem.ts +33 -36
  122. package/src/menu/useMenuTrigger.tsx +217 -0
  123. package/src/menu/{useSubmenuTrigger.ts → useSubmenuTrigger.tsx} +112 -152
  124. package/src/numberfield/{useNumberField.ts → useNumberField.tsx} +185 -239
  125. package/src/overlays/Overlay.ts +5 -1
  126. package/src/overlays/ariaHideOutside.ts +6 -8
  127. package/src/overlays/useCloseOnScroll.ts +7 -5
  128. package/src/overlays/{useOverlay.ts → useOverlay.tsx} +72 -84
  129. package/src/overlays/usePopover.ts +10 -5
  130. package/src/overlays/usePreventScroll.ts +4 -4
  131. package/src/progress/useProgressBar.ts +2 -1
  132. package/src/radio/{useRadioGroup.ts → useRadioGroup.tsx} +83 -80
  133. package/src/searchfield/useSearchField.tsx +147 -0
  134. package/src/select/useSelect.ts +20 -20
  135. package/src/selection/useSelectableCollection.tsx +751 -0
  136. package/src/selection/useSelectableItem.ts +48 -11
  137. package/src/slider/{useSliderThumb.ts → useSliderThumb.tsx} +116 -159
  138. package/src/spinbutton/useSpinButton.tsx +330 -0
  139. package/src/stately/combobox/useComboBoxState.ts +1 -1
  140. package/src/stately/grid/useGridState.ts +9 -10
  141. package/src/stately/index.ts +11 -0
  142. package/src/stately/menu/useMenuTriggerState.ts +1 -1
  143. package/src/stately/menu/useSubmenuTriggerState.ts +2 -0
  144. package/src/stately/overlays/useOverlayTriggerState.ts +12 -1
  145. package/src/stately/radio/useRadioGroupState.ts +5 -1
  146. package/src/stately/table/TableUtils.ts +15 -1
  147. package/src/stately/table/useTableState.ts +7 -0
  148. package/src/stately/tokenfield/TokenFieldValue.tsx +440 -0
  149. package/src/stately/tokenfield/useTokenFieldState.tsx +57 -0
  150. package/src/stately/tooltip/useTooltipTriggerState.ts +16 -7
  151. package/src/stately/virtualizer/Layout.tsx +2 -0
  152. package/src/stately/virtualizer/ScrollAnchor.tsx +338 -0
  153. package/src/stately/virtualizer/Virtualizer.tsx +52 -1
  154. package/src/stately/virtualizer/types.tsx +2 -0
  155. package/src/table/useTableCell.ts +12 -0
  156. package/src/table/useTableColumnHeader.ts +8 -1
  157. package/src/table/useTableColumnResize.tsx +354 -0
  158. package/src/tag/useTag.ts +22 -15
  159. package/src/textfield/useTextField.ts +2 -0
  160. package/src/tokenfield/useToken.tsx +64 -0
  161. package/src/tokenfield/useTokenField.tsx +849 -0
  162. package/src/tooltip/usePreviewTrigger.tsx +266 -0
  163. package/src/tooltip/useSafeArea.tsx +171 -0
  164. package/src/upstream-exports/react-aria/mergeRefs.tsx +3 -0
  165. package/src/upstream-exports/react-aria/usePreviewTrigger.tsx +18 -0
  166. package/src/upstream-exports/react-aria/useTokenField.tsx +22 -0
  167. package/src/upstream-exports/react-stately/private/collections/getChildNodes.tsx +8 -0
  168. package/src/upstream-exports/react-stately/private/collections/getItemCount.tsx +2 -0
  169. package/src/upstream-exports/react-stately/private/grid/GridCollection.tsx +7 -0
  170. package/src/upstream-exports/react-stately/private/grid/useGridState.tsx +6 -0
  171. package/src/upstream-exports/react-stately/private/utils/number.tsx +2 -0
  172. package/src/upstream-exports/react-stately/useComboBoxState.tsx +23 -0
  173. package/src/upstream-exports/react-stately/useListState.tsx +15 -0
  174. package/src/upstream-exports/react-stately/useMenuTriggerState.tsx +26 -0
  175. package/src/upstream-exports/react-stately/useMultipleSelectionState.tsx +21 -0
  176. package/src/upstream-exports/react-stately/useNumberFieldState.tsx +20 -0
  177. package/src/upstream-exports/react-stately/useOverlayTriggerState.tsx +20 -0
  178. package/src/upstream-exports/react-stately/useRadioGroupState.tsx +16 -0
  179. package/src/upstream-exports/react-stately/useSearchFieldState.tsx +19 -0
  180. package/src/upstream-exports/react-stately/useSliderState.tsx +20 -0
  181. package/src/upstream-exports/react-stately/useTableState.tsx +45 -0
  182. package/src/upstream-exports/react-stately/useTokenFieldState.tsx +25 -0
  183. package/src/upstream-exports/react-stately/useTooltipTriggerState.tsx +20 -0
  184. package/src/upstream-exports/react-stately/useTreeState.tsx +14 -0
  185. package/src/utils/domHelpers.ts +74 -17
  186. package/src/utils/isVirtualEvent.ts +2 -1
  187. package/src/utils/openLink.ts +8 -3
  188. package/src/utils/platform.ts +7 -3
  189. package/src/utils/scrollIntoView.ts +4 -4
  190. package/src/utils/shadowdom/DOMFunctions.ts +42 -2
  191. package/src/utils/useId.ts +11 -2
  192. package/src/utils/useLoadMoreSentinel.ts +8 -3
  193. package/src/utils/useViewportSize.ts +3 -3
  194. package/src/virtualizer/ScrollView.tsx +13 -4
  195. package/src/virtualizer/VirtualizerItem.tsx +3 -1
  196. package/src/virtualizer/useVirtualizerItem.tsx +43 -3
  197. package/src/menu/useMenuTrigger.ts +0 -189
  198. package/src/searchfield/useSearchField.ts +0 -156
  199. package/src/selection/useSelectableCollection.ts +0 -750
  200. package/src/spinbutton/useSpinButton.ts +0 -380
  201. package/src/table/useTableColumnResize.ts +0 -374
@@ -0,0 +1,751 @@
1
+ /** @jsxImportSource octane */
2
+ import type { BaseEvent } from '../interactions/createEventHandler';
3
+ type KeyboardEvent = BaseEvent<globalThis.KeyboardEvent>;
4
+ type DOMAttributes = Record<string, any>;
5
+ // Ported from adobe/react-spectrum@5ecb3333001313e83898cd07644227897e3bae1f (packages/react-aria/src/selection/useSelectableCollection.ts).
6
+ /*
7
+ * Copyright 2020 Adobe. All rights reserved.
8
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License. You may obtain a copy
10
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software distributed under
13
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
14
+ * OF ANY KIND, either express or implied. See the License for the specific language
15
+ * governing permissions and limitations under the License.
16
+ */
17
+
18
+ import { CLEAR_FOCUS_EVENT, FOCUS_EVENT } from '../utils/constants';
19
+
20
+ import { dispatchVirtualFocus, moveVirtualFocus } from '../focus/virtualFocus';
21
+ import {
22
+ FocusableElement,
23
+ FocusStrategy,
24
+ Key,
25
+ KeyboardDelegate,
26
+ RefObject,
27
+ } from '@react-types/shared';
28
+ import { flushSync } from 'octane';
29
+ import { FocusEvent, useEffect, useRef } from '../compat/react';
30
+ import { focusSafely } from '../interactions/focusSafely';
31
+ import { focusWithoutScrolling } from '../utils/focusWithoutScrolling';
32
+ import {
33
+ getActiveElement,
34
+ getEventTarget,
35
+ isFocusWithin,
36
+ nodeContains,
37
+ } from '../utils/shadowdom/DOMFunctions';
38
+ import { getFocusableTreeWalker } from '../focus/FocusScope';
39
+ import { getInteractionModality } from '../interactions/useFocusVisible';
40
+ import { getItemElement, isNonContiguousSelectionModifier, useCollectionId } from './utils';
41
+ import { isCtrlKeyPressed } from '../utils/keyboard';
42
+ import { isMac } from '../utils/platform';
43
+ import { isTabbable } from '../utils/isFocusable';
44
+ import { mergeProps } from '../utils/mergeProps';
45
+ import { MultipleSelectionManager } from '../upstream-exports/react-stately/useMultipleSelectionState';
46
+ import { scrollIntoView, scrollIntoViewport } from '../utils/scrollIntoView';
47
+ import { useEvent } from '../utils/useEvent';
48
+ import { useKeyboard } from '../interactions/useKeyboard';
49
+ import { useLocale } from '../i18n/I18nProvider';
50
+ import { useRouter } from '../utils/openLink';
51
+ import { useTypeSelect } from './useTypeSelect';
52
+ import { useUpdateLayoutEffect } from '../utils/useUpdateLayoutEffect';
53
+
54
+ export interface AriaSelectableCollectionOptions {
55
+ /**
56
+ * An interface for reading and updating multiple selection state.
57
+ */
58
+ selectionManager: MultipleSelectionManager;
59
+ /**
60
+ * A delegate object that implements behavior for keyboard focus movement.
61
+ */
62
+ keyboardDelegate: KeyboardDelegate;
63
+ /**
64
+ * The ref attached to the element representing the collection.
65
+ */
66
+ ref: RefObject<HTMLElement | null>;
67
+ /**
68
+ * Whether the collection or one of its items should be automatically focused upon render.
69
+ *
70
+ * @default false
71
+ */
72
+ autoFocus?: boolean | FocusStrategy;
73
+ /**
74
+ * Whether focus should wrap around when the end/start is reached.
75
+ *
76
+ * @default false
77
+ */
78
+ shouldFocusWrap?: boolean;
79
+ /**
80
+ * Whether the collection allows empty selection.
81
+ *
82
+ * @default false
83
+ */
84
+ disallowEmptySelection?: boolean;
85
+ /**
86
+ * Whether the collection allows the user to select all items via keyboard shortcut.
87
+ *
88
+ * @default false
89
+ */
90
+ disallowSelectAll?: boolean;
91
+ /**
92
+ * Whether pressing the Escape should clear selection in the collection or not.
93
+ *
94
+ * @default 'clearSelection'
95
+ */
96
+ escapeKeyBehavior?: 'clearSelection' | 'none';
97
+ /**
98
+ * Whether selection should occur automatically on focus.
99
+ *
100
+ * @default false
101
+ */
102
+ selectOnFocus?: boolean;
103
+ /**
104
+ * Whether typeahead is disabled.
105
+ *
106
+ * @default false
107
+ */
108
+ disallowTypeAhead?: boolean;
109
+ /**
110
+ * Whether the collection items should use virtual focus instead of being focused directly.
111
+ */
112
+ shouldUseVirtualFocus?: boolean;
113
+ /**
114
+ * Whether navigation through tab key is enabled.
115
+ */
116
+ allowsTabNavigation?: boolean;
117
+ /**
118
+ * Whether the collection items are contained in a virtual scroller.
119
+ */
120
+ isVirtualized?: boolean;
121
+ /**
122
+ * The ref attached to the scrollable body. Used to provide automatic scrolling on item focus for
123
+ * non-virtualized collections. If not provided, defaults to the collection ref.
124
+ */
125
+ scrollRef?: RefObject<HTMLElement | null>;
126
+ /**
127
+ * The behavior of links in the collection.
128
+ * - 'action': link behaves like onAction.
129
+ * - 'selection': link follows selection interactions (e.g. if URL drives selection).
130
+ * - 'override': links override all other interactions (link items are not selectable).
131
+ *
132
+ * @default 'action'
133
+ */
134
+ linkBehavior?: 'action' | 'selection' | 'override';
135
+ /**
136
+ * Which item in the collection to focus when tabbing into the collection. Overrides default
137
+ * roving tab index like behavior.
138
+ *
139
+ * @private
140
+ */
141
+ UNSTABLE_focusOnEntry?: 'first' | 'last';
142
+ }
143
+
144
+ export interface SelectableCollectionAria {
145
+ /** Props for the collection element. */
146
+ collectionProps: DOMAttributes;
147
+ }
148
+
149
+ /**
150
+ * Handles interactions with selectable collections.
151
+ */
152
+ export function useSelectableCollection(
153
+ options: AriaSelectableCollectionOptions,
154
+ _slot?: symbol,
155
+ ): SelectableCollectionAria {
156
+ let {
157
+ selectionManager: manager,
158
+ keyboardDelegate: delegate,
159
+ ref,
160
+ autoFocus = false,
161
+ shouldFocusWrap = false,
162
+ disallowEmptySelection = false,
163
+ disallowSelectAll = false,
164
+ escapeKeyBehavior = 'clearSelection',
165
+ selectOnFocus = manager.selectionBehavior === 'replace',
166
+ disallowTypeAhead = false,
167
+ shouldUseVirtualFocus,
168
+ allowsTabNavigation = false,
169
+ // If no scrollRef is provided, assume the collection ref is the scrollable region
170
+ scrollRef = ref,
171
+ linkBehavior = 'action',
172
+ UNSTABLE_focusOnEntry,
173
+ } = options;
174
+ let { direction } = useLocale();
175
+ let router = useRouter();
176
+
177
+ const navigateToKey = (
178
+ e: KeyboardEvent,
179
+ key: Key | undefined,
180
+ childFocus?: FocusStrategy,
181
+ ): boolean | void => {
182
+ if (key != null) {
183
+ if (
184
+ manager.isLink(key) &&
185
+ linkBehavior === 'selection' &&
186
+ selectOnFocus &&
187
+ !isNonContiguousSelectionModifier(e)
188
+ ) {
189
+ // Set focused key and re-render synchronously to bring item into view if needed.
190
+ flushSync(() => {
191
+ manager.setFocusedKey(key, childFocus);
192
+ });
193
+
194
+ let item = getItemElement(ref, key);
195
+ let itemProps = manager.getItemProps(key);
196
+ if (item) {
197
+ router.open(item, e, itemProps.href, itemProps.routerOptions);
198
+ return;
199
+ }
200
+
201
+ return false;
202
+ }
203
+
204
+ manager.setFocusedKey(key, childFocus);
205
+
206
+ if (manager.isLink(key) && linkBehavior === 'override') {
207
+ return false;
208
+ }
209
+
210
+ if (e.shiftKey && manager.selectionMode === 'multiple') {
211
+ manager.extendSelection(key);
212
+ return;
213
+ } else if (selectOnFocus && !isNonContiguousSelectionModifier(e)) {
214
+ manager.replaceSelection(key);
215
+ return;
216
+ }
217
+ }
218
+ return false;
219
+ };
220
+
221
+ let arrowDown = (e: KeyboardEvent) => {
222
+ if (delegate.getKeyBelow) {
223
+ let nextKey =
224
+ manager.focusedKey != null
225
+ ? delegate.getKeyBelow?.(manager.focusedKey)
226
+ : delegate.getFirstKey?.();
227
+ if (nextKey == null && shouldFocusWrap) {
228
+ nextKey = delegate.getFirstKey?.(manager.focusedKey);
229
+ }
230
+ if (nextKey != null) {
231
+ navigateToKey(e, nextKey);
232
+ return;
233
+ }
234
+ }
235
+ return false;
236
+ };
237
+
238
+ let arrowUp = (e: KeyboardEvent) => {
239
+ if (delegate.getKeyAbove) {
240
+ let nextKey =
241
+ manager.focusedKey != null
242
+ ? delegate.getKeyAbove?.(manager.focusedKey)
243
+ : delegate.getLastKey?.();
244
+ if (nextKey == null && shouldFocusWrap) {
245
+ nextKey = delegate.getLastKey?.(manager.focusedKey);
246
+ }
247
+ if (nextKey != null) {
248
+ navigateToKey(e, nextKey);
249
+ return;
250
+ }
251
+ }
252
+ return false;
253
+ };
254
+
255
+ let home = (e: KeyboardEvent) => {
256
+ if (delegate.getFirstKey) {
257
+ if (manager.focusedKey === null && e.shiftKey) {
258
+ return false;
259
+ }
260
+ // TODO: should Home and End also be reversed in column reverse aka Home goes to top? Or should Home always to to the "first" (bottom)
261
+ let firstKey: Key | null = delegate.getFirstKey(manager.focusedKey, isCtrlKeyPressed(e));
262
+ manager.setFocusedKey(firstKey);
263
+ if (firstKey != null) {
264
+ if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {
265
+ manager.extendSelection(firstKey);
266
+ return;
267
+ } else if (selectOnFocus) {
268
+ manager.replaceSelection(firstKey);
269
+ return;
270
+ }
271
+ }
272
+ }
273
+ return false;
274
+ };
275
+
276
+ let arrowLeft = (e: KeyboardEvent) => {
277
+ if (delegate.getKeyLeftOf) {
278
+ let nextKey: Key | undefined | null =
279
+ manager.focusedKey != null
280
+ ? delegate.getKeyLeftOf?.(manager.focusedKey)
281
+ : delegate.getFirstKey?.();
282
+ if (nextKey == null && shouldFocusWrap) {
283
+ nextKey =
284
+ direction === 'rtl'
285
+ ? delegate.getFirstKey?.(manager.focusedKey)
286
+ : delegate.getLastKey?.(manager.focusedKey);
287
+ }
288
+ if (nextKey != null) {
289
+ navigateToKey(e, nextKey, direction === 'rtl' ? 'first' : 'last');
290
+ return;
291
+ }
292
+ }
293
+ return false;
294
+ };
295
+
296
+ let arrowRight = (e: KeyboardEvent) => {
297
+ if (delegate.getKeyRightOf) {
298
+ let nextKey: Key | undefined | null =
299
+ manager.focusedKey != null
300
+ ? delegate.getKeyRightOf?.(manager.focusedKey)
301
+ : delegate.getFirstKey?.();
302
+ if (nextKey == null && shouldFocusWrap) {
303
+ nextKey =
304
+ direction === 'rtl'
305
+ ? delegate.getLastKey?.(manager.focusedKey)
306
+ : delegate.getFirstKey?.(manager.focusedKey);
307
+ }
308
+ if (nextKey != null) {
309
+ navigateToKey(e, nextKey, direction === 'rtl' ? 'last' : 'first');
310
+ return;
311
+ }
312
+ }
313
+ return false;
314
+ };
315
+
316
+ let end = (e: KeyboardEvent) => {
317
+ if (delegate.getLastKey) {
318
+ if (manager.focusedKey === null && e.shiftKey) {
319
+ return false;
320
+ }
321
+ let lastKey = delegate.getLastKey(manager.focusedKey, isCtrlKeyPressed(e));
322
+ manager.setFocusedKey(lastKey);
323
+ if (lastKey != null) {
324
+ if (isCtrlKeyPressed(e) && e.shiftKey && manager.selectionMode === 'multiple') {
325
+ manager.extendSelection(lastKey);
326
+ return;
327
+ } else if (selectOnFocus) {
328
+ manager.replaceSelection(lastKey);
329
+ return;
330
+ }
331
+ }
332
+ }
333
+ return false;
334
+ };
335
+
336
+ let pageDown = (e: KeyboardEvent) => {
337
+ if (delegate.getKeyPageBelow && manager.focusedKey != null) {
338
+ let nextKey = delegate.getKeyPageBelow(manager.focusedKey);
339
+ if (nextKey != null) {
340
+ return navigateToKey(e, nextKey);
341
+ }
342
+ }
343
+ return false;
344
+ };
345
+
346
+ let pageUp = (e: KeyboardEvent) => {
347
+ if (delegate.getKeyPageAbove && manager.focusedKey != null) {
348
+ let nextKey = delegate.getKeyPageAbove(manager.focusedKey);
349
+ if (nextKey != null) {
350
+ return navigateToKey(e, nextKey);
351
+ }
352
+ }
353
+ return false;
354
+ };
355
+
356
+ let aHandler = () => {
357
+ if (manager.selectionMode === 'multiple' && disallowSelectAll !== true) {
358
+ manager.selectAll();
359
+ return;
360
+ }
361
+ return false;
362
+ };
363
+
364
+ let escape = () => {
365
+ if (
366
+ escapeKeyBehavior === 'clearSelection' &&
367
+ !disallowEmptySelection &&
368
+ manager.selectedKeys.size !== 0
369
+ ) {
370
+ manager.clearSelection();
371
+ return;
372
+ }
373
+ return false;
374
+ };
375
+
376
+ let tab = () => {
377
+ if (!allowsTabNavigation && ref.current) {
378
+ // There may be elements that are "tabbable" inside a collection (e.g. in a grid cell).
379
+ // However, collections should be treated as a single tab stop, with arrow key navigation internally.
380
+ // We don't control the rendering of these, so we can't override the tabIndex to prevent tabbing.
381
+ // Instead, we handle the Tab key, and move focus manually to the first/last tabbable element
382
+ // in the collection, so that the browser default behavior will apply starting from that element
383
+ // rather than the currently focused one.
384
+
385
+ let walker = getFocusableTreeWalker(ref.current, { tabbable: true });
386
+ let next: FocusableElement | undefined = undefined;
387
+ let last: FocusableElement;
388
+ do {
389
+ last = walker.lastChild() as FocusableElement;
390
+ if (last) {
391
+ next = last;
392
+ }
393
+ } while (last);
394
+
395
+ // 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
396
+ // the containing element. We need to special case this so that tab will move focus out of the grid instead of looping between
397
+ // focusing the containing cell and back to the non-tabbable child element
398
+ let activeElement = getActiveElement();
399
+ if (next && (!isFocusWithin(next) || (activeElement && !isTabbable(activeElement)))) {
400
+ focusWithoutScrolling(next);
401
+ }
402
+ }
403
+ return { shouldContinuePropagation: true, shouldPreventDefault: false };
404
+ };
405
+
406
+ let shiftTab = () => {
407
+ if (!allowsTabNavigation && ref.current) {
408
+ ref.current.focus();
409
+ }
410
+ return { shouldContinuePropagation: true, shouldPreventDefault: false };
411
+ };
412
+
413
+ let withShiftSel = (
414
+ key: string,
415
+ callback: import('../interactions/createKeyboardShortcutHandler').KeyboardShortcutAction,
416
+ ) => {
417
+ return {
418
+ [isMac() ? key + '+Shift+Alt' : key + '+Shift+Control']: callback,
419
+ [key + '+Shift']: callback,
420
+ [isMac() ? key + '+Alt' : key + '+Control']: callback,
421
+ [key]: callback,
422
+ };
423
+ };
424
+ // oxlint-disable react/react-compiler
425
+ let { keyboardProps: repeatKeyboardProps } = useKeyboard({
426
+ shortcuts: {
427
+ ...withShiftSel('ArrowDown', arrowDown),
428
+ ...withShiftSel('ArrowUp', arrowUp),
429
+ ...withShiftSel('ArrowLeft', arrowLeft),
430
+ ...withShiftSel('ArrowRight', arrowRight),
431
+ ...withShiftSel('PageDown', pageDown),
432
+ ...withShiftSel('PageUp', pageUp),
433
+ },
434
+ allowRepeats: true,
435
+ });
436
+ // oxlint-disable react/react-compiler
437
+ let { keyboardProps } = useKeyboard({
438
+ shortcuts: {
439
+ ...withShiftSel('Home', home),
440
+ ...withShiftSel('End', end),
441
+ 'Mod+A': aHandler,
442
+ Escape: escape,
443
+ Tab: tab,
444
+ 'Tab+Shift': shiftTab,
445
+ },
446
+ });
447
+ // oxlint-enable react/react-compiler
448
+
449
+ // Store the scroll position so we can restore it later.
450
+ /// TODO: should this happen all the time??
451
+ let scrollPos = useRef({ top: 0, left: 0 });
452
+ useEvent(scrollRef, 'scroll', () => {
453
+ scrollPos.current = {
454
+ top: scrollRef.current?.scrollTop ?? 0,
455
+ left: scrollRef.current?.scrollLeft ?? 0,
456
+ };
457
+ });
458
+
459
+ let onFocus = (e: FocusEvent) => {
460
+ if (manager.isFocused) {
461
+ // If a focus event bubbled through a portal, reset focus state.
462
+ if (!nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)) {
463
+ manager.setFocused(false);
464
+ }
465
+ return;
466
+ }
467
+
468
+ // Focus events can bubble through portals. Ignore these events.
469
+ if (!nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)) {
470
+ return;
471
+ }
472
+
473
+ let modality = getInteractionModality();
474
+ manager.setFocused(true);
475
+ let navigateToKey = (key: Key | undefined | null) => {
476
+ if (key != null) {
477
+ manager.setFocusedKey(key);
478
+ if (selectOnFocus && !manager.isSelected(key)) {
479
+ manager.replaceSelection(key);
480
+ }
481
+ }
482
+ };
483
+
484
+ // we need the "virtual" modality case checks here because shift tabbing from the prompt field's attachment card back into the
485
+ // thread is a virtual focus event (the tab handler in onKeyDown focuses the ref of the attachmentList aka TagGroup via a focus() call, hence the virtual modality)
486
+ if (UNSTABLE_focusOnEntry && (modality === 'keyboard' || modality === 'virtual')) {
487
+ // always go to the first item in the Thread when tabbing forwards/backwards into the collection
488
+ // since it is probably more important to the user to see the new prompt reply rather than go to the last focused key
489
+ navigateToKey(
490
+ UNSTABLE_focusOnEntry === 'first' ? delegate.getFirstKey?.() : delegate.getLastKey?.(),
491
+ );
492
+ } else if (manager.focusedKey == null) {
493
+ // If the user hasn't yet interacted with the collection, there will be no focusedKey set.
494
+ // Attempt to detect whether the user is tabbing forward or backward into the collection
495
+ // and either focus the first or last item accordingly.
496
+ let relatedTarget = e.relatedTarget as Element;
497
+ if (
498
+ relatedTarget &&
499
+ e.currentTarget.compareDocumentPosition(relatedTarget) & Node.DOCUMENT_POSITION_FOLLOWING
500
+ ) {
501
+ navigateToKey(manager.lastSelectedKey ?? delegate.getLastKey?.());
502
+ } else {
503
+ navigateToKey(manager.firstSelectedKey ?? delegate.getFirstKey?.());
504
+ }
505
+ } else if (scrollRef.current) {
506
+ // Restore the scroll position to what it was before.
507
+ scrollRef.current.scrollTop = scrollPos.current.top;
508
+ scrollRef.current.scrollLeft = scrollPos.current.left;
509
+ }
510
+
511
+ if (manager.focusedKey != null && scrollRef.current) {
512
+ // Refocus and scroll the focused item into view if it exists within the scrollable region.
513
+ let element = getItemElement(ref, manager.focusedKey);
514
+ if (element instanceof HTMLElement) {
515
+ // This prevents a flash of focus on the first/last element in the collection, or the collection itself.
516
+ if (!isFocusWithin(element) && !shouldUseVirtualFocus) {
517
+ focusWithoutScrolling(element);
518
+ }
519
+
520
+ if (modality === 'keyboard' || (UNSTABLE_focusOnEntry && modality === 'virtual')) {
521
+ scrollIntoViewport(element, { containingElement: ref.current });
522
+ }
523
+ }
524
+ }
525
+ };
526
+
527
+ let onBlur = (e: globalThis.FocusEvent) => {
528
+ // Don't set blurred and then focused again if moving focus within the collection.
529
+ if (!nodeContains(e.currentTarget as Element, e.relatedTarget as HTMLElement)) {
530
+ manager.setFocused(false);
531
+ }
532
+ };
533
+
534
+ // Ref to track whether the first item in the collection should be automatically focused. Specifically used for autocomplete when user types
535
+ // to focus the first key AFTER the collection updates.
536
+ // TODO: potentially expand the usage of this
537
+ let shouldVirtualFocusFirst = useRef(false);
538
+ // Add event listeners for custom virtual events. These handle updating the focused key in response to various keyboard events
539
+ // at the autocomplete level
540
+ // TODO: fix type later
541
+ useEvent(
542
+ ref,
543
+ FOCUS_EVENT,
544
+ !shouldUseVirtualFocus
545
+ ? undefined
546
+ : (e: any) => {
547
+ let { detail } = e;
548
+ e.stopPropagation();
549
+ manager.setFocused(true);
550
+ // If the user is typing forwards, autofocus the first option in the list.
551
+ if (detail?.focusStrategy === 'first') {
552
+ shouldVirtualFocusFirst.current = true;
553
+ }
554
+ },
555
+ );
556
+
557
+ // update active descendant
558
+ let firstKey = delegate.getFirstKey?.() ?? null;
559
+ useUpdateLayoutEffect(() => {
560
+ if (shouldVirtualFocusFirst.current) {
561
+ // If no focusable items exist in the list, make sure to clear any activedescendant that may still exist and move focus back to
562
+ // the original active element (e.g. the autocomplete input)
563
+ if (firstKey == null) {
564
+ let previousActiveElement = getActiveElement();
565
+ moveVirtualFocus(ref.current);
566
+ dispatchVirtualFocus(previousActiveElement!, null);
567
+
568
+ // 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.
569
+ // Reset shouldVirtualFocusFirst so that we don't erronously autofocus an item when the collection is filtered again.
570
+ if (manager.collection.size > 0) {
571
+ shouldVirtualFocusFirst.current = false;
572
+ }
573
+ } else {
574
+ manager.setFocusedKey(firstKey);
575
+ // Only set shouldVirtualFocusFirst to false if we've successfully set the first key as the focused key
576
+ // 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
577
+ // after the collection updates after load
578
+ shouldVirtualFocusFirst.current = false;
579
+ }
580
+ }
581
+ }, [firstKey, manager.collection.size]);
582
+
583
+ // reset focus first flag
584
+ useUpdateLayoutEffect(() => {
585
+ // If user causes the focused key to change in any other way, clear shouldVirtualFocusFirst so we don't
586
+ // accidentally move focus from under them. Skip this if the collection was empty because we might be in a load
587
+ // state and will still want to focus the first item after load
588
+ if (manager.collection.size > 0) {
589
+ shouldVirtualFocusFirst.current = false;
590
+ }
591
+ }, [manager.focusedKey]);
592
+
593
+ useEvent(
594
+ ref,
595
+ CLEAR_FOCUS_EVENT,
596
+ !shouldUseVirtualFocus
597
+ ? undefined
598
+ : (e: any) => {
599
+ e.stopPropagation();
600
+ manager.setFocused(false);
601
+ if (e.detail?.clearFocusKey) {
602
+ manager.setFocusedKey(null);
603
+ }
604
+ },
605
+ );
606
+
607
+ const autoFocusRef = useRef(autoFocus);
608
+ const didAutoFocusRef = useRef(false);
609
+ useEffect(() => {
610
+ if (autoFocusRef.current) {
611
+ let focusedKey: Key | null = null;
612
+
613
+ // Check focus strategy to determine which item to focus
614
+ if (autoFocus === 'first') {
615
+ focusedKey = delegate.getFirstKey?.() ?? null;
616
+ }
617
+ if (autoFocus === 'last') {
618
+ focusedKey = delegate.getLastKey?.() ?? null;
619
+ }
620
+
621
+ // If there are any selected keys, make the first one the new focus target
622
+ let selectedKeys = manager.selectedKeys;
623
+ if (selectedKeys.size) {
624
+ for (let key of selectedKeys) {
625
+ if (manager.canSelectItem(key)) {
626
+ focusedKey = key;
627
+ break;
628
+ }
629
+ }
630
+ }
631
+
632
+ manager.setFocused(true);
633
+ manager.setFocusedKey(focusedKey);
634
+
635
+ // If no default focus key is selected, focus the collection itself.
636
+ if (focusedKey == null && !shouldUseVirtualFocus && ref.current) {
637
+ focusSafely(ref.current);
638
+ }
639
+
640
+ // Wait until the collection has items to autofocus.
641
+ if (manager.collection.size > 0) {
642
+ autoFocusRef.current = false;
643
+ didAutoFocusRef.current = true;
644
+ }
645
+ }
646
+ });
647
+
648
+ // Scroll the focused element into view when the focusedKey changes.
649
+ let lastFocusedKey = useRef<typeof manager.focusedKey>(manager.focusedKey);
650
+ let raf = useRef<number | null>(null);
651
+ useEffect(() => {
652
+ if (
653
+ manager.isFocused &&
654
+ manager.focusedKey != null &&
655
+ (manager.focusedKey !== lastFocusedKey.current || didAutoFocusRef.current) &&
656
+ scrollRef.current &&
657
+ ref.current
658
+ ) {
659
+ let modality = getInteractionModality();
660
+ let element = getItemElement(ref, manager.focusedKey);
661
+ if (!(element instanceof HTMLElement)) {
662
+ // If item element wasn't found, return early (don't update autoFocusRef and lastFocusedKey).
663
+ // The collection may initially be empty (e.g. virtualizer), so wait until the element exists.
664
+ return;
665
+ }
666
+
667
+ if (modality === 'keyboard' || didAutoFocusRef.current) {
668
+ if (raf.current) {
669
+ cancelAnimationFrame(raf.current);
670
+ }
671
+
672
+ raf.current = requestAnimationFrame(() => {
673
+ if (scrollRef.current) {
674
+ scrollIntoView(scrollRef.current, element);
675
+ // Avoid scroll in iOS VO, since it may cause overlay to close (i.e. RAC submenu)
676
+ if (modality !== 'virtual') {
677
+ scrollIntoViewport(element, { containingElement: ref.current });
678
+ }
679
+ }
680
+ });
681
+ }
682
+ }
683
+
684
+ // If the focused key becomes null (e.g. the last item is deleted), focus the whole collection.
685
+ if (
686
+ !shouldUseVirtualFocus &&
687
+ manager.isFocused &&
688
+ manager.focusedKey == null &&
689
+ lastFocusedKey.current != null &&
690
+ ref.current
691
+ ) {
692
+ focusSafely(ref.current);
693
+ }
694
+
695
+ lastFocusedKey.current = manager.focusedKey;
696
+ didAutoFocusRef.current = false;
697
+ });
698
+
699
+ useEffect(() => {
700
+ return () => {
701
+ if (raf.current) {
702
+ cancelAnimationFrame(raf.current);
703
+ }
704
+ };
705
+ }, []);
706
+
707
+ // Intercept FocusScope restoration since virtualized collections can reuse DOM nodes.
708
+ useEvent(ref, 'react-aria-focus-scope-restore', (e) => {
709
+ e.preventDefault();
710
+ manager.setFocused(true);
711
+ });
712
+
713
+ let handlers = {
714
+ ...mergeProps(keyboardProps, repeatKeyboardProps),
715
+ onFocus,
716
+ onBlur,
717
+ onMouseDown(e: MouseEvent) {
718
+ // Ignore events that bubbled through portals.
719
+ if (scrollRef.current === (getEventTarget(e) as Element)) {
720
+ // Prevent focus going to the collection when clicking on the scrollbar.
721
+ e.preventDefault();
722
+ }
723
+ },
724
+ };
725
+
726
+ let { typeSelectProps } = useTypeSelect({
727
+ keyboardDelegate: delegate,
728
+ selectionManager: manager,
729
+ });
730
+
731
+ if (!disallowTypeAhead) {
732
+ // oxlint-disable-next-line react/react-compiler
733
+ handlers = mergeProps(typeSelectProps, handlers);
734
+ }
735
+
736
+ // If nothing is focused within the collection, make the collection itself tabbable.
737
+ // This will be marshalled to either the first or last item depending on where focus came from.
738
+ let tabIndex: number | undefined = undefined;
739
+ if (!shouldUseVirtualFocus) {
740
+ tabIndex = manager.focusedKey == null ? 0 : -1;
741
+ }
742
+
743
+ let collectionId = useCollectionId(manager.collection);
744
+ return {
745
+ // oxlint-disable-next-line react/react-compiler
746
+ collectionProps: mergeProps(handlers, {
747
+ tabIndex,
748
+ 'data-collection': collectionId,
749
+ }),
750
+ };
751
+ }