@octanejs/aria 0.0.5 → 0.0.7

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 (124) hide show
  1. package/package.json +7 -7
  2. package/src/components/Table.ts +2618 -0
  3. package/src/components/Tree.ts +1450 -0
  4. package/src/components/TreeDropTargetDelegate.ts +363 -0
  5. package/src/components/index.ts +57 -0
  6. package/src/components/utils.ts +3 -6
  7. package/src/grid/GridKeyboardDelegate.ts +514 -0
  8. package/src/grid/useGrid.ts +263 -0
  9. package/src/grid/useGridCell.ts +346 -0
  10. package/src/grid/useGridRow.ts +108 -0
  11. package/src/grid/useGridRowGroup.ts +29 -0
  12. package/src/grid/utils.ts +24 -0
  13. package/src/index.ts +38 -0
  14. package/src/intl/autocomplete/index.ts +9 -2
  15. package/src/intl/breadcrumbs/index.ts +9 -2
  16. package/src/intl/combobox/index.ts +9 -2
  17. package/src/intl/compileMessages.ts +356 -0
  18. package/src/intl/grid/index.ts +9 -2
  19. package/src/intl/gridlist/index.ts +9 -2
  20. package/src/intl/menu/index.ts +9 -2
  21. package/src/intl/numberfield/index.ts +9 -2
  22. package/src/intl/overlays/index.ts +9 -2
  23. package/src/intl/searchfield/index.ts +9 -2
  24. package/src/intl/spinbutton/index.ts +9 -2
  25. package/src/intl/table/ar-AE.json +13 -0
  26. package/src/intl/table/bg-BG.json +13 -0
  27. package/src/intl/table/cs-CZ.json +13 -0
  28. package/src/intl/table/da-DK.json +13 -0
  29. package/src/intl/table/de-DE.json +13 -0
  30. package/src/intl/table/el-GR.json +13 -0
  31. package/src/intl/table/en-US.json +13 -0
  32. package/src/intl/table/es-ES.json +13 -0
  33. package/src/intl/table/et-EE.json +13 -0
  34. package/src/intl/table/fi-FI.json +13 -0
  35. package/src/intl/table/fr-FR.json +13 -0
  36. package/src/intl/table/he-IL.json +13 -0
  37. package/src/intl/table/hr-HR.json +13 -0
  38. package/src/intl/table/hu-HU.json +13 -0
  39. package/src/intl/table/index.ts +82 -0
  40. package/src/intl/table/it-IT.json +13 -0
  41. package/src/intl/table/ja-JP.json +13 -0
  42. package/src/intl/table/ko-KR.json +13 -0
  43. package/src/intl/table/lt-LT.json +13 -0
  44. package/src/intl/table/lv-LV.json +13 -0
  45. package/src/intl/table/nb-NO.json +13 -0
  46. package/src/intl/table/nl-NL.json +13 -0
  47. package/src/intl/table/pl-PL.json +13 -0
  48. package/src/intl/table/pt-BR.json +13 -0
  49. package/src/intl/table/pt-PT.json +13 -0
  50. package/src/intl/table/ro-RO.json +13 -0
  51. package/src/intl/table/ru-RU.json +13 -0
  52. package/src/intl/table/sk-SK.json +13 -0
  53. package/src/intl/table/sl-SI.json +13 -0
  54. package/src/intl/table/sr-SP.json +13 -0
  55. package/src/intl/table/sv-SE.json +13 -0
  56. package/src/intl/table/tr-TR.json +13 -0
  57. package/src/intl/table/uk-UA.json +13 -0
  58. package/src/intl/table/zh-CN.json +13 -0
  59. package/src/intl/table/zh-TW.json +13 -0
  60. package/src/intl/tag/index.ts +9 -2
  61. package/src/intl/tree/ar-AE.json +4 -0
  62. package/src/intl/tree/bg-BG.json +4 -0
  63. package/src/intl/tree/cs-CZ.json +4 -0
  64. package/src/intl/tree/da-DK.json +4 -0
  65. package/src/intl/tree/de-DE.json +4 -0
  66. package/src/intl/tree/el-GR.json +4 -0
  67. package/src/intl/tree/en-US.json +4 -0
  68. package/src/intl/tree/es-ES.json +4 -0
  69. package/src/intl/tree/et-EE.json +4 -0
  70. package/src/intl/tree/fi-FI.json +4 -0
  71. package/src/intl/tree/fr-FR.json +4 -0
  72. package/src/intl/tree/he-IL.json +4 -0
  73. package/src/intl/tree/hr-HR.json +4 -0
  74. package/src/intl/tree/hu-HU.json +4 -0
  75. package/src/intl/tree/index.ts +82 -0
  76. package/src/intl/tree/it-IT.json +4 -0
  77. package/src/intl/tree/ja-JP.json +4 -0
  78. package/src/intl/tree/ko-KR.json +4 -0
  79. package/src/intl/tree/lt-LT.json +4 -0
  80. package/src/intl/tree/lv-LV.json +4 -0
  81. package/src/intl/tree/nb-NO.json +4 -0
  82. package/src/intl/tree/nl-NL.json +4 -0
  83. package/src/intl/tree/pl-PL.json +4 -0
  84. package/src/intl/tree/pt-BR.json +4 -0
  85. package/src/intl/tree/pt-PT.json +4 -0
  86. package/src/intl/tree/ro-RO.json +4 -0
  87. package/src/intl/tree/ru-RU.json +4 -0
  88. package/src/intl/tree/sk-SK.json +4 -0
  89. package/src/intl/tree/sl-SI.json +4 -0
  90. package/src/intl/tree/sr-SP.json +4 -0
  91. package/src/intl/tree/sv-SE.json +4 -0
  92. package/src/intl/tree/tr-TR.json +4 -0
  93. package/src/intl/tree/uk-UA.json +4 -0
  94. package/src/intl/tree/zh-CN.json +4 -0
  95. package/src/intl/tree/zh-TW.json +4 -0
  96. package/src/stately/grid/GridCollection.ts +200 -0
  97. package/src/stately/grid/useGridState.ts +160 -0
  98. package/src/stately/index.ts +43 -0
  99. package/src/stately/table/Cell.ts +49 -0
  100. package/src/stately/table/Column.ts +121 -0
  101. package/src/stately/table/Row.ts +145 -0
  102. package/src/stately/table/TableBody.ts +66 -0
  103. package/src/stately/table/TableCollection.ts +390 -0
  104. package/src/stately/table/TableColumnLayout.ts +148 -0
  105. package/src/stately/table/TableHeader.ts +65 -0
  106. package/src/stately/table/TableUtils.ts +269 -0
  107. package/src/stately/table/useTableColumnResizeState.ts +202 -0
  108. package/src/stately/table/useTableState.ts +235 -0
  109. package/src/stately/table/useTreeGridState.ts +248 -0
  110. package/src/table/TableKeyboardDelegate.ts +205 -0
  111. package/src/table/useTable.ts +175 -0
  112. package/src/table/useTableCell.ts +81 -0
  113. package/src/table/useTableColumnHeader.ts +155 -0
  114. package/src/table/useTableColumnResize.ts +374 -0
  115. package/src/table/useTableHeaderRow.ts +57 -0
  116. package/src/table/useTableRow.ts +198 -0
  117. package/src/table/useTableRowGroup.ts +13 -0
  118. package/src/table/useTableSelectionCheckbox.ts +105 -0
  119. package/src/table/utils.ts +42 -0
  120. package/src/tree/useTree.ts +76 -0
  121. package/src/tree/useTreeItem.ts +109 -0
  122. package/src/utils/mergeRefs.ts +1 -4
  123. package/src/utils/useFormReset.ts +1 -2
  124. package/src/utils/useObjectRef.ts +2 -8
