@octanejs/aria 0.0.4 → 0.0.6

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 (210) hide show
  1. package/package.json +7 -5
  2. package/src/autocomplete/useAutocomplete.ts +656 -0
  3. package/src/collections/BaseCollection.ts +374 -0
  4. package/src/collections/CollectionBuilder.ts +343 -0
  5. package/src/collections/Document.ts +451 -0
  6. package/src/collections/Hidden.ts +84 -0
  7. package/src/collections/useCachedChildren.ts +103 -0
  8. package/src/components/Autocomplete.ts +124 -0
  9. package/src/components/Breadcrumbs.ts +170 -0
  10. package/src/components/Button.ts +209 -0
  11. package/src/components/Checkbox.ts +593 -0
  12. package/src/components/Collection.ts +301 -0
  13. package/src/components/ComboBox.ts +477 -0
  14. package/src/components/Dialog.ts +195 -0
  15. package/src/components/Disclosure.ts +322 -0
  16. package/src/components/DragAndDrop.ts +177 -0
  17. package/src/components/FieldError.ts +79 -0
  18. package/src/components/Form.ts +65 -0
  19. package/src/components/GridList.ts +1099 -0
  20. package/src/components/Group.ts +146 -0
  21. package/src/components/Header.ts +32 -0
  22. package/src/components/Heading.ts +45 -0
  23. package/src/components/Input.ts +139 -0
  24. package/src/components/Keyboard.ts +23 -0
  25. package/src/components/Label.ts +30 -0
  26. package/src/components/Link.ts +137 -0
  27. package/src/components/ListBox.ts +886 -0
  28. package/src/components/Menu.ts +685 -0
  29. package/src/components/Meter.ts +100 -0
  30. package/src/components/Modal.ts +352 -0
  31. package/src/components/NumberField.ts +221 -0
  32. package/src/components/OverlayArrow.ts +117 -0
  33. package/src/components/Popover.ts +385 -0
  34. package/src/components/ProgressBar.ts +112 -0
  35. package/src/components/RadioGroup.ts +542 -0
  36. package/src/components/SearchField.ts +208 -0
  37. package/src/components/Select.ts +505 -0
  38. package/src/components/SelectionIndicator.ts +52 -0
  39. package/src/components/Separator.ts +97 -0
  40. package/src/components/SharedElementTransition.ts +209 -0
  41. package/src/components/Slider.ts +472 -0
  42. package/src/components/Switch.ts +449 -0
  43. package/src/components/Table.ts +2618 -0
  44. package/src/components/Tabs.ts +675 -0
  45. package/src/components/TagGroup.ts +443 -0
  46. package/src/components/Text.ts +27 -0
  47. package/src/components/TextArea.ts +92 -0
  48. package/src/components/TextField.ts +200 -0
  49. package/src/components/ToggleButton.ts +147 -0
  50. package/src/components/ToggleButtonGroup.ts +106 -0
  51. package/src/components/Toolbar.ts +74 -0
  52. package/src/components/Tooltip.ts +249 -0
  53. package/src/components/Tree.ts +1450 -0
  54. package/src/components/TreeDropTargetDelegate.ts +363 -0
  55. package/src/components/index.ts +359 -0
  56. package/src/components/useDragAndDrop.ts +164 -0
  57. package/src/components/utils.ts +521 -0
  58. package/src/grid/GridKeyboardDelegate.ts +514 -0
  59. package/src/grid/useGrid.ts +263 -0
  60. package/src/grid/useGridCell.ts +346 -0
  61. package/src/grid/useGridRow.ts +108 -0
  62. package/src/grid/useGridRowGroup.ts +29 -0
  63. package/src/grid/utils.ts +24 -0
  64. package/src/index.ts +38 -0
  65. package/src/intl/autocomplete/ar-AE.json +3 -0
  66. package/src/intl/autocomplete/bg-BG.json +3 -0
  67. package/src/intl/autocomplete/cs-CZ.json +3 -0
  68. package/src/intl/autocomplete/da-DK.json +3 -0
  69. package/src/intl/autocomplete/de-DE.json +3 -0
  70. package/src/intl/autocomplete/el-GR.json +3 -0
  71. package/src/intl/autocomplete/en-US.json +3 -0
  72. package/src/intl/autocomplete/es-ES.json +3 -0
  73. package/src/intl/autocomplete/et-EE.json +3 -0
  74. package/src/intl/autocomplete/fi-FI.json +3 -0
  75. package/src/intl/autocomplete/fr-FR.json +3 -0
  76. package/src/intl/autocomplete/he-IL.json +3 -0
  77. package/src/intl/autocomplete/hr-HR.json +3 -0
  78. package/src/intl/autocomplete/hu-HU.json +3 -0
  79. package/src/intl/autocomplete/index.ts +82 -0
  80. package/src/intl/autocomplete/it-IT.json +3 -0
  81. package/src/intl/autocomplete/ja-JP.json +3 -0
  82. package/src/intl/autocomplete/ko-KR.json +3 -0
  83. package/src/intl/autocomplete/lt-LT.json +3 -0
  84. package/src/intl/autocomplete/lv-LV.json +3 -0
  85. package/src/intl/autocomplete/nb-NO.json +3 -0
  86. package/src/intl/autocomplete/nl-NL.json +3 -0
  87. package/src/intl/autocomplete/pl-PL.json +3 -0
  88. package/src/intl/autocomplete/pt-BR.json +3 -0
  89. package/src/intl/autocomplete/pt-PT.json +3 -0
  90. package/src/intl/autocomplete/ro-RO.json +3 -0
  91. package/src/intl/autocomplete/ru-RU.json +3 -0
  92. package/src/intl/autocomplete/sk-SK.json +3 -0
  93. package/src/intl/autocomplete/sl-SI.json +3 -0
  94. package/src/intl/autocomplete/sr-SP.json +3 -0
  95. package/src/intl/autocomplete/sv-SE.json +3 -0
  96. package/src/intl/autocomplete/tr-TR.json +3 -0
  97. package/src/intl/autocomplete/uk-UA.json +3 -0
  98. package/src/intl/autocomplete/zh-CN.json +3 -0
  99. package/src/intl/autocomplete/zh-TW.json +3 -0
  100. package/src/intl/breadcrumbs/index.ts +9 -2
  101. package/src/intl/combobox/index.ts +9 -2
  102. package/src/intl/compileMessages.ts +356 -0
  103. package/src/intl/grid/index.ts +9 -2
  104. package/src/intl/gridlist/index.ts +9 -2
  105. package/src/intl/menu/index.ts +9 -2
  106. package/src/intl/numberfield/index.ts +9 -2
  107. package/src/intl/overlays/index.ts +9 -2
  108. package/src/intl/searchfield/index.ts +9 -2
  109. package/src/intl/spinbutton/index.ts +9 -2
  110. package/src/intl/table/ar-AE.json +13 -0
  111. package/src/intl/table/bg-BG.json +13 -0
  112. package/src/intl/table/cs-CZ.json +13 -0
  113. package/src/intl/table/da-DK.json +13 -0
  114. package/src/intl/table/de-DE.json +13 -0
  115. package/src/intl/table/el-GR.json +13 -0
  116. package/src/intl/table/en-US.json +13 -0
  117. package/src/intl/table/es-ES.json +13 -0
  118. package/src/intl/table/et-EE.json +13 -0
  119. package/src/intl/table/fi-FI.json +13 -0
  120. package/src/intl/table/fr-FR.json +13 -0
  121. package/src/intl/table/he-IL.json +13 -0
  122. package/src/intl/table/hr-HR.json +13 -0
  123. package/src/intl/table/hu-HU.json +13 -0
  124. package/src/intl/table/index.ts +82 -0
  125. package/src/intl/table/it-IT.json +13 -0
  126. package/src/intl/table/ja-JP.json +13 -0
  127. package/src/intl/table/ko-KR.json +13 -0
  128. package/src/intl/table/lt-LT.json +13 -0
  129. package/src/intl/table/lv-LV.json +13 -0
  130. package/src/intl/table/nb-NO.json +13 -0
  131. package/src/intl/table/nl-NL.json +13 -0
  132. package/src/intl/table/pl-PL.json +13 -0
  133. package/src/intl/table/pt-BR.json +13 -0
  134. package/src/intl/table/pt-PT.json +13 -0
  135. package/src/intl/table/ro-RO.json +13 -0
  136. package/src/intl/table/ru-RU.json +13 -0
  137. package/src/intl/table/sk-SK.json +13 -0
  138. package/src/intl/table/sl-SI.json +13 -0
  139. package/src/intl/table/sr-SP.json +13 -0
  140. package/src/intl/table/sv-SE.json +13 -0
  141. package/src/intl/table/tr-TR.json +13 -0
  142. package/src/intl/table/uk-UA.json +13 -0
  143. package/src/intl/table/zh-CN.json +13 -0
  144. package/src/intl/table/zh-TW.json +13 -0
  145. package/src/intl/tag/index.ts +9 -2
  146. package/src/intl/tree/ar-AE.json +4 -0
  147. package/src/intl/tree/bg-BG.json +4 -0
  148. package/src/intl/tree/cs-CZ.json +4 -0
  149. package/src/intl/tree/da-DK.json +4 -0
  150. package/src/intl/tree/de-DE.json +4 -0
  151. package/src/intl/tree/el-GR.json +4 -0
  152. package/src/intl/tree/en-US.json +4 -0
  153. package/src/intl/tree/es-ES.json +4 -0
  154. package/src/intl/tree/et-EE.json +4 -0
  155. package/src/intl/tree/fi-FI.json +4 -0
  156. package/src/intl/tree/fr-FR.json +4 -0
  157. package/src/intl/tree/he-IL.json +4 -0
  158. package/src/intl/tree/hr-HR.json +4 -0
  159. package/src/intl/tree/hu-HU.json +4 -0
  160. package/src/intl/tree/index.ts +82 -0
  161. package/src/intl/tree/it-IT.json +4 -0
  162. package/src/intl/tree/ja-JP.json +4 -0
  163. package/src/intl/tree/ko-KR.json +4 -0
  164. package/src/intl/tree/lt-LT.json +4 -0
  165. package/src/intl/tree/lv-LV.json +4 -0
  166. package/src/intl/tree/nb-NO.json +4 -0
  167. package/src/intl/tree/nl-NL.json +4 -0
  168. package/src/intl/tree/pl-PL.json +4 -0
  169. package/src/intl/tree/pt-BR.json +4 -0
  170. package/src/intl/tree/pt-PT.json +4 -0
  171. package/src/intl/tree/ro-RO.json +4 -0
  172. package/src/intl/tree/ru-RU.json +4 -0
  173. package/src/intl/tree/sk-SK.json +4 -0
  174. package/src/intl/tree/sl-SI.json +4 -0
  175. package/src/intl/tree/sr-SP.json +4 -0
  176. package/src/intl/tree/sv-SE.json +4 -0
  177. package/src/intl/tree/tr-TR.json +4 -0
  178. package/src/intl/tree/uk-UA.json +4 -0
  179. package/src/intl/tree/zh-CN.json +4 -0
  180. package/src/intl/tree/zh-TW.json +4 -0
  181. package/src/stately/autocomplete/useAutocompleteState.ts +85 -0
  182. package/src/stately/grid/GridCollection.ts +200 -0
  183. package/src/stately/grid/useGridState.ts +160 -0
  184. package/src/stately/index.ts +43 -0
  185. package/src/stately/table/Cell.ts +49 -0
  186. package/src/stately/table/Column.ts +121 -0
  187. package/src/stately/table/Row.ts +145 -0
  188. package/src/stately/table/TableBody.ts +66 -0
  189. package/src/stately/table/TableCollection.ts +390 -0
  190. package/src/stately/table/TableColumnLayout.ts +148 -0
  191. package/src/stately/table/TableHeader.ts +65 -0
  192. package/src/stately/table/TableUtils.ts +269 -0
  193. package/src/stately/table/useTableColumnResizeState.ts +202 -0
  194. package/src/stately/table/useTableState.ts +235 -0
  195. package/src/stately/table/useTreeGridState.ts +248 -0
  196. package/src/table/TableKeyboardDelegate.ts +205 -0
  197. package/src/table/useTable.ts +175 -0
  198. package/src/table/useTableCell.ts +81 -0
  199. package/src/table/useTableColumnHeader.ts +155 -0
  200. package/src/table/useTableColumnResize.ts +374 -0
  201. package/src/table/useTableHeaderRow.ts +57 -0
  202. package/src/table/useTableRow.ts +198 -0
  203. package/src/table/useTableRowGroup.ts +13 -0
  204. package/src/table/useTableSelectionCheckbox.ts +105 -0
  205. package/src/table/utils.ts +42 -0
  206. package/src/tree/useTree.ts +76 -0
  207. package/src/tree/useTreeItem.ts +109 -0
  208. package/src/utils/animation.ts +149 -0
  209. package/src/utils/useLoadMoreSentinel.ts +78 -0
  210. package/src/utils/useViewportSize.ts +123 -0
