@proyecto-viviana/solidaria 0.2.4 → 0.2.8

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 (219) hide show
  1. package/LICENSE +21 -0
  2. package/dist/actiongroup/createActionGroup.d.ts +29 -0
  3. package/dist/actiongroup/createActionGroup.d.ts.map +1 -0
  4. package/dist/actiongroup/index.d.ts +2 -0
  5. package/dist/actiongroup/index.d.ts.map +1 -0
  6. package/dist/autocomplete/createAutocomplete.d.ts +6 -2
  7. package/dist/autocomplete/createAutocomplete.d.ts.map +1 -1
  8. package/dist/breadcrumbs/createBreadcrumbs.d.ts +2 -0
  9. package/dist/breadcrumbs/createBreadcrumbs.d.ts.map +1 -1
  10. package/dist/button/createToggleButtonGroup.d.ts +32 -0
  11. package/dist/button/createToggleButtonGroup.d.ts.map +1 -0
  12. package/dist/button/index.d.ts +2 -0
  13. package/dist/button/index.d.ts.map +1 -1
  14. package/dist/calendar/createCalendarCell.d.ts +2 -0
  15. package/dist/calendar/createCalendarCell.d.ts.map +1 -1
  16. package/dist/calendar/createCalendarGrid.d.ts.map +1 -1
  17. package/dist/calendar/createRangeCalendarCell.d.ts +3 -1
  18. package/dist/calendar/createRangeCalendarCell.d.ts.map +1 -1
  19. package/dist/checkbox/createCheckboxGroup.d.ts +5 -1
  20. package/dist/checkbox/createCheckboxGroup.d.ts.map +1 -1
  21. package/dist/collections/index.d.ts +56 -0
  22. package/dist/collections/index.d.ts.map +1 -0
  23. package/dist/color/createColorArea.d.ts.map +1 -1
  24. package/dist/color/createColorSlider.d.ts.map +1 -1
  25. package/dist/color/createColorWheel.d.ts.map +1 -1
  26. package/dist/combobox/createComboBox.d.ts +6 -0
  27. package/dist/combobox/createComboBox.d.ts.map +1 -1
  28. package/dist/datepicker/createDatePicker.d.ts +6 -0
  29. package/dist/datepicker/createDatePicker.d.ts.map +1 -1
  30. package/dist/datepicker/createDateRangePicker.d.ts +40 -0
  31. package/dist/datepicker/createDateRangePicker.d.ts.map +1 -0
  32. package/dist/datepicker/createDateSegment.d.ts +1 -1
  33. package/dist/datepicker/createDateSegment.d.ts.map +1 -1
  34. package/dist/datepicker/createTimeSegment.d.ts +29 -0
  35. package/dist/datepicker/createTimeSegment.d.ts.map +1 -0
  36. package/dist/datepicker/index.d.ts +2 -0
  37. package/dist/datepicker/index.d.ts.map +1 -1
  38. package/dist/disclosure/createDisclosureGroup.d.ts +2 -1
  39. package/dist/disclosure/createDisclosureGroup.d.ts.map +1 -1
  40. package/dist/dnd/createDrag.d.ts.map +1 -1
  41. package/dist/dnd/createDraggableCollection.d.ts +4 -0
  42. package/dist/dnd/createDraggableCollection.d.ts.map +1 -1
  43. package/dist/dnd/createDraggableItem.d.ts.map +1 -1
  44. package/dist/dnd/createDrop.d.ts.map +1 -1
  45. package/dist/dnd/createDroppableCollection.d.ts +32 -1
  46. package/dist/dnd/createDroppableCollection.d.ts.map +1 -1
  47. package/dist/dnd/createDroppableItem.d.ts.map +1 -1
  48. package/dist/dnd/index.d.ts +1 -1
  49. package/dist/dnd/index.d.ts.map +1 -1
  50. package/dist/grid/createGrid.d.ts.map +1 -1
  51. package/dist/gridlist/createGridList.d.ts.map +1 -1
  52. package/dist/index.d.ts +6 -4
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +4659 -3452
  55. package/dist/index.js.map +1 -7
  56. package/dist/index.ssr.js +4659 -3452
  57. package/dist/index.ssr.js.map +1 -7
  58. package/dist/interactions/createFocus.d.ts.map +1 -1
  59. package/dist/interactions/createFocusWithin.d.ts.map +1 -1
  60. package/dist/link/createLink.d.ts +10 -0
  61. package/dist/link/createLink.d.ts.map +1 -1
  62. package/dist/listbox/createListBox.d.ts +1 -0
  63. package/dist/listbox/createListBox.d.ts.map +1 -1
  64. package/dist/listbox/createOption.d.ts.map +1 -1
  65. package/dist/menu/createMenu.d.ts +1 -0
  66. package/dist/menu/createMenu.d.ts.map +1 -1
  67. package/dist/meter/createMeter.d.ts.map +1 -1
  68. package/dist/numberfield/createNumberField.d.ts +18 -0
  69. package/dist/numberfield/createNumberField.d.ts.map +1 -1
  70. package/dist/overlays/createModal.d.ts +16 -0
  71. package/dist/overlays/createModal.d.ts.map +1 -1
  72. package/dist/overlays/createOverlay.d.ts.map +1 -1
  73. package/dist/overlays/index.d.ts +1 -1
  74. package/dist/overlays/index.d.ts.map +1 -1
  75. package/dist/popover/createOverlayPosition.d.ts.map +1 -1
  76. package/dist/popover/createPopover.d.ts.map +1 -1
  77. package/dist/progress/createProgressBar.d.ts.map +1 -1
  78. package/dist/radio/createRadioGroup.d.ts +2 -2
  79. package/dist/radio/createRadioGroup.d.ts.map +1 -1
  80. package/dist/searchfield/createSearchField.d.ts.map +1 -1
  81. package/dist/select/createHiddenSelect.d.ts.map +1 -1
  82. package/dist/select/createSelect.d.ts.map +1 -1
  83. package/dist/slider/createSlider.d.ts.map +1 -1
  84. package/dist/table/createTable.d.ts.map +1 -1
  85. package/dist/tabs/createTabs.d.ts +1 -1
  86. package/dist/tabs/createTabs.d.ts.map +1 -1
  87. package/dist/tag/createTag.d.ts.map +1 -1
  88. package/dist/tag/createTagGroup.d.ts.map +1 -1
  89. package/dist/toast/createToast.d.ts +4 -0
  90. package/dist/toast/createToast.d.ts.map +1 -1
  91. package/dist/toast/createToastRegion.d.ts.map +1 -1
  92. package/dist/toolbar/createToolbar.d.ts.map +1 -1
  93. package/dist/tooltip/createTooltipTrigger.d.ts.map +1 -1
  94. package/dist/tree/createTree.d.ts.map +1 -1
  95. package/dist/tree/createTreeItem.d.ts.map +1 -1
  96. package/dist/tree/types.d.ts +4 -0
  97. package/dist/tree/types.d.ts.map +1 -1
  98. package/dist/utils/env.d.ts +1 -1
  99. package/dist/utils/env.d.ts.map +1 -1
  100. package/dist/utils/platform.d.ts.map +1 -1
  101. package/dist/visually-hidden/createVisuallyHidden.d.ts.map +1 -1
  102. package/package.json +8 -6
  103. package/src/actiongroup/createActionGroup.ts +324 -0
  104. package/src/actiongroup/index.ts +8 -0
  105. package/src/autocomplete/createAutocomplete.ts +32 -9
  106. package/src/breadcrumbs/createBreadcrumbs.ts +10 -15
  107. package/src/button/createButton.ts +1 -1
  108. package/src/button/createToggleButtonGroup.ts +128 -0
  109. package/src/button/index.ts +9 -0
  110. package/src/calendar/createCalendarCell.ts +6 -4
  111. package/src/calendar/createCalendarGrid.ts +27 -18
  112. package/src/calendar/createRangeCalendarCell.ts +26 -9
  113. package/src/checkbox/createCheckboxGroup.ts +21 -4
  114. package/src/collections/index.ts +242 -0
  115. package/src/color/createColorArea.ts +380 -314
  116. package/src/color/createColorField.ts +137 -137
  117. package/src/color/createColorSlider.ts +286 -197
  118. package/src/color/createColorSwatch.ts +40 -40
  119. package/src/color/createColorWheel.ts +218 -208
  120. package/src/color/index.ts +24 -24
  121. package/src/color/types.ts +116 -116
  122. package/src/combobox/createComboBox.ts +670 -647
  123. package/src/combobox/index.ts +6 -6
  124. package/src/datepicker/createDatePicker.ts +54 -16
  125. package/src/datepicker/createDateRangePicker.ts +246 -0
  126. package/src/datepicker/createDateSegment.ts +185 -31
  127. package/src/datepicker/createTimeSegment.ts +370 -0
  128. package/src/datepicker/index.ts +14 -0
  129. package/src/dialog/createDialog.ts +120 -120
  130. package/src/dialog/index.ts +2 -2
  131. package/src/dialog/types.ts +19 -19
  132. package/src/disclosure/createDisclosureGroup.ts +5 -2
  133. package/src/dnd/createDrag.ts +224 -209
  134. package/src/dnd/createDraggableCollection.ts +96 -63
  135. package/src/dnd/createDraggableItem.ts +259 -243
  136. package/src/dnd/createDrop.ts +322 -321
  137. package/src/dnd/createDroppableCollection.ts +682 -293
  138. package/src/dnd/createDroppableItem.ts +215 -213
  139. package/src/dnd/index.ts +55 -47
  140. package/src/dnd/types.ts +89 -89
  141. package/src/dnd/utils.ts +294 -294
  142. package/src/focus/createAutoFocus.ts +321 -321
  143. package/src/focus/createFocusRestore.ts +313 -313
  144. package/src/focus/createVirtualFocus.ts +396 -396
  145. package/src/form/createFormValidation.ts +224 -224
  146. package/src/form/index.ts +11 -11
  147. package/src/grid/createGrid.ts +3 -1
  148. package/src/gridlist/createGridList.ts +16 -0
  149. package/src/gridlist/createGridListItem.ts +1 -1
  150. package/src/i18n/NumberFormatter.ts +266 -266
  151. package/src/i18n/createCollator.ts +79 -79
  152. package/src/i18n/createDateFormatter.ts +83 -83
  153. package/src/i18n/createFilter.ts +131 -131
  154. package/src/i18n/createNumberFormatter.ts +52 -52
  155. package/src/i18n/index.ts +40 -40
  156. package/src/i18n/locale.tsx +188 -188
  157. package/src/i18n/utils.ts +99 -99
  158. package/src/index.ts +51 -0
  159. package/src/interactions/createFocus.ts +6 -5
  160. package/src/interactions/createFocusWithin.ts +6 -5
  161. package/src/interactions/createLongPress.ts +174 -174
  162. package/src/interactions/createMove.ts +289 -289
  163. package/src/interactions/createPress.ts +5 -5
  164. package/src/landmark/createLandmark.ts +377 -377
  165. package/src/landmark/index.ts +8 -8
  166. package/src/link/createLink.ts +23 -8
  167. package/src/listbox/createListBox.ts +308 -269
  168. package/src/listbox/createOption.ts +162 -151
  169. package/src/listbox/index.ts +12 -12
  170. package/src/live-announcer/announce.ts +322 -322
  171. package/src/live-announcer/index.ts +9 -9
  172. package/src/menu/createMenu.ts +405 -396
  173. package/src/menu/createMenuItem.ts +149 -149
  174. package/src/menu/createMenuTrigger.ts +88 -88
  175. package/src/menu/index.ts +18 -18
  176. package/src/meter/createMeter.ts +1 -6
  177. package/src/numberfield/createNumberField.ts +311 -268
  178. package/src/numberfield/index.ts +5 -5
  179. package/src/overlays/ariaHideOutside.ts +219 -219
  180. package/src/overlays/createInteractOutside.ts +149 -149
  181. package/src/overlays/createModal.tsx +238 -202
  182. package/src/overlays/createOverlay.ts +165 -155
  183. package/src/overlays/createOverlayTrigger.ts +85 -85
  184. package/src/overlays/createPreventScroll.ts +266 -266
  185. package/src/overlays/index.ts +48 -44
  186. package/src/popover/calculatePosition.ts +6 -6
  187. package/src/popover/createOverlayPosition.ts +7 -4
  188. package/src/popover/createPopover.ts +21 -7
  189. package/src/progress/createProgressBar.ts +6 -1
  190. package/src/radio/createRadioGroup.ts +88 -14
  191. package/src/searchfield/createSearchField.ts +241 -186
  192. package/src/searchfield/index.ts +2 -2
  193. package/src/select/createHiddenSelect.tsx +263 -236
  194. package/src/select/createSelect.ts +373 -395
  195. package/src/select/index.ts +14 -14
  196. package/src/slider/createSlider.ts +364 -349
  197. package/src/slider/index.ts +2 -2
  198. package/src/ssr/index.tsx +370 -370
  199. package/src/table/createTable.ts +3 -1
  200. package/src/table/createTableColumnHeader.ts +1 -1
  201. package/src/table/createTableRow.ts +1 -1
  202. package/src/tabs/createTabs.ts +80 -51
  203. package/src/tag/createTag.ts +135 -6
  204. package/src/tag/createTagGroup.ts +7 -2
  205. package/src/toast/createToast.ts +8 -2
  206. package/src/toast/createToastRegion.ts +0 -1
  207. package/src/toolbar/createToolbar.ts +75 -1
  208. package/src/tooltip/createTooltip.ts +79 -79
  209. package/src/tooltip/createTooltipTrigger.ts +226 -222
  210. package/src/tooltip/index.ts +6 -6
  211. package/src/tree/createTree.ts +261 -246
  212. package/src/tree/createTreeItem.ts +282 -233
  213. package/src/tree/createTreeSelectionCheckbox.ts +68 -68
  214. package/src/tree/index.ts +16 -16
  215. package/src/tree/types.ts +91 -87
  216. package/src/utils/env.ts +55 -54
  217. package/src/utils/platform.ts +16 -6
  218. package/src/visually-hidden/createVisuallyHidden.ts +139 -124
  219. package/src/visually-hidden/index.ts +6 -6