@@ -0,0 +1,263 @@
1
+ // Ported from react-aria (source: .react-spectrum/packages/react-aria/src/grid/useGrid.ts).
2
+ // octane adaptations:
3
+ // - Handlers receive NATIVE events: React's `FocusEventHandler` becomes a native
4
+ // FocusEvent handler and `e.currentTarget` casts to Element for the shadow-DOM
5
+ // helpers (octane's delegated dispatch guarantees the per-handler currentTarget).
6
+ // - `GridState`/`IGridCollection` come from the ported stately grid hooks;
7
+ // `DOMAttributes` is a local structural prop-bag alias (upstream's is typed over
8
+ // React's synthetic handlers).
9
+ // - Public-hook slot threading (splitSlot/subSlot); explicit dependency arrays are kept
10
+ // verbatim.
11
+ import type {
12
+ AriaLabelingProps,
13
+ DOMProps,
14
+ Key,
15
+ KeyboardDelegate,
16
+ RefObject,
17
+ } from '@react-types/shared';
18
+ import { filterDOMProps } from '../utils/filterDOMProps';
19
+ import { useCallback, useMemo } from 'octane';
20
+ import { getEventTarget, nodeContains } from '../utils/shadowdom/DOMFunctions';
21
+ import type { IGridCollection as GridCollection } from '../stately/grid/GridCollection';
22
+ import { GridKeyboardDelegate } from './GridKeyboardDelegate';
23
+ import { gridMap } from './utils';
24
+ import type { GridState } from '../stately/grid/useGridState';
25
+ import { mergeProps } from '../utils/mergeProps';
26
+ import { useCollator } from '../i18n/useCollator';
27
+ import { useGridSelectionAnnouncement } from './useGridSelectionAnnouncement';
28
+ import { useHasTabbableChild } from '../focus/useHasTabbableChild';
29
+ import { useHighlightSelectionDescription } from './useHighlightSelectionDescription';
30
+ import { useId } from '../utils/useId';
31
+ import { useLocale } from '../i18n/I18nProvider';
32
+ import { useSelectableCollection } from '../selection/useSelectableCollection';
33
+
34
+ import { S, splitSlot, subSlot } from '../internal';
35
+
36
+ // octane adaptation: minimal structural DOMAttributes (upstream's drags React's synthetic
37
+ // handler types along).
38
+ type DOMAttributes = Record<string, any>;
39
+
40
+ export interface GridProps extends DOMProps, AriaLabelingProps {
41
+ /** Whether the grid uses virtual scrolling. */
42
+ isVirtualized?: boolean;
43
+ /**
44
+ * Whether typeahead navigation is disabled.
45
+ *
46
+ * @default false
47
+ */
48
+ disallowTypeAhead?: boolean;
49
+ /**
50
+ * An optional keyboard delegate implementation for type to select,
51
+ * to override the default.
52
+ */
53
+ keyboardDelegate?: KeyboardDelegate;
54
+ /**
55
+ * Whether initial grid focus should be placed on the grid row or grid cell.
56
+ *
57
+ * @default 'row'
58
+ */
59
+ focusMode?: 'row' | 'cell';
60
+ /**
61
+ * A function that returns the text that should be announced by assistive technology when a row is
62
+ * added or removed from selection.
63
+ *
64
+ * @default (key) => state.collection.getItem(key)?.textValue
65
+ */
66
+ getRowText?: (key: Key) => string;
67
+ /**
68
+ * The ref attached to the scrollable body. Used to provided automatic scrolling on item focus for
69
+ * non-virtualized grids.
70
+ */
71
+ scrollRef?: RefObject<HTMLElement | null>;
72
+ /** Handler that is called when a user performs an action on the row. */
73
+ onRowAction?: (key: Key) => void;
74
+ /** Handler that is called when a user performs an action on the cell. */
75
+ onCellAction?: (key: Key) => void;
76
+ /**
77
+ * Whether pressing the escape key should clear selection in the grid or not.
78
+ *
79
+ * Most experiences should not modify this option as it eliminates a keyboard user's ability to
80
+ * easily clear selection. Only use if the escape key is being handled externally or should not
81
+ * trigger selection clearing contextually.
82
+ *
83
+ * @default 'clearSelection'
84
+ */
85
+ escapeKeyBehavior?: 'clearSelection' | 'none';
86
+ /** Whether selection should occur on press up instead of press down. */
87
+ shouldSelectOnPressUp?: boolean;
88
+ }
89
+
90
+ export interface GridAria {
91
+ /** Props for the grid element. */
92
+ gridProps: DOMAttributes;
93
+ }
94
+
95
+ /**
96
+ * Provides the behavior and accessibility implementation for a grid component. A grid displays data
97
+ * in one or more rows and columns and enables a user to navigate its contents via directional
98
+ * navigation keys.
99
+ *
100
+ * @param props - Props for the grid.
101
+ * @param state - State for the grid, as returned by `useGridState`.
102
+ * @param ref - The ref attached to the grid element.
103
+ */
104
+ export function useGrid<T>(
105
+ props: GridProps,
106
+ state: GridState<T, GridCollection<T>>,
107
+ ref: RefObject<HTMLElement | null>,
108
+ ): GridAria;
109
+ // Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
110
+ export function useGrid<T>(
111
+ props: GridProps,
112
+ state: GridState<T, GridCollection<T>>,
113
+ ref: RefObject<HTMLElement | null>,
114
+ slot: symbol | undefined,
115
+ ): GridAria;
116
+ export function useGrid(...args: any[]): GridAria {
117
+ const [user, slotArg] = splitSlot(args);
118
+ const slot = slotArg ?? S('useGrid');
119
+ const props = user[0] as GridProps;
120
+ const state = user[1] as GridState<any, GridCollection<any>>;
121
+ const ref = user[2] as RefObject<HTMLElement | null>;
122
+
123
+ let {
124
+ isVirtualized,
125
+ disallowTypeAhead,
126
+ keyboardDelegate,
127
+ focusMode,
128
+ scrollRef,
129
+ getRowText,
130
+ onRowAction,
131
+ onCellAction,
132
+ escapeKeyBehavior = 'clearSelection',
133
+ shouldSelectOnPressUp,
134
+ } = props;
135
+ let { selectionManager: manager } = state;
136
+
137
+ if (!props['aria-label'] && !props['aria-labelledby']) {
138
+ console.warn('An aria-label or aria-labelledby prop is required for accessibility.');
139
+ }
140
+
141
+ // By default, a KeyboardDelegate is provided which uses the DOM to query layout information (e.g. for page up/page down).
142
+ // When virtualized, the layout object will be passed in as a prop and override this.
143
+ let collator = useCollator({ usage: 'search', sensitivity: 'base' }, subSlot(slot, 'collator'));
144
+ let { direction } = useLocale(subSlot(slot, 'locale'));
145
+ let disabledBehavior = state.selectionManager.disabledBehavior;
146
+ let delegate = useMemo(
147
+ () =>
148
+ keyboardDelegate ||
149
+ new GridKeyboardDelegate({
150
+ collection: state.collection,
151
+ disabledKeys: state.disabledKeys,
152
+ disabledBehavior,
153
+ ref,
154
+ direction,
155
+ collator,
156
+ focusMode,
157
+ }),
158
+ [
159
+ keyboardDelegate,
160
+ state.collection,
161
+ state.disabledKeys,
162
+ disabledBehavior,
163
+ ref,
164
+ direction,
165
+ collator,
166
+ focusMode,
167
+ ],
168
+ subSlot(slot, 'delegate'),
169
+ );
170
+
171
+ let { collectionProps } = useSelectableCollection(
172
+ {
173
+ ref,
174
+ selectionManager: manager,
175
+ keyboardDelegate: delegate,
176
+ isVirtualized,
177
+ scrollRef,
178
+ disallowTypeAhead,
179
+ escapeKeyBehavior,
180
+ },
181
+ subSlot(slot, 'selectableCollection'),
182
+ );
183
+
184
+ let id = useId(props.id, subSlot(slot, 'id'));
185
+ gridMap.set(state, {
186
+ keyboardDelegate: delegate,
187
+ actions: { onRowAction, onCellAction },
188
+ shouldSelectOnPressUp,
189
+ });
190
+
191
+ let descriptionProps = useHighlightSelectionDescription(
192
+ {
193
+ selectionManager: manager,
194
+ hasItemActions: !!(onRowAction || onCellAction),
195
+ },
196
+ subSlot(slot, 'highlightDescription'),
197
+ );
198
+
199
+ let domProps = filterDOMProps(props, { labelable: true });
200
+
201
+ let onFocus = useCallback(
202
+ (e: FocusEvent) => {
203
+ if (manager.isFocused) {
204
+ // If a focus event bubbled through a portal, reset focus state.
205
+ if (!nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)) {
206
+ manager.setFocused(false);
207
+ }
208
+
209
+ return;
210
+ }
211
+
212
+ // Focus events can bubble through portals. Ignore these events.
213
+ if (!nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)) {
214
+ return;
215
+ }
216
+
217
+ manager.setFocused(true);
218
+ },
219
+ [manager],
220
+ subSlot(slot, 'onFocus'),
221
+ );
222
+
223
+ // Continue to track collection focused state even if keyboard navigation is disabled
224
+ let navDisabledHandlers = useMemo(
225
+ () => ({
226
+ onBlur: collectionProps.onBlur,
227
+ onFocus,
228
+ }),
229
+ [onFocus, collectionProps.onBlur],
230
+ subSlot(slot, 'navDisabled'),
231
+ );
232
+
233
+ let hasTabbableChild = useHasTabbableChild(
234
+ ref,
235
+ {
236
+ isDisabled: state.collection.size !== 0,
237
+ },
238
+ subSlot(slot, 'hasTabbableChild'),
239
+ );
240
+
241
+ let gridProps: DOMAttributes = mergeProps(
242
+ domProps,
243
+ {
244
+ role: 'grid',
245
+ id,
246
+ 'aria-multiselectable': manager.selectionMode === 'multiple' ? 'true' : undefined,
247
+ },
248
+ state.isKeyboardNavigationDisabled ? navDisabledHandlers : collectionProps,
249
+ // If collection is empty, make sure the grid is tabbable unless there is a child tabbable element.
250
+ (state.collection.size === 0 && { tabIndex: hasTabbableChild ? -1 : 0 }) || undefined,
251
+ descriptionProps,
252
+ );
253
+
254
+ if (isVirtualized) {
255
+ gridProps['aria-rowcount'] = state.collection.size;
256
+ gridProps['aria-colcount'] = state.collection.columnCount;
257
+ }
258
+
259
+ useGridSelectionAnnouncement({ getRowText }, state, subSlot(slot, 'selectionAnnouncement'));
260
+ return {
261
+ gridProps,
262
+ };
263
+ }
@@ -0,0 +1,346 @@
1
+ // Ported from react-aria (source: .react-spectrum/packages/react-aria/src/grid/useGridCell.ts).
2
+ // octane adaptations:
3
+ // - Handlers receive NATIVE events (there is no synthetic layer): keydown handlers take
4
+ // the native KeyboardEvent, and the ArrowUp/ArrowDown/edge re-dispatch builds
5
+ // `new KeyboardEvent(e.type, e)` — upstream's `e.nativeEvent` IS the event here.
6
+ // `onKeyDownCapture` maps to the capture phase natively in octane, matching
7
+ // upstream's React capture prop.
8
+ // - `GridState`/`IGridCollection`/`GridNode` come from the ported stately grid hooks;
9
+ // `DOMAttributes` is a local structural prop-bag alias.
10
+ // - Public-hook slot threading (splitSlot/subSlot).
11
+ import type { FocusableElement, Key, RefObject } from '@react-types/shared';
12
+ import { focusSafely } from '../interactions/focusSafely';
13
+ import {
14
+ getActiveElement,
15
+ getEventTarget,
16
+ isFocusWithin,
17
+ nodeContains,
18
+ } from '../utils/shadowdom/DOMFunctions';
19
+ import { getFocusableTreeWalker } from '../focus/FocusScope';
20
+ import { getScrollParent } from '../utils/getScrollParent';
21
+ import type { IGridCollection as GridCollection, GridNode } from '../stately/grid/GridCollection';
22
+ import { gridMap } from './utils';
23
+ import type { GridState } from '../stately/grid/useGridState';
24
+ import { isFocusVisible } from '../interactions/useFocusVisible';
25
+ import { mergeProps } from '../utils/mergeProps';
26
+ import { useRef } from 'octane';
27
+ import { scrollIntoViewport } from '../utils/scrollIntoView';
28
+ import { useLocale } from '../i18n/I18nProvider';
29
+ import { useSelectableItem } from '../selection/useSelectableItem';
30
+
31
+ import { S, splitSlot, subSlot } from '../internal';
32
+
33
+ // octane adaptation: minimal structural DOMAttributes (upstream's drags React's synthetic
34
+ // handler types along).
35
+ type DOMAttributes = Record<string, any>;
36
+
37
+ export interface GridCellProps {
38
+ /**
39
+ * An object representing the grid cell. Contains all the relevant information that makes up the
40
+ * grid cell.
41
+ */
42
+ node: GridNode<unknown>;
43
+ /** Whether the grid cell is contained in a virtual scroller. */
44
+ isVirtualized?: boolean;
45
+ /**
46
+ * Whether the cell or its first focusable child element should be focused when the grid cell is
47
+ * focused.
48
+ */
49
+ focusMode?: 'child' | 'cell';
50
+ /** Whether selection should occur on press up instead of press down. */
51
+ shouldSelectOnPressUp?: boolean;
52
+ /** Indicates how many columns the data cell spans. */
53
+ colSpan?: number;
54
+ /**
55
+ * Handler that is called when a user performs an action on the cell.
56
+ * Please use onCellAction at the collection level instead.
57
+ *
58
+ * @deprecated
59
+ */
60
+ onAction?: () => void;
61
+ }
62
+
63
+ export interface GridCellAria {
64
+ /** Props for the grid cell element. */
65
+ gridCellProps: DOMAttributes;
66
+ /** Whether the cell is currently in a pressed state. */
67
+ isPressed: boolean;
68
+ }
69
+
70
+ /**
71
+ * Provides the behavior and accessibility implementation for a cell in a grid.
72
+ *
73
+ * @param props - Props for the cell.
74
+ * @param state - State of the parent grid, as returned by `useGridState`.
75
+ */
76
+ export function useGridCell<T, C extends GridCollection<T>>(
77
+ props: GridCellProps,
78
+ state: GridState<T, C>,
79
+ ref: RefObject<FocusableElement | null>,
80
+ ): GridCellAria;
81
+ // Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
82
+ export function useGridCell<T, C extends GridCollection<T>>(
83
+ props: GridCellProps,
84
+ state: GridState<T, C>,
85
+ ref: RefObject<FocusableElement | null>,
86
+ slot: symbol | undefined,
87
+ ): GridCellAria;
88
+ export function useGridCell(...args: any[]): GridCellAria {
89
+ const [user, slotArg] = splitSlot(args);
90
+ const slot = slotArg ?? S('useGridCell');
91
+ const props = user[0] as GridCellProps;
92
+ const state = user[1] as GridState<any, GridCollection<any>>;
93
+ const ref = user[2] as RefObject<FocusableElement | null>;
94
+
95
+ let { node, isVirtualized, focusMode = 'child', shouldSelectOnPressUp, onAction } = props;
96
+
97
+ let { direction } = useLocale(subSlot(slot, 'locale'));
98
+ let {
99
+ keyboardDelegate,
100
+ actions: { onCellAction },
101
+ } = gridMap.get(state)!;
102
+
103
+ // We need to track the key of the item at the time it was last focused so that we force
104
+ // focus to go to the item when the DOM node is reused for a different item in a virtualizer.
105
+ let keyWhenFocused = useRef<Key | null>(null, subSlot(slot, 'keyWhenFocused'));
106
+
107
+ // Handles focusing the cell. If there is a focusable child,
108
+ // it is focused, otherwise the cell itself is focused.
109
+ let focus = () => {
110
+ if (ref.current) {
111
+ let treeWalker = getFocusableTreeWalker(ref.current);
112
+ if (focusMode === 'child') {
113
+ // If focus is already on a focusable child within the cell, early return so we don't shift focus
114
+ if (isFocusWithin(ref.current) && ref.current !== getActiveElement()) {
115
+ return;
116
+ }
117
+
118
+ let focusable =
119
+ state.selectionManager.childFocusStrategy === 'last'
120
+ ? last(treeWalker)
121
+ : (treeWalker.firstChild() as FocusableElement);
122
+ if (focusable) {
123
+ focusSafely(focusable);
124
+ return;
125
+ }
126
+ }
127
+
128
+ if (
129
+ (keyWhenFocused.current != null && node.key !== keyWhenFocused.current) ||
130
+ !isFocusWithin(ref.current)
131
+ ) {
132
+ focusSafely(ref.current);
133
+ }
134
+ }
135
+ };
136
+
137
+ let { itemProps, isPressed } = useSelectableItem(
138
+ {
139
+ selectionManager: state.selectionManager,
140
+ key: node.key,
141
+ ref,
142
+ isVirtualized,
143
+ focus,
144
+ shouldSelectOnPressUp,
145
+ onAction: onCellAction ? () => onCellAction(node.key) : onAction,
146
+ isDisabled: state.collection.size === 0,
147
+ },
148
+ subSlot(slot, 'selectableItem'),
149
+ );
150
+
151
+ let onKeyDownCapture = (e: KeyboardEvent) => {
152
+ let activeElement = getActiveElement();
153
+ if (
154
+ !nodeContains(e.currentTarget as Element, getEventTarget(e) as Element) ||
155
+ state.isKeyboardNavigationDisabled ||
156
+ !ref.current ||
157
+ !activeElement
158
+ ) {
159
+ return;
160
+ }
161
+
162
+ let walker = getFocusableTreeWalker(ref.current);
163
+ walker.currentNode = activeElement;
164
+
165
+ switch (e.key) {
166
+ case 'ArrowLeft': {
167
+ // Find the next focusable element within the cell.
168
+ let focusable: FocusableElement | null =
169
+ direction === 'rtl'
170
+ ? (walker.nextNode() as FocusableElement)
171
+ : (walker.previousNode() as FocusableElement);
172
+
173
+ // Don't focus the cell itself if focusMode is "child"
174
+ if (focusMode === 'child' && focusable === ref.current) {
175
+ focusable = null;
176
+ }
177
+
178
+ e.preventDefault();
179
+ e.stopPropagation();
180
+ if (focusable) {
181
+ focusSafely(focusable);
182
+ scrollIntoViewport(focusable, { containingElement: getScrollParent(ref.current) });
183
+ } else {
184
+ // If there is no next focusable child, then move to the next cell to the left of this one.
185
+ // This will be handled by useSelectableCollection. However, if there is no cell to the left
186
+ // of this one, only one column, and the grid doesn't focus rows, then the next key will be the
187
+ // same as this one. In that case we need to handle focusing either the cell or the first/last
188
+ // child, depending on the focus mode.
189
+ let prev = keyboardDelegate.getKeyLeftOf?.(node.key);
190
+ if (prev !== node.key) {
191
+ // We prevent the capturing event from reaching children of the cell, e.g. pickers.
192
+ // We want arrow keys to navigate to the next cell instead. We need to re-dispatch
193
+ // the event from a higher parent so it still bubbles and gets handled by useSelectableCollection.
194
+ // octane adaptation: the native event IS `e.nativeEvent`.
195
+ ref.current.parentElement?.dispatchEvent(new KeyboardEvent(e.type, e));
196
+ break;
197
+ }
198
+
199
+ if (focusMode === 'cell' && direction === 'rtl') {
200
+ focusSafely(ref.current);
201
+ scrollIntoViewport(ref.current, { containingElement: getScrollParent(ref.current) });
202
+ } else {
203
+ walker.currentNode = ref.current;
204
+ focusable =
205
+ direction === 'rtl' ? (walker.firstChild() as FocusableElement) : last(walker);
206
+ if (focusable) {
207
+ focusSafely(focusable);
208
+ scrollIntoViewport(focusable, { containingElement: getScrollParent(ref.current) });
209
+ }
210
+ }
211
+ }
212
+ break;
213
+ }
214
+ case 'ArrowRight': {
215
+ let focusable: FocusableElement | null =
216
+ direction === 'rtl'
217
+ ? (walker.previousNode() as FocusableElement)
218
+ : (walker.nextNode() as FocusableElement);
219
+
220
+ if (focusMode === 'child' && focusable === ref.current) {
221
+ focusable = null;
222
+ }
223
+
224
+ e.preventDefault();
225
+ e.stopPropagation();
226
+ if (focusable) {
227
+ focusSafely(focusable);
228
+ scrollIntoViewport(focusable, { containingElement: getScrollParent(ref.current) });
229
+ } else {
230
+ let next = keyboardDelegate.getKeyRightOf?.(node.key);
231
+ if (next !== node.key) {
232
+ // We prevent the capturing event from reaching children of the cell, e.g. pickers.
233
+ // We want arrow keys to navigate to the next cell instead. We need to re-dispatch
234
+ // the event from a higher parent so it still bubbles and gets handled by useSelectableCollection.
235
+ // octane adaptation: the native event IS `e.nativeEvent`.
236
+ ref.current.parentElement?.dispatchEvent(new KeyboardEvent(e.type, e));
237
+ break;
238
+ }
239
+
240
+ if (focusMode === 'cell' && direction === 'ltr') {
241
+ focusSafely(ref.current);
242
+ scrollIntoViewport(ref.current, { containingElement: getScrollParent(ref.current) });
243
+ } else {
244
+ walker.currentNode = ref.current;
245
+ focusable =
246
+ direction === 'rtl' ? last(walker) : (walker.firstChild() as FocusableElement);
247
+ if (focusable) {
248
+ focusSafely(focusable);
249
+ scrollIntoViewport(focusable, { containingElement: getScrollParent(ref.current) });
250
+ }
251
+ }
252
+ }
253
+ break;
254
+ }
255
+ case 'ArrowUp':
256
+ case 'ArrowDown':
257
+ // Prevent this event from reaching cell children, e.g. menu buttons. We want arrow keys to navigate
258
+ // to the cell above/below instead. We need to re-dispatch the event from a higher parent so it still
259
+ // bubbles and gets handled by useSelectableCollection.
260
+ if (!e.altKey && nodeContains(ref.current, getEventTarget(e) as Element)) {
261
+ e.stopPropagation();
262
+ e.preventDefault();
263
+ // octane adaptation: the native event IS `e.nativeEvent`.
264
+ ref.current.parentElement?.dispatchEvent(new KeyboardEvent(e.type, e));
265
+ }
266
+ break;
267
+ }
268
+ };
269
+
270
+ // Grid cells can have focusable elements inside them. In this case, focus should
271
+ // be marshalled to that element rather than focusing the cell itself.
272
+ let onFocus = (e: FocusEvent) => {
273
+ keyWhenFocused.current = node.key;
274
+ if (getEventTarget(e) !== ref.current) {
275
+ // useSelectableItem only handles setting the focused key when
276
+ // the focused element is the gridcell itself. We also want to
277
+ // set the focused key when a child element receives focus.
278
+ // If focus is currently visible (e.g. the user is navigating with the keyboard),
279
+ // then skip this. We want to restore focus to the previously focused row/cell
280
+ // in that case since the table should act like a single tab stop.
281
+ if (!isFocusVisible()) {
282
+ state.selectionManager.setFocusedKey(node.key);
283
+ }
284
+ return;
285
+ }
286
+
287
+ // If the cell itself is focused, wait a frame so that focus finishes propagatating
288
+ // up to the tree, and move focus to a focusable child if possible.
289
+ requestAnimationFrame(() => {
290
+ if (focusMode === 'child' && getActiveElement() === ref.current) {
291
+ focus();
292
+ }
293
+ });
294
+ };
295
+
296
+ let gridCellProps: DOMAttributes = mergeProps(itemProps, {
297
+ role: 'gridcell',
298
+ onKeyDownCapture,
299
+ 'aria-colspan': node.colSpan,
300
+ 'aria-colindex': node.colIndex != null ? node.colIndex + 1 : undefined, // aria-colindex is 1-based
301
+ colSpan: isVirtualized ? undefined : node.colSpan,
302
+ onFocus,
303
+ });
304
+
305
+ if (isVirtualized) {
306
+ gridCellProps['aria-colindex'] = (node.colIndex ?? node.index) + 1; // aria-colindex is 1-based
307
+ }
308
+
309
+ // When pressing with a pointer and cell selection is not enabled, usePress will be applied to the
310
+ // row rather than the cell. However, when the row is draggable, usePress cannot preventDefault
311
+ // on pointer down, so the browser will try to focus the cell which has a tabIndex applied.
312
+ // To avoid this, remove the tabIndex from the cell briefly on pointer down.
313
+ if (
314
+ shouldSelectOnPressUp &&
315
+ gridCellProps.tabIndex != null &&
316
+ gridCellProps.onPointerDown == null
317
+ ) {
318
+ gridCellProps.onPointerDown = (e: PointerEvent) => {
319
+ let el = e.currentTarget as Element;
320
+ let tabindex = el.getAttribute('tabindex');
321
+ el.removeAttribute('tabindex');
322
+ requestAnimationFrame(() => {
323
+ if (tabindex != null) {
324
+ el.setAttribute('tabindex', tabindex);
325
+ }
326
+ });
327
+ };
328
+ }
329
+
330
+ return {
331
+ gridCellProps,
332
+ isPressed,
333
+ };
334
+ }
335
+
336
+ function last(walker: TreeWalker) {
337
+ let next: FocusableElement | null = null;
338
+ let last: FocusableElement | null = null;
339
+ do {
340
+ last = walker.lastChild() as FocusableElement | null;
341
+ if (last) {
342
+ next = last;
343
+ }
344
+ } while (last);
345
+ return next;
346
+ }