@@ -0,0 +1,155 @@
1
+ // Ported from react-aria (source: .react-spectrum/packages/react-aria/src/table/useTableColumnHeader.ts).
2
+ // octane adaptations:
3
+ // - `TableState`/`GridNode` come from the ported stately hooks; `useGridCell` from the
4
+ // ported src/grid/; `DOMAttributes` is a local structural prop-bag alias (the
5
+ // `aria-sort` local keeps upstream's literal union).
6
+ // - The Parcel glob intl import becomes the generated src/intl/table index (verbatim
7
+ // dictionaries).
8
+ // - Public-hook slot threading (splitSlot/subSlot); explicit dependency arrays are kept
9
+ // verbatim.
10
+ import type { FocusableElement, RefObject } from '@react-types/shared';
11
+ import { getColumnHeaderId } from './utils';
12
+ import type { GridNode } from '../stately/grid/GridCollection';
13
+ import intlMessages from '../intl/table';
14
+ import { isAndroid } from '../utils/platform';
15
+ import { mergeProps } from '../utils/mergeProps';
16
+ import type { TableState } from '../stately/table/useTableState';
17
+ import { useDescription } from '../utils/useDescription';
18
+ import { useEffect } from 'octane';
19
+ import { useFocusable } from '../interactions/useFocusable';
20
+ import { useGridCell } from '../grid/useGridCell';
21
+ import { useLocalizedStringFormatter } from '../i18n/useLocalizedStringFormatter';
22
+ import { usePress } from '../interactions/usePress';
23
+
24
+ import { S, splitSlot, subSlot } from '../internal';
25
+
26
+ // octane adaptation: minimal structural DOMAttributes (upstream's drags React's synthetic
27
+ // handler types along).
28
+ type DOMAttributes = Record<string, any>;
29
+
30
+ export interface AriaTableColumnHeaderProps<T> {
31
+ /**
32
+ * An object representing the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader).
33
+ * Contains all the relevant information that makes up the column header.
34
+ */
35
+ node: GridNode<T>;
36
+ /**
37
+ * Whether the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader) is contained in a
38
+ * virtual scroller.
39
+ */
40
+ isVirtualized?: boolean;
41
+ }
42
+
43
+ export interface TableColumnHeaderAria {
44
+ /** Props for the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader) element. */
45
+ columnHeaderProps: DOMAttributes;
46
+ /** Whether the column is currently in a pressed state. */
47
+ isPressed: boolean;
48
+ }
49
+
50
+ /**
51
+ * Provides the behavior and accessibility implementation for a column header in a table.
52
+ *
53
+ * @param props - Props for the column header.
54
+ * @param state - State of the table, as returned by `useTableState`.
55
+ * @param ref - The ref attached to the column header element.
56
+ */
57
+ export function useTableColumnHeader<T>(
58
+ props: AriaTableColumnHeaderProps<T>,
59
+ state: TableState<T>,
60
+ ref: RefObject<FocusableElement | null>,
61
+ ): TableColumnHeaderAria;
62
+ // Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
63
+ export function useTableColumnHeader<T>(
64
+ props: AriaTableColumnHeaderProps<T>,
65
+ state: TableState<T>,
66
+ ref: RefObject<FocusableElement | null>,
67
+ slot: symbol | undefined,
68
+ ): TableColumnHeaderAria;
69
+ export function useTableColumnHeader(...args: any[]): TableColumnHeaderAria {
70
+ const [user, slotArg] = splitSlot(args);
71
+ const slot = slotArg ?? S('useTableColumnHeader');
72
+ const props = user[0] as AriaTableColumnHeaderProps<any>;
73
+ const state = user[1] as TableState<any>;
74
+ const ref = user[2] as RefObject<FocusableElement | null>;
75
+
76
+ let { node } = props;
77
+ let allowsSorting = node.props.allowsSorting;
78
+ // if there are no focusable children, the column header will focus the cell
79
+ let { gridCellProps } = useGridCell(
80
+ { ...props, focusMode: 'child' },
81
+ state,
82
+ ref,
83
+ subSlot(slot, 'gridCell'),
84
+ );
85
+
86
+ let isSelectionCellDisabled =
87
+ node.props.isSelectionCell && state.selectionManager.selectionMode === 'single';
88
+
89
+ let { pressProps, isPressed } = usePress(
90
+ {
91
+ isDisabled: !allowsSorting || isSelectionCellDisabled,
92
+ onPress() {
93
+ state.sort(node.key);
94
+ },
95
+ ref,
96
+ },
97
+ subSlot(slot, 'press'),
98
+ );
99
+
100
+ // Needed to pick up the focusable context, enabling things like Tooltips for example
101
+ let { focusableProps } = useFocusable({}, ref, subSlot(slot, 'focusable'));
102
+
103
+ let ariaSort: 'ascending' | 'descending' | 'none' | 'other' | undefined = undefined;
104
+ let isSortedColumn = state.sortDescriptor?.column === node.key;
105
+ let sortDirection = state.sortDescriptor?.direction;
106
+ // aria-sort not supported in Android Talkback
107
+ if (node.props.allowsSorting && !isAndroid()) {
108
+ ariaSort = isSortedColumn ? sortDirection : 'none';
109
+ }
110
+
111
+ let stringFormatter = useLocalizedStringFormatter(
112
+ intlMessages,
113
+ '@react-aria/table',
114
+ subSlot(slot, 'strings'),
115
+ );
116
+ let sortDescription;
117
+ if (allowsSorting) {
118
+ sortDescription = `${stringFormatter.format('sortable')}`;
119
+ // Android Talkback doesn't support aria-sort so we add sort order details to the aria-described by here
120
+ if (isSortedColumn && sortDirection && isAndroid()) {
121
+ sortDescription = `${sortDescription}, ${stringFormatter.format(sortDirection)}`;
122
+ }
123
+ }
124
+
125
+ let descriptionProps = useDescription(sortDescription, subSlot(slot, 'description'));
126
+
127
+ let shouldDisableFocus = state.collection.size === 0;
128
+ useEffect(
129
+ () => {
130
+ if (shouldDisableFocus && state.selectionManager.focusedKey === node.key) {
131
+ state.selectionManager.setFocusedKey(null);
132
+ }
133
+ },
134
+ [shouldDisableFocus, state.selectionManager, node.key],
135
+ subSlot(slot, 'disableFocus'),
136
+ );
137
+
138
+ return {
139
+ columnHeaderProps: {
140
+ ...mergeProps(
141
+ focusableProps,
142
+ gridCellProps,
143
+ pressProps,
144
+ descriptionProps,
145
+ // If the table is empty, make all column headers untabbable
146
+ shouldDisableFocus ? { tabIndex: -1 } : null,
147
+ ),
148
+ role: 'columnheader',
149
+ id: getColumnHeaderId(state, node.key),
150
+ 'aria-colspan': node.colSpan && node.colSpan > 1 ? node.colSpan : undefined,
151
+ 'aria-sort': ariaSort,
152
+ },
153
+ isPressed,
154
+ };
155
+ }
@@ -0,0 +1,374 @@
1
+ // Ported from react-aria (source: .react-spectrum/packages/react-aria/src/table/useTableColumnResize.ts).
2
+ // octane adaptations:
3
+ // - Handlers receive NATIVE events (there is no synthetic layer): React's `ChangeEvent`
4
+ // becomes the native change Event and `getEventTarget(e)` casts to the input. The
5
+ // returned `inputProps.onChange` stays a native `change` handler (the visually hidden
6
+ // resizer is a range input — screen readers drive it via commit-style value changes,
7
+ // matching upstream's intent).
8
+ // - `TableColumnResizeState`/`ColumnSize`/`GridNode` come from the ported stately hooks;
9
+ // `DOMAttributes` is a local structural prop-bag alias.
10
+ // - The Parcel glob intl import becomes the generated src/intl/table index (verbatim
11
+ // dictionaries).
12
+ // - Public-hook slot threading (splitSlot/subSlot); explicit dependency arrays are kept
13
+ // verbatim. Column-resize pixel math is layout-driven and inert in jsdom; the logic is
14
+ // ported verbatim regardless.
15
+ import { useCallback, useEffect, useRef } from 'octane';
16
+ import type { ColumnSize } from '../stately/table/Column';
17
+ import type { FocusableElement, Key, RefObject } from '@react-types/shared';
18
+ import { focusSafely } from '../interactions/focusSafely';
19
+ import { getActiveElement, getEventTarget } from '../utils/shadowdom/DOMFunctions';
20
+ import { getColumnHeaderId } from './utils';
21
+ import type { GridNode } from '../stately/grid/GridCollection';
22
+ import intlMessages from '../intl/table';
23
+ import { mergeProps } from '../utils/mergeProps';
24
+ import type { TableColumnResizeState } from '../stately/table/useTableColumnResizeState';
25
+ import { useDescription } from '../utils/useDescription';
26
+ import { useEffectEvent } from '../utils/useEffectEvent';
27
+ import { useId } from '../utils/useId';
28
+ import { useInteractionModality } from '../interactions/useFocusVisible';
29
+ import { useKeyboard } from '../interactions/useKeyboard';
30
+ import { useLocale } from '../i18n/I18nProvider';
31
+ import { useLocalizedStringFormatter } from '../i18n/useLocalizedStringFormatter';
32
+ import { useMove } from '../interactions/useMove';
33
+ import { usePress } from '../interactions/usePress';
34
+ import { useVisuallyHidden } from '../visually-hidden/VisuallyHidden';
35
+
36
+ import { S, splitSlot, subSlot } from '../internal';
37
+
38
+ // octane adaptation: minimal structural DOMAttributes (upstream's drags React's synthetic
39
+ // handler types along).
40
+ type DOMAttributes = Record<string, any>;
41
+
42
+ export interface TableColumnResizeAria {
43
+ /** Props for the visually hidden input element. */
44
+ inputProps: DOMAttributes;
45
+ /** Props for the resizer element. */
46
+ resizerProps: DOMAttributes;
47
+ /** Whether this column is currently being resized. */
48
+ isResizing: boolean;
49
+ }
50
+
51
+ export interface AriaTableColumnResizeProps<T> {
52
+ /**
53
+ * An object representing the [column header](https://www.w3.org/TR/wai-aria-1.1/#columnheader).
54
+ * Contains all the relevant information that makes up the column header.
55
+ */
56
+ column: GridNode<T>;
57
+ /** Aria label for the hidden input. Gets read when resizing. */
58
+ 'aria-label': string;
59
+ /**
60
+ * Ref to the trigger if resizing was started from a column header menu. If it's provided, focus
61
+ * will be returned there when resizing is done. If it isn't provided, it is assumed that the
62
+ * resizer is visible at all time and keyboard resizing is started via pressing Enter on the
63
+ * resizer and not on focus.
64
+ */
65
+ triggerRef?: RefObject<FocusableElement | null>;
66
+ /** If resizing is disabled. */
67
+ isDisabled?: boolean;
68
+ /** Called when resizing starts. */
69
+ onResizeStart?: (widths: Map<Key, ColumnSize>) => void;
70
+ /** Called for every resize event that results in new column sizes. */
71
+ onResize?: (widths: Map<Key, ColumnSize>) => void;
72
+ /** Called when resizing ends. */
73
+ onResizeEnd?: (widths: Map<Key, ColumnSize>) => void;
74
+ }
75
+
76
+ /**
77
+ * Provides the behavior and accessibility implementation for a table column resizer element.
78
+ *
79
+ * @param props - Props for the resizer.
80
+ * @param state - State for the table's resizable columns, as returned by
81
+ * `useTableColumnResizeState`.
82
+ * @param ref - The ref attached to the resizer's visually hidden input element.
83
+ */
84
+ export function useTableColumnResize<T>(
85
+ props: AriaTableColumnResizeProps<T>,
86
+ state: TableColumnResizeState<T>,
87
+ ref: RefObject<HTMLInputElement | null>,
88
+ ): TableColumnResizeAria;
89
+ // Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
90
+ export function useTableColumnResize<T>(
91
+ props: AriaTableColumnResizeProps<T>,
92
+ state: TableColumnResizeState<T>,
93
+ ref: RefObject<HTMLInputElement | null>,
94
+ slot: symbol | undefined,
95
+ ): TableColumnResizeAria;
96
+ export function useTableColumnResize(...args: any[]): TableColumnResizeAria {
97
+ const [user, slotArg] = splitSlot(args);
98
+ const slot = slotArg ?? S('useTableColumnResize');
99
+ const props = user[0] as AriaTableColumnResizeProps<any>;
100
+ const state = user[1] as TableColumnResizeState<any>;
101
+ const ref = user[2] as RefObject<HTMLInputElement | null>;
102
+
103
+ let {
104
+ column: item,
105
+ triggerRef,
106
+ isDisabled,
107
+ onResizeStart,
108
+ onResize,
109
+ onResizeEnd,
110
+ 'aria-label': ariaLabel,
111
+ } = props;
112
+ const stringFormatter = useLocalizedStringFormatter(
113
+ intlMessages,
114
+ '@react-aria/table',
115
+ subSlot(slot, 'strings'),
116
+ );
117
+ let id = useId(subSlot(slot, 'id'));
118
+ let isResizing = state.resizingColumn === item.key;
119
+ let isResizingRef = useRef(isResizing, subSlot(slot, 'isResizing'));
120
+ let lastSize = useRef<Map<Key, ColumnSize> | null>(null, subSlot(slot, 'lastSize'));
121
+ let wasFocusedOnResizeStart = useRef(false, subSlot(slot, 'wasFocused'));
122
+ let editModeEnabled = state.tableState.isKeyboardNavigationDisabled;
123
+
124
+ let { direction } = useLocale(subSlot(slot, 'locale'));
125
+
126
+ let startResize = useCallback(
127
+ (item: GridNode<any>) => {
128
+ if (!isResizingRef.current) {
129
+ lastSize.current = state.updateResizedColumns(item.key, state.getColumnWidth(item.key));
130
+ state.startResize(item.key);
131
+ state.tableState.setKeyboardNavigationDisabled(true);
132
+ onResizeStart?.(lastSize.current);
133
+ }
134
+ isResizingRef.current = true;
135
+ },
136
+ [state, onResizeStart],
137
+ subSlot(slot, 'startResize'),
138
+ );
139
+
140
+ let resize = useCallback(
141
+ (item: GridNode<any>, newWidth: number) => {
142
+ let sizes = state.updateResizedColumns(item.key, newWidth);
143
+ onResize?.(sizes);
144
+ lastSize.current = sizes;
145
+ },
146
+ [state, onResize],
147
+ subSlot(slot, 'resize'),
148
+ );
149
+
150
+ let endResize = useCallback(
151
+ (item: GridNode<any>) => {
152
+ if (isResizingRef.current) {
153
+ if (lastSize.current == null) {
154
+ lastSize.current = state.updateResizedColumns(item.key, state.getColumnWidth(item.key));
155
+ }
156
+
157
+ state.endResize();
158
+ state.tableState.setKeyboardNavigationDisabled(false);
159
+ onResizeEnd?.(lastSize.current);
160
+ isResizingRef.current = false;
161
+
162
+ if (triggerRef?.current && !wasFocusedOnResizeStart.current) {
163
+ // switch focus back to the column header unless the resizer was already focused when resizing started.
164
+ focusSafely(triggerRef.current);
165
+ }
166
+ }
167
+ lastSize.current = null;
168
+ },
169
+ [state, triggerRef, onResizeEnd],
170
+ subSlot(slot, 'endResize'),
171
+ );
172
+
173
+ let { keyboardProps } = useKeyboard(
174
+ {
175
+ onKeyDown: (e) => {
176
+ if (editModeEnabled) {
177
+ if (e.key === 'Escape' || e.key === 'Enter' || e.key === ' ' || e.key === 'Tab') {
178
+ e.preventDefault();
179
+ endResize(item);
180
+ }
181
+ } else {
182
+ // Continue propagation on keydown events so they still bubbles to useSelectableCollection and are handled there
183
+ e.continuePropagation();
184
+
185
+ if (e.key === 'Enter') {
186
+ startResize(item);
187
+ }
188
+ }
189
+ },
190
+ },
191
+ subSlot(slot, 'keyboard'),
192
+ );
193
+
194
+ const columnResizeWidthRef = useRef<number>(0, subSlot(slot, 'resizeWidth'));
195
+ const { moveProps } = useMove(
196
+ {
197
+ onMoveStart() {
198
+ columnResizeWidthRef.current = state.getColumnWidth(item.key);
199
+ startResize(item);
200
+ },
201
+ onMove(e) {
202
+ let { deltaX, deltaY, pointerType } = e;
203
+ if (direction === 'rtl') {
204
+ deltaX *= -1;
205
+ }
206
+ if (pointerType === 'keyboard') {
207
+ if (deltaY !== 0 && deltaX === 0) {
208
+ deltaX = deltaY * -1;
209
+ }
210
+ deltaX *= 10;
211
+ }
212
+ // if moving up/down only, no need to resize
213
+ if (deltaX !== 0) {
214
+ columnResizeWidthRef.current += deltaX;
215
+ resize(item, columnResizeWidthRef.current);
216
+ }
217
+ },
218
+ onMoveEnd(e) {
219
+ let { pointerType } = e;
220
+ columnResizeWidthRef.current = 0;
221
+ if (
222
+ pointerType === 'mouse' ||
223
+ (pointerType === 'touch' && wasFocusedOnResizeStart.current)
224
+ ) {
225
+ endResize(item);
226
+ }
227
+ },
228
+ },
229
+ subSlot(slot, 'move'),
230
+ );
231
+
232
+ let onKeyDown = useCallback(
233
+ (e: KeyboardEvent) => {
234
+ if (editModeEnabled) {
235
+ moveProps.onKeyDown?.(e);
236
+ }
237
+ },
238
+ [editModeEnabled, moveProps],
239
+ subSlot(slot, 'onKeyDown'),
240
+ );
241
+
242
+ let min = Math.floor(state.getColumnMinWidth(item.key));
243
+ let max = Math.floor(state.getColumnMaxWidth(item.key));
244
+ if (max === Infinity) {
245
+ max = Number.MAX_SAFE_INTEGER;
246
+ }
247
+ let value = Math.floor(state.getColumnWidth(item.key));
248
+ let modality: string | null = useInteractionModality(subSlot(slot, 'modality'));
249
+ if (modality === 'virtual' && typeof window !== 'undefined' && 'ontouchstart' in window) {
250
+ modality = 'touch';
251
+ }
252
+ let description =
253
+ triggerRef?.current == null &&
254
+ (modality === 'keyboard' || modality === 'virtual') &&
255
+ !isResizing
256
+ ? stringFormatter.format('resizerDescription')
257
+ : undefined;
258
+ let descriptionProps = useDescription(description, subSlot(slot, 'description'));
259
+ let ariaProps = {
260
+ 'aria-label': ariaLabel,
261
+ 'aria-orientation': 'horizontal' as 'horizontal',
262
+ 'aria-labelledby': `${id} ${getColumnHeaderId(state.tableState, item.key)}`,
263
+ 'aria-valuetext': stringFormatter.format('columnSize', { value }),
264
+ type: 'range',
265
+ min,
266
+ max,
267
+ value,
268
+ ...descriptionProps,
269
+ };
270
+
271
+ const focusInput = useCallback(
272
+ () => {
273
+ if (ref.current) {
274
+ focusSafely(ref.current);
275
+ }
276
+ },
277
+ [ref],
278
+ subSlot(slot, 'focusInput'),
279
+ );
280
+
281
+ let resizingColumn = state.resizingColumn;
282
+ let prevResizingColumn = useRef<Key | null>(null, subSlot(slot, 'prevResizing'));
283
+ let startResizeEvent = useEffectEvent(startResize, subSlot(slot, 'startResizeEvent'));
284
+ useEffect(
285
+ () => {
286
+ if (
287
+ prevResizingColumn.current !== resizingColumn &&
288
+ resizingColumn != null &&
289
+ resizingColumn === item.key
290
+ ) {
291
+ wasFocusedOnResizeStart.current = getActiveElement() === ref.current;
292
+ startResizeEvent(item);
293
+ // Delay focusing input until Android Chrome's delayed click after touchend happens: https://bugs.chromium.org/p/chromium/issues/detail?id=1150073
294
+ let timeout = setTimeout(() => focusInput(), 0);
295
+ // VoiceOver on iOS has problems focusing the input from a menu.
296
+ let VOTimeout = setTimeout(focusInput, 400);
297
+ return () => {
298
+ clearTimeout(timeout);
299
+ clearTimeout(VOTimeout);
300
+ };
301
+ }
302
+ prevResizingColumn.current = resizingColumn;
303
+ },
304
+ [resizingColumn, item, focusInput, ref],
305
+ subSlot(slot, 'resizeEffect'),
306
+ );
307
+
308
+ let onChange = (e: Event) => {
309
+ let currentWidth = state.getColumnWidth(item.key);
310
+ let nextValue = parseFloat((getEventTarget(e) as HTMLInputElement).value);
311
+
312
+ if (nextValue > currentWidth) {
313
+ nextValue = currentWidth + 10;
314
+ } else {
315
+ nextValue = currentWidth - 10;
316
+ }
317
+ resize(item, nextValue);
318
+ };
319
+
320
+ let { pressProps } = usePress(
321
+ {
322
+ preventFocusOnPress: true,
323
+ onPressStart: (e) => {
324
+ if (e.ctrlKey || e.altKey || e.metaKey || e.shiftKey || e.pointerType === 'keyboard') {
325
+ return;
326
+ }
327
+ if (e.pointerType === 'virtual' && state.resizingColumn != null) {
328
+ endResize(item);
329
+ return;
330
+ }
331
+
332
+ // Sometimes onPress won't trigger for quick taps on mobile so we want to focus the input so blurring away
333
+ // can cancel resize mode for us.
334
+ focusInput();
335
+
336
+ // If resizer is always visible, mobile screenreader user can access the visually hidden resizer directly and thus we don't need
337
+ // to handle a virtual click to start the resizer.
338
+ if (e.pointerType !== 'virtual') {
339
+ startResize(item);
340
+ }
341
+ },
342
+ onPress: (e) => {
343
+ if (
344
+ ((e.pointerType === 'touch' && wasFocusedOnResizeStart.current) ||
345
+ e.pointerType === 'mouse') &&
346
+ state.resizingColumn != null
347
+ ) {
348
+ endResize(item);
349
+ }
350
+ },
351
+ },
352
+ subSlot(slot, 'press'),
353
+ );
354
+ let { visuallyHiddenProps } = useVisuallyHidden(undefined, subSlot(slot, 'visuallyHidden'));
355
+
356
+ return {
357
+ resizerProps: mergeProps(keyboardProps, { ...moveProps, onKeyDown }, pressProps, {
358
+ style: { touchAction: 'none' },
359
+ }),
360
+ inputProps: mergeProps(
361
+ visuallyHiddenProps,
362
+ {
363
+ id,
364
+ onBlur: () => {
365
+ endResize(item);
366
+ },
367
+ onChange,
368
+ disabled: isDisabled,
369
+ },
370
+ ariaProps,
371
+ ),
372
+ isResizing,
373
+ };
374
+ }
@@ -0,0 +1,57 @@
1
+ // Ported from react-aria (source: .react-spectrum/packages/react-aria/src/table/useTableHeaderRow.ts).
2
+ // octane adaptations:
3
+ // - `TableState` comes from the ported stately table hook; `GridRowProps` from the ported
4
+ // src/grid/useGridRow; `DOMAttributes` is a local structural prop-bag alias.
5
+ // - The hook composes no base hooks, so the optional trailing slot is accepted (uniform
6
+ // calling convention) and unused.
7
+ import type { RefObject } from '@react-types/shared';
8
+ import type { GridRowProps } from '../grid/useGridRow';
9
+ import type { TableState } from '../stately/table/useTableState';
10
+
11
+ import { splitSlot } from '../internal';
12
+
13
+ // octane adaptation: minimal structural DOMAttributes (upstream's drags React's synthetic
14
+ // handler types along).
15
+ type DOMAttributes = Record<string, any>;
16
+
17
+ export interface TableHeaderRowAria {
18
+ /** Props for the grid row element. */
19
+ rowProps: DOMAttributes;
20
+ }
21
+
22
+ /**
23
+ * Provides the behavior and accessibility implementation for a header row in a table.
24
+ *
25
+ * @param props - Props for the row.
26
+ * @param state - State of the table, as returned by `useTableState`.
27
+ */
28
+ export function useTableHeaderRow<T>(
29
+ props: GridRowProps<T>,
30
+ state: TableState<T>,
31
+ ref: RefObject<Element | null>,
32
+ ): TableHeaderRowAria;
33
+ // Slot-threading form: sibling ported hooks pass their derived sub-slot as the trailing arg.
34
+ export function useTableHeaderRow<T>(
35
+ props: GridRowProps<T>,
36
+ state: TableState<T>,
37
+ ref: RefObject<Element | null>,
38
+ slot: symbol | undefined,
39
+ ): TableHeaderRowAria;
40
+ export function useTableHeaderRow(...args: any[]): TableHeaderRowAria {
41
+ const [user] = splitSlot(args);
42
+ const props = user[0] as GridRowProps<any>;
43
+ const state = user[1] as TableState<any>;
44
+
45
+ let { node, isVirtualized } = props;
46
+ let rowProps: DOMAttributes = {
47
+ role: 'row',
48
+ };
49
+
50
+ if (isVirtualized && state.treeColumn == null) {
51
+ rowProps['aria-rowindex'] = node.index + 1; // aria-rowindex is 1 based
52
+ }
53
+
54
+ return {
55
+ rowProps,
56
+ };
57
+ }