@@ -1,396 +1,396 @@
1
- /**
2
- * Virtual focus management for solidaria
3
- *
4
- * Provides virtual focus for large collections where tracking DOM focus
5
- * on every item is impractical. Instead, a single element receives real
6
- * focus while aria-activedescendant indicates the virtually focused item.
7
- *
8
- * This is commonly used in:
9
- * - Virtualized lists (where not all items are in the DOM)
10
- * - Large trees
11
- * - Autocomplete/combobox suggestions
12
- * - Grid/table navigation
13
- */
14
-
15
- import { type Accessor, createSignal, createEffect, onCleanup } from 'solid-js';
16
- import { isServer } from 'solid-js/web';
17
-
18
- // ============================================
19
- // TYPES
20
- // ============================================
21
-
22
- export interface VirtualFocusOptions<T> {
23
- /**
24
- * The items in the collection.
25
- */
26
- items: Accessor<T[]>;
27
- /**
28
- * Function to get a unique key for each item.
29
- */
30
- getKey: (item: T) => string;
31
- /**
32
- * Function to check if an item is disabled.
33
- */
34
- isDisabled?: (item: T) => boolean;
35
- /**
36
- * Initial focused key.
37
- */
38
- defaultFocusedKey?: string;
39
- /**
40
- * Controlled focused key.
41
- */
42
- focusedKey?: Accessor<string | null>;
43
- /**
44
- * Callback when focused key changes.
45
- */
46
- onFocusChange?: (key: string | null) => void;
47
- /**
48
- * Whether to wrap focus at the edges.
49
- * @default true
50
- */
51
- wrap?: boolean;
52
- /**
53
- * Whether to loop through disabled items.
54
- * @default false
55
- */
56
- skipDisabled?: boolean;
57
- /**
58
- * Orientation for keyboard navigation.
59
- * @default 'vertical'
60
- */
61
- orientation?: 'horizontal' | 'vertical' | 'both';
62
- }
63
-
64
- export interface VirtualFocusResult<T> {
65
- /**
66
- * The currently focused key.
67
- */
68
- focusedKey: Accessor<string | null>;
69
- /**
70
- * The currently focused item (if any).
71
- */
72
- focusedItem: Accessor<T | null>;
73
- /**
74
- * Set the focused key.
75
- */
76
- setFocusedKey: (key: string | null) => void;
77
- /**
78
- * Move focus to the next item.
79
- */
80
- focusNext: () => void;
81
- /**
82
- * Move focus to the previous item.
83
- */
84
- focusPrevious: () => void;
85
- /**
86
- * Move focus to the first item.
87
- */
88
- focusFirst: () => void;
89
- /**
90
- * Move focus to the last item.
91
- */
92
- focusLast: () => void;
93
- /**
94
- * Move focus by a page (for large lists).
95
- */
96
- focusPageDown: (pageSize?: number) => void;
97
- /**
98
- * Move focus up by a page (for large lists).
99
- */
100
- focusPageUp: (pageSize?: number) => void;
101
- /**
102
- * Check if a key is the currently focused key.
103
- */
104
- isFocused: (key: string) => boolean;
105
- /**
106
- * Props to spread on the container element.
107
- */
108
- containerProps: {
109
- 'aria-activedescendant': Accessor<string | undefined>;
110
- onKeyDown: (e: KeyboardEvent) => void;
111
- };
112
- /**
113
- * Get props for an item element.
114
- */
115
- getItemProps: (key: string) => {
116
- id: string;
117
- 'aria-selected'?: boolean;
118
- };
119
- }
120
-
121
- // ============================================
122
- // CONSTANTS
123
- // ============================================
124
-
125
- const DEFAULT_PAGE_SIZE = 10;
126
-
127
- // ============================================
128
- // HOOK
129
- // ============================================
130
-
131
- /**
132
- * Creates virtual focus management for a collection.
133
- *
134
- * Virtual focus uses aria-activedescendant to indicate which item is
135
- * "focused" while keeping real DOM focus on a container element. This
136
- * is more performant for large collections and required for virtualized
137
- * lists where items may not be in the DOM.
138
- *
139
- * @example
140
- * ```tsx
141
- * function Listbox(props) {
142
- * const virtualFocus = createVirtualFocus({
143
- * items: () => props.items,
144
- * getKey: (item) => item.id,
145
- * isDisabled: (item) => item.disabled,
146
- * });
147
- *
148
- * return (
149
- * <ul
150
- * role="listbox"
151
- * tabIndex={0}
152
- * aria-activedescendant={virtualFocus.containerProps['aria-activedescendant']()}
153
- * onKeyDown={virtualFocus.containerProps.onKeyDown}
154
- * >
155
- * <For each={props.items}>
156
- * {(item) => (
157
- * <li
158
- * {...virtualFocus.getItemProps(item.id)}
159
- * role="option"
160
- * aria-selected={virtualFocus.isFocused(item.id)}
161
- * >
162
- * {item.name}
163
- * </li>
164
- * )}
165
- * </For>
166
- * </ul>
167
- * );
168
- * }
169
- * ```
170
- *
171
- * @example
172
- * ```tsx
173
- * // With controlled focus
174
- * function ControlledListbox() {
175
- * const [focusedKey, setFocusedKey] = createSignal<string | null>(null);
176
- *
177
- * const virtualFocus = createVirtualFocus({
178
- * items: () => items,
179
- * getKey: (item) => item.id,
180
- * focusedKey: focusedKey,
181
- * onFocusChange: setFocusedKey,
182
- * });
183
- *
184
- * return <ul>...</ul>;
185
- * }
186
- * ```
187
- */
188
- export function createVirtualFocus<T>(
189
- options: VirtualFocusOptions<T>
190
- ): VirtualFocusResult<T> {
191
- const {
192
- items,
193
- getKey,
194
- isDisabled = () => false,
195
- defaultFocusedKey,
196
- focusedKey: controlledFocusedKey,
197
- onFocusChange,
198
- wrap = true,
199
- skipDisabled = true,
200
- orientation = 'vertical',
201
- } = options;
202
-
203
- // During SSR, return minimal implementation
204
- if (isServer) {
205
- const emptyAccessor = () => null;
206
- return {
207
- focusedKey: emptyAccessor,
208
- focusedItem: emptyAccessor,
209
- setFocusedKey: () => {},
210
- focusNext: () => {},
211
- focusPrevious: () => {},
212
- focusFirst: () => {},
213
- focusLast: () => {},
214
- focusPageDown: () => {},
215
- focusPageUp: () => {},
216
- isFocused: () => false,
217
- containerProps: {
218
- 'aria-activedescendant': () => undefined,
219
- onKeyDown: () => {},
220
- },
221
- getItemProps: (key: string) => ({ id: `item-${key}` }),
222
- };
223
- }
224
-
225
- // Internal state for uncontrolled mode
226
- const [internalKey, setInternalKey] = createSignal<string | null>(
227
- defaultFocusedKey ?? null
228
- );
229
-
230
- // Use controlled or uncontrolled value
231
- const focusedKey = controlledFocusedKey ?? internalKey;
232
-
233
- const setFocusedKey = (key: string | null) => {
234
- if (controlledFocusedKey) {
235
- onFocusChange?.(key);
236
- } else {
237
- setInternalKey(key);
238
- onFocusChange?.(key);
239
- }
240
- };
241
-
242
- // Get focused item
243
- const focusedItem = (): T | null => {
244
- const key = focusedKey();
245
- if (!key) return null;
246
- return items().find((item) => getKey(item) === key) ?? null;
247
- };
248
-
249
- // Get valid items (not disabled if skipDisabled is true)
250
- const getValidItems = (): T[] => {
251
- if (!skipDisabled) return items();
252
- return items().filter((item) => !isDisabled(item));
253
- };
254
-
255
- // Get index of key in valid items
256
- const getKeyIndex = (key: string | null): number => {
257
- if (!key) return -1;
258
- const validItems = getValidItems();
259
- return validItems.findIndex((item) => getKey(item) === key);
260
- };
261
-
262
- // Focus by index
263
- const focusByIndex = (index: number): void => {
264
- const validItems = getValidItems();
265
- if (validItems.length === 0) return;
266
-
267
- let targetIndex = index;
268
-
269
- if (wrap) {
270
- targetIndex = ((index % validItems.length) + validItems.length) % validItems.length;
271
- } else {
272
- targetIndex = Math.max(0, Math.min(index, validItems.length - 1));
273
- }
274
-
275
- const item = validItems[targetIndex];
276
- if (item) {
277
- setFocusedKey(getKey(item));
278
- }
279
- };
280
-
281
- const focusNext = (): void => {
282
- const currentIndex = getKeyIndex(focusedKey());
283
- focusByIndex(currentIndex + 1);
284
- };
285
-
286
- const focusPrevious = (): void => {
287
- const currentIndex = getKeyIndex(focusedKey());
288
- if (currentIndex === -1) {
289
- focusByIndex(getValidItems().length - 1);
290
- } else {
291
- focusByIndex(currentIndex - 1);
292
- }
293
- };
294
-
295
- const focusFirst = (): void => {
296
- focusByIndex(0);
297
- };
298
-
299
- const focusLast = (): void => {
300
- focusByIndex(getValidItems().length - 1);
301
- };
302
-
303
- const focusPageDown = (pageSize: number = DEFAULT_PAGE_SIZE): void => {
304
- const currentIndex = getKeyIndex(focusedKey());
305
- focusByIndex(currentIndex + pageSize);
306
- };
307
-
308
- const focusPageUp = (pageSize: number = DEFAULT_PAGE_SIZE): void => {
309
- const currentIndex = getKeyIndex(focusedKey());
310
- if (currentIndex === -1) {
311
- focusByIndex(getValidItems().length - 1);
312
- } else {
313
- focusByIndex(currentIndex - pageSize);
314
- }
315
- };
316
-
317
- const isFocused = (key: string): boolean => {
318
- return focusedKey() === key;
319
- };
320
-
321
- // Keyboard handler
322
- const onKeyDown = (e: KeyboardEvent): void => {
323
- const isVertical = orientation === 'vertical' || orientation === 'both';
324
- const isHorizontal = orientation === 'horizontal' || orientation === 'both';
325
-
326
- switch (e.key) {
327
- case 'ArrowDown':
328
- if (isVertical) {
329
- e.preventDefault();
330
- focusNext();
331
- }
332
- break;
333
- case 'ArrowUp':
334
- if (isVertical) {
335
- e.preventDefault();
336
- focusPrevious();
337
- }
338
- break;
339
- case 'ArrowRight':
340
- if (isHorizontal) {
341
- e.preventDefault();
342
- focusNext();
343
- }
344
- break;
345
- case 'ArrowLeft':
346
- if (isHorizontal) {
347
- e.preventDefault();
348
- focusPrevious();
349
- }
350
- break;
351
- case 'Home':
352
- e.preventDefault();
353
- focusFirst();
354
- break;
355
- case 'End':
356
- e.preventDefault();
357
- focusLast();
358
- break;
359
- case 'PageDown':
360
- e.preventDefault();
361
- focusPageDown();
362
- break;
363
- case 'PageUp':
364
- e.preventDefault();
365
- focusPageUp();
366
- break;
367
- }
368
- };
369
-
370
- const containerProps = {
371
- 'aria-activedescendant': () => {
372
- const key = focusedKey();
373
- return key ? `item-${key}` : undefined;
374
- },
375
- onKeyDown,
376
- };
377
-
378
- const getItemProps = (key: string) => ({
379
- id: `item-${key}`,
380
- });
381
-
382
- return {
383
- focusedKey,
384
- focusedItem,
385
- setFocusedKey,
386
- focusNext,
387
- focusPrevious,
388
- focusFirst,
389
- focusLast,
390
- focusPageDown,
391
- focusPageUp,
392
- isFocused,
393
- containerProps,
394
- getItemProps,
395
- };
396
- }
1
+ /**
2
+ * Virtual focus management for solidaria
3
+ *
4
+ * Provides virtual focus for large collections where tracking DOM focus
5
+ * on every item is impractical. Instead, a single element receives real
6
+ * focus while aria-activedescendant indicates the virtually focused item.
7
+ *
8
+ * This is commonly used in:
9
+ * - Virtualized lists (where not all items are in the DOM)
10
+ * - Large trees
11
+ * - Autocomplete/combobox suggestions
12
+ * - Grid/table navigation
13
+ */
14
+
15
+ import { type Accessor, createSignal, createEffect, onCleanup } from 'solid-js';
16
+ import { isServer } from 'solid-js/web';
17
+
18
+ // ============================================
19
+ // TYPES
20
+ // ============================================
21
+
22
+ export interface VirtualFocusOptions<T> {
23
+ /**
24
+ * The items in the collection.
25
+ */
26
+ items: Accessor<T[]>;
27
+ /**
28
+ * Function to get a unique key for each item.
29
+ */
30
+ getKey: (item: T) => string;
31
+ /**
32
+ * Function to check if an item is disabled.
33
+ */
34
+ isDisabled?: (item: T) => boolean;
35
+ /**
36
+ * Initial focused key.
37
+ */
38
+ defaultFocusedKey?: string;
39
+ /**
40
+ * Controlled focused key.
41
+ */
42
+ focusedKey?: Accessor<string | null>;
43
+ /**
44
+ * Callback when focused key changes.
45
+ */
46
+ onFocusChange?: (key: string | null) => void;
47
+ /**
48
+ * Whether to wrap focus at the edges.
49
+ * @default true
50
+ */
51
+ wrap?: boolean;
52
+ /**
53
+ * Whether to loop through disabled items.
54
+ * @default false
55
+ */
56
+ skipDisabled?: boolean;
57
+ /**
58
+ * Orientation for keyboard navigation.
59
+ * @default 'vertical'
60
+ */
61
+ orientation?: 'horizontal' | 'vertical' | 'both';
62
+ }
63
+
64
+ export interface VirtualFocusResult<T> {
65
+ /**
66
+ * The currently focused key.
67
+ */
68
+ focusedKey: Accessor<string | null>;
69
+ /**
70
+ * The currently focused item (if any).
71
+ */
72
+ focusedItem: Accessor<T | null>;
73
+ /**
74
+ * Set the focused key.
75
+ */
76
+ setFocusedKey: (key: string | null) => void;
77
+ /**
78
+ * Move focus to the next item.
79
+ */
80
+ focusNext: () => void;
81
+ /**
82
+ * Move focus to the previous item.
83
+ */
84
+ focusPrevious: () => void;
85
+ /**
86
+ * Move focus to the first item.
87
+ */
88
+ focusFirst: () => void;
89
+ /**
90
+ * Move focus to the last item.
91
+ */
92
+ focusLast: () => void;
93
+ /**
94
+ * Move focus by a page (for large lists).
95
+ */
96
+ focusPageDown: (pageSize?: number) => void;
97
+ /**
98
+ * Move focus up by a page (for large lists).
99
+ */
100
+ focusPageUp: (pageSize?: number) => void;
101
+ /**
102
+ * Check if a key is the currently focused key.
103
+ */
104
+ isFocused: (key: string) => boolean;
105
+ /**
106
+ * Props to spread on the container element.
107
+ */
108
+ containerProps: {
109
+ 'aria-activedescendant': Accessor<string | undefined>;
110
+ onKeyDown: (e: KeyboardEvent) => void;
111
+ };
112
+ /**
113
+ * Get props for an item element.
114
+ */
115
+ getItemProps: (key: string) => {
116
+ id: string;
117
+ 'aria-selected'?: boolean;
118
+ };
119
+ }
120
+
121
+ // ============================================
122
+ // CONSTANTS
123
+ // ============================================
124
+
125
+ const DEFAULT_PAGE_SIZE = 10;
126
+
127
+ // ============================================
128
+ // HOOK
129
+ // ============================================
130
+
131
+ /**
132
+ * Creates virtual focus management for a collection.
133
+ *
134
+ * Virtual focus uses aria-activedescendant to indicate which item is
135
+ * "focused" while keeping real DOM focus on a container element. This
136
+ * is more performant for large collections and required for virtualized
137
+ * lists where items may not be in the DOM.
138
+ *
139
+ * @example
140
+ * ```tsx
141
+ * function Listbox(props) {
142
+ * const virtualFocus = createVirtualFocus({
143
+ * items: () => props.items,
144
+ * getKey: (item) => item.id,
145
+ * isDisabled: (item) => item.disabled,
146
+ * });
147
+ *
148
+ * return (
149
+ * <ul
150
+ * role="listbox"
151
+ * tabIndex={0}
152
+ * aria-activedescendant={virtualFocus.containerProps['aria-activedescendant']()}
153
+ * onKeyDown={virtualFocus.containerProps.onKeyDown}
154
+ * >
155
+ * <For each={props.items}>
156
+ * {(item) => (
157
+ * <li
158
+ * {...virtualFocus.getItemProps(item.id)}
159
+ * role="option"
160
+ * aria-selected={virtualFocus.isFocused(item.id)}
161
+ * >
162
+ * {item.name}
163
+ * </li>
164
+ * )}
165
+ * </For>
166
+ * </ul>
167
+ * );
168
+ * }
169
+ * ```
170
+ *
171
+ * @example
172
+ * ```tsx
173
+ * // With controlled focus
174
+ * function ControlledListbox() {
175
+ * const [focusedKey, setFocusedKey] = createSignal<string | null>(null);
176
+ *
177
+ * const virtualFocus = createVirtualFocus({
178
+ * items: () => items,
179
+ * getKey: (item) => item.id,
180
+ * focusedKey: focusedKey,
181
+ * onFocusChange: setFocusedKey,
182
+ * });
183
+ *
184
+ * return <ul>...</ul>;
185
+ * }
186
+ * ```
187
+ */
188
+ export function createVirtualFocus<T>(
189
+ options: VirtualFocusOptions<T>
190
+ ): VirtualFocusResult<T> {
191
+ const {
192
+ items,
193
+ getKey,
194
+ isDisabled = () => false,
195
+ defaultFocusedKey,
196
+ focusedKey: controlledFocusedKey,
197
+ onFocusChange,
198
+ wrap = true,
199
+ skipDisabled = true,
200
+ orientation = 'vertical',
201
+ } = options;
202
+
203
+ // During SSR, return minimal implementation
204
+ if (isServer) {
205
+ const emptyAccessor = () => null;
206
+ return {
207
+ focusedKey: emptyAccessor,
208
+ focusedItem: emptyAccessor,
209
+ setFocusedKey: () => {},
210
+ focusNext: () => {},
211
+ focusPrevious: () => {},
212
+ focusFirst: () => {},
213
+ focusLast: () => {},
214
+ focusPageDown: () => {},
215
+ focusPageUp: () => {},
216
+ isFocused: () => false,
217
+ containerProps: {
218
+ 'aria-activedescendant': () => undefined,
219
+ onKeyDown: () => {},
220
+ },
221
+ getItemProps: (key: string) => ({ id: `item-${key}` }),
222
+ };
223
+ }
224
+
225
+ // Internal state for uncontrolled mode
226
+ const [internalKey, setInternalKey] = createSignal<string | null>(
227
+ defaultFocusedKey ?? null
228
+ );
229
+
230
+ // Use controlled or uncontrolled value
231
+ const focusedKey = controlledFocusedKey ?? internalKey;
232
+
233
+ const setFocusedKey = (key: string | null) => {
234
+ if (controlledFocusedKey) {
235
+ onFocusChange?.(key);
236
+ } else {
237
+ setInternalKey(key);
238
+ onFocusChange?.(key);
239
+ }
240
+ };
241
+
242
+ // Get focused item
243
+ const focusedItem = (): T | null => {
244
+ const key = focusedKey();
245
+ if (!key) return null;
246
+ return items().find((item) => getKey(item) === key) ?? null;
247
+ };
248
+
249
+ // Get valid items (not disabled if skipDisabled is true)
250
+ const getValidItems = (): T[] => {
251
+ if (!skipDisabled) return items();
252
+ return items().filter((item) => !isDisabled(item));
253
+ };
254
+
255
+ // Get index of key in valid items
256
+ const getKeyIndex = (key: string | null): number => {
257
+ if (!key) return -1;
258
+ const validItems = getValidItems();
259
+ return validItems.findIndex((item) => getKey(item) === key);
260
+ };
261
+
262
+ // Focus by index
263
+ const focusByIndex = (index: number): void => {
264
+ const validItems = getValidItems();
265
+ if (validItems.length === 0) return;
266
+
267
+ let targetIndex = index;
268
+
269
+ if (wrap) {
270
+ targetIndex = ((index % validItems.length) + validItems.length) % validItems.length;
271
+ } else {
272
+ targetIndex = Math.max(0, Math.min(index, validItems.length - 1));
273
+ }
274
+
275
+ const item = validItems[targetIndex];
276
+ if (item) {
277
+ setFocusedKey(getKey(item));
278
+ }
279
+ };
280
+
281
+ const focusNext = (): void => {
282
+ const currentIndex = getKeyIndex(focusedKey());
283
+ focusByIndex(currentIndex + 1);
284
+ };
285
+
286
+ const focusPrevious = (): void => {
287
+ const currentIndex = getKeyIndex(focusedKey());
288
+ if (currentIndex === -1) {
289
+ focusByIndex(getValidItems().length - 1);
290
+ } else {
291
+ focusByIndex(currentIndex - 1);
292
+ }
293
+ };
294
+
295
+ const focusFirst = (): void => {
296
+ focusByIndex(0);
297
+ };
298
+
299
+ const focusLast = (): void => {
300
+ focusByIndex(getValidItems().length - 1);
301
+ };
302
+
303
+ const focusPageDown = (pageSize: number = DEFAULT_PAGE_SIZE): void => {
304
+ const currentIndex = getKeyIndex(focusedKey());
305
+ focusByIndex(currentIndex + pageSize);
306
+ };
307
+
308
+ const focusPageUp = (pageSize: number = DEFAULT_PAGE_SIZE): void => {
309
+ const currentIndex = getKeyIndex(focusedKey());
310
+ if (currentIndex === -1) {
311
+ focusByIndex(getValidItems().length - 1);
312
+ } else {
313
+ focusByIndex(currentIndex - pageSize);
314
+ }
315
+ };
316
+
317
+ const isFocused = (key: string): boolean => {
318
+ return focusedKey() === key;
319
+ };
320
+
321
+ // Keyboard handler
322
+ const onKeyDown = (e: KeyboardEvent): void => {
323
+ const isVertical = orientation === 'vertical' || orientation === 'both';
324
+ const isHorizontal = orientation === 'horizontal' || orientation === 'both';
325
+
326
+ switch (e.key) {
327
+ case 'ArrowDown':
328
+ if (isVertical) {
329
+ e.preventDefault();
330
+ focusNext();
331
+ }
332
+ break;
333
+ case 'ArrowUp':
334
+ if (isVertical) {
335
+ e.preventDefault();
336
+ focusPrevious();
337
+ }
338
+ break;
339
+ case 'ArrowRight':
340
+ if (isHorizontal) {
341
+ e.preventDefault();
342
+ focusNext();
343
+ }
344
+ break;
345
+ case 'ArrowLeft':
346
+ if (isHorizontal) {
347
+ e.preventDefault();
348
+ focusPrevious();
349
+ }
350
+ break;
351
+ case 'Home':
352
+ e.preventDefault();
353
+ focusFirst();
354
+ break;
355
+ case 'End':
356
+ e.preventDefault();
357
+ focusLast();
358
+ break;
359
+ case 'PageDown':
360
+ e.preventDefault();
361
+ focusPageDown();
362
+ break;
363
+ case 'PageUp':
364
+ e.preventDefault();
365
+ focusPageUp();
366
+ break;
367
+ }
368
+ };
369
+
370
+ const containerProps = {
371
+ 'aria-activedescendant': () => {
372
+ const key = focusedKey();
373
+ return key ? `item-${key}` : undefined;
374
+ },
375
+ onKeyDown,
376
+ };
377
+
378
+ const getItemProps = (key: string) => ({
379
+ id: `item-${key}`,
380
+ });
381
+
382
+ return {
383
+ focusedKey,
384
+ focusedItem,
385
+ setFocusedKey,
386
+ focusNext,
387
+ focusPrevious,
388
+ focusFirst,
389
+ focusLast,
390
+ focusPageDown,
391
+ focusPageUp,
392
+ isFocused,
393
+ containerProps,
394
+ getItemProps,
395
+ };
396
+ }