@proyecto-viviana/solidaria 0.2.2 → 0.2.4

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/dist/autocomplete/createAutocomplete.d.ts +2 -2
  2. package/dist/autocomplete/createAutocomplete.d.ts.map +1 -1
  3. package/dist/index.js +233 -234
  4. package/dist/index.js.map +2 -2
  5. package/dist/index.ssr.js +233 -234
  6. package/dist/index.ssr.js.map +2 -2
  7. package/dist/interactions/PressEvent.d.ts +13 -10
  8. package/dist/interactions/PressEvent.d.ts.map +1 -1
  9. package/dist/interactions/createPress.d.ts.map +1 -1
  10. package/dist/interactions/index.d.ts +1 -1
  11. package/dist/interactions/index.d.ts.map +1 -1
  12. package/dist/select/createHiddenSelect.d.ts.map +1 -1
  13. package/dist/toolbar/createToolbar.d.ts.map +1 -1
  14. package/dist/tooltip/createTooltipTrigger.d.ts.map +1 -1
  15. package/package.json +9 -7
  16. package/src/autocomplete/createAutocomplete.ts +341 -0
  17. package/src/autocomplete/index.ts +9 -0
  18. package/src/breadcrumbs/createBreadcrumbs.ts +196 -0
  19. package/src/breadcrumbs/index.ts +8 -0
  20. package/src/button/createButton.ts +142 -0
  21. package/src/button/createToggleButton.ts +101 -0
  22. package/src/button/index.ts +4 -0
  23. package/src/button/types.ts +78 -0
  24. package/src/calendar/createCalendar.ts +138 -0
  25. package/src/calendar/createCalendarCell.ts +187 -0
  26. package/src/calendar/createCalendarGrid.ts +140 -0
  27. package/src/calendar/createRangeCalendar.ts +136 -0
  28. package/src/calendar/createRangeCalendarCell.ts +186 -0
  29. package/src/calendar/index.ts +34 -0
  30. package/src/checkbox/createCheckbox.ts +135 -0
  31. package/src/checkbox/createCheckboxGroup.ts +137 -0
  32. package/src/checkbox/createCheckboxGroupItem.ts +117 -0
  33. package/src/checkbox/createCheckboxGroupState.ts +193 -0
  34. package/src/checkbox/index.ts +13 -0
  35. package/src/color/createColorArea.ts +314 -0
  36. package/src/color/createColorField.ts +137 -0
  37. package/src/color/createColorSlider.ts +197 -0
  38. package/src/color/createColorSwatch.ts +40 -0
  39. package/src/color/createColorWheel.ts +208 -0
  40. package/src/color/index.ts +24 -0
  41. package/src/color/types.ts +116 -0
  42. package/src/combobox/createComboBox.ts +647 -0
  43. package/src/combobox/index.ts +6 -0
  44. package/src/combobox/intl/en-US.json +7 -0
  45. package/src/combobox/intl/es-ES.json +7 -0
  46. package/src/combobox/intl/index.ts +23 -0
  47. package/src/datepicker/createDateField.ts +154 -0
  48. package/src/datepicker/createDatePicker.ts +206 -0
  49. package/src/datepicker/createDateSegment.ts +229 -0
  50. package/src/datepicker/createTimeField.ts +154 -0
  51. package/src/datepicker/index.ts +28 -0
  52. package/src/dialog/createDialog.ts +120 -0
  53. package/src/dialog/index.ts +2 -0
  54. package/src/dialog/types.ts +19 -0
  55. package/src/disclosure/createDisclosure.ts +131 -0
  56. package/src/disclosure/createDisclosureGroup.ts +62 -0
  57. package/src/disclosure/index.ts +11 -0
  58. package/src/dnd/createDrag.ts +209 -0
  59. package/src/dnd/createDraggableCollection.ts +63 -0
  60. package/src/dnd/createDraggableItem.ts +243 -0
  61. package/src/dnd/createDrop.ts +321 -0
  62. package/src/dnd/createDroppableCollection.ts +293 -0
  63. package/src/dnd/createDroppableItem.ts +213 -0
  64. package/src/dnd/index.ts +47 -0
  65. package/src/dnd/types.ts +89 -0
  66. package/src/dnd/utils.ts +294 -0
  67. package/src/focus/FocusScope.tsx +408 -0
  68. package/src/focus/createAutoFocus.ts +321 -0
  69. package/src/focus/createFocusRestore.ts +313 -0
  70. package/src/focus/createVirtualFocus.ts +396 -0
  71. package/src/focus/index.ts +35 -0
  72. package/src/form/createFormReset.ts +51 -0
  73. package/src/form/createFormValidation.ts +224 -0
  74. package/src/form/index.ts +11 -0
  75. package/src/grid/GridKeyboardDelegate.ts +429 -0
  76. package/src/grid/createGrid.ts +261 -0
  77. package/src/grid/createGridCell.ts +182 -0
  78. package/src/grid/createGridRow.ts +153 -0
  79. package/src/grid/index.ts +18 -0
  80. package/src/grid/types.ts +133 -0
  81. package/src/gridlist/createGridList.ts +185 -0
  82. package/src/gridlist/createGridListItem.ts +180 -0
  83. package/src/gridlist/createGridListSelectionCheckbox.ts +59 -0
  84. package/src/gridlist/index.ts +16 -0
  85. package/src/gridlist/types.ts +81 -0
  86. package/src/i18n/NumberFormatter.ts +266 -0
  87. package/src/i18n/createCollator.ts +79 -0
  88. package/src/i18n/createDateFormatter.ts +83 -0
  89. package/src/i18n/createFilter.ts +131 -0
  90. package/src/i18n/createNumberFormatter.ts +52 -0
  91. package/src/i18n/createStringFormatter.ts +87 -0
  92. package/src/i18n/index.ts +40 -0
  93. package/src/i18n/locale.tsx +188 -0
  94. package/src/i18n/utils.ts +99 -0
  95. package/src/index.ts +670 -0
  96. package/src/interactions/FocusableProvider.tsx +44 -0
  97. package/src/interactions/PressEvent.ts +126 -0
  98. package/src/interactions/createFocus.ts +163 -0
  99. package/src/interactions/createFocusRing.ts +89 -0
  100. package/src/interactions/createFocusWithin.ts +206 -0
  101. package/src/interactions/createFocusable.ts +168 -0
  102. package/src/interactions/createHover.ts +254 -0
  103. package/src/interactions/createInteractionModality.ts +424 -0
  104. package/src/interactions/createKeyboard.ts +82 -0
  105. package/src/interactions/createLongPress.ts +174 -0
  106. package/src/interactions/createMove.ts +289 -0
  107. package/src/interactions/createPress.ts +834 -0
  108. package/src/interactions/index.ts +78 -0
  109. package/src/label/createField.ts +145 -0
  110. package/src/label/createLabel.ts +117 -0
  111. package/src/label/createLabels.ts +50 -0
  112. package/src/label/index.ts +19 -0
  113. package/src/landmark/createLandmark.ts +377 -0
  114. package/src/landmark/index.ts +8 -0
  115. package/src/link/createLink.ts +182 -0
  116. package/src/link/index.ts +1 -0
  117. package/src/listbox/createListBox.ts +269 -0
  118. package/src/listbox/createOption.ts +151 -0
  119. package/src/listbox/index.ts +12 -0
  120. package/src/live-announcer/announce.ts +322 -0
  121. package/src/live-announcer/index.ts +9 -0
  122. package/src/menu/createMenu.ts +396 -0
  123. package/src/menu/createMenuItem.ts +149 -0
  124. package/src/menu/createMenuTrigger.ts +88 -0
  125. package/src/menu/index.ts +18 -0
  126. package/src/meter/createMeter.ts +75 -0
  127. package/src/meter/index.ts +1 -0
  128. package/src/numberfield/createNumberField.ts +268 -0
  129. package/src/numberfield/index.ts +5 -0
  130. package/src/overlays/ariaHideOutside.ts +219 -0
  131. package/src/overlays/createInteractOutside.ts +149 -0
  132. package/src/overlays/createModal.tsx +202 -0
  133. package/src/overlays/createOverlay.ts +155 -0
  134. package/src/overlays/createOverlayTrigger.ts +85 -0
  135. package/src/overlays/createPreventScroll.ts +266 -0
  136. package/src/overlays/index.ts +44 -0
  137. package/src/popover/calculatePosition.ts +766 -0
  138. package/src/popover/createOverlayPosition.ts +356 -0
  139. package/src/popover/createPopover.ts +170 -0
  140. package/src/popover/index.ts +24 -0
  141. package/src/progress/createProgressBar.ts +128 -0
  142. package/src/progress/index.ts +5 -0
  143. package/src/radio/createRadio.ts +287 -0
  144. package/src/radio/createRadioGroup.ts +189 -0
  145. package/src/radio/createRadioGroupState.ts +201 -0
  146. package/src/radio/index.ts +23 -0
  147. package/src/searchfield/createSearchField.ts +186 -0
  148. package/src/searchfield/index.ts +2 -0
  149. package/src/select/createHiddenSelect.tsx +236 -0
  150. package/src/select/createSelect.ts +395 -0
  151. package/src/select/index.ts +14 -0
  152. package/src/selection/createTypeSelect.ts +201 -0
  153. package/src/selection/index.ts +6 -0
  154. package/src/separator/createSeparator.ts +82 -0
  155. package/src/separator/index.ts +6 -0
  156. package/src/slider/createSlider.ts +349 -0
  157. package/src/slider/index.ts +2 -0
  158. package/src/ssr/index.tsx +370 -0
  159. package/src/switch/createSwitch.ts +70 -0
  160. package/src/switch/index.ts +1 -0
  161. package/src/table/createTable.ts +526 -0
  162. package/src/table/createTableCell.ts +147 -0
  163. package/src/table/createTableColumnHeader.ts +115 -0
  164. package/src/table/createTableHeaderRow.ts +40 -0
  165. package/src/table/createTableRow.ts +155 -0
  166. package/src/table/createTableRowGroup.ts +32 -0
  167. package/src/table/createTableSelectAllCheckbox.ts +73 -0
  168. package/src/table/createTableSelectionCheckbox.ts +59 -0
  169. package/src/table/index.ts +30 -0
  170. package/src/table/types.ts +165 -0
  171. package/src/tabs/createTabs.ts +472 -0
  172. package/src/tabs/index.ts +14 -0
  173. package/src/tag/createTag.ts +194 -0
  174. package/src/tag/createTagGroup.ts +154 -0
  175. package/src/tag/index.ts +12 -0
  176. package/src/textfield/createTextField.ts +198 -0
  177. package/src/textfield/index.ts +5 -0
  178. package/src/toast/createToast.ts +118 -0
  179. package/src/toast/createToastRegion.ts +100 -0
  180. package/src/toast/index.ts +11 -0
  181. package/src/toggle/createToggle.ts +223 -0
  182. package/src/toggle/createToggleState.ts +94 -0
  183. package/src/toggle/index.ts +7 -0
  184. package/src/toolbar/createToolbar.ts +369 -0
  185. package/src/toolbar/index.ts +6 -0
  186. package/src/tooltip/createTooltip.ts +79 -0
  187. package/src/tooltip/createTooltipTrigger.ts +222 -0
  188. package/src/tooltip/index.ts +6 -0
  189. package/src/tree/createTree.ts +246 -0
  190. package/src/tree/createTreeItem.ts +233 -0
  191. package/src/tree/createTreeSelectionCheckbox.ts +68 -0
  192. package/src/tree/index.ts +16 -0
  193. package/src/tree/types.ts +87 -0
  194. package/src/utils/createDescription.ts +137 -0
  195. package/src/utils/dom.ts +327 -0
  196. package/src/utils/env.ts +54 -0
  197. package/src/utils/events.ts +106 -0
  198. package/src/utils/filterDOMProps.ts +116 -0
  199. package/src/utils/focus.ts +151 -0
  200. package/src/utils/geometry.ts +115 -0
  201. package/src/utils/globalListeners.ts +142 -0
  202. package/src/utils/index.ts +80 -0
  203. package/src/utils/mergeProps.ts +52 -0
  204. package/src/utils/platform.ts +52 -0
  205. package/src/utils/reactivity.ts +36 -0
  206. package/src/utils/textSelection.ts +114 -0
  207. package/src/visually-hidden/createVisuallyHidden.ts +124 -0
  208. package/src/visually-hidden/index.ts +6 -0
  209. package/dist/index.jsx +0 -15845
  210. package/dist/index.jsx.map +0 -7
@@ -0,0 +1,313 @@
1
+ /**
2
+ * Focus restoration utilities for solidaria
3
+ *
4
+ * Provides enhanced focus restoration with retry logic, cross-scope tracking,
5
+ * and safe restoration patterns.
6
+ */
7
+
8
+ import { createEffect, onCleanup, onMount } from 'solid-js';
9
+ import { isServer } from 'solid-js/web';
10
+ import { getOwnerDocument } from '../utils';
11
+ import { focusSafely } from '../utils/focus';
12
+
13
+ // ============================================
14
+ // TYPES
15
+ // ============================================
16
+
17
+ export interface FocusRestoreOptions {
18
+ /**
19
+ * Whether to restore focus when the component unmounts.
20
+ * @default true
21
+ */
22
+ restoreOnUnmount?: boolean;
23
+ /**
24
+ * Maximum number of retries if the element is not in the DOM.
25
+ * @default 3
26
+ */
27
+ maxRetries?: number;
28
+ /**
29
+ * Delay between retries in milliseconds.
30
+ * @default 50
31
+ */
32
+ retryDelay?: number;
33
+ /**
34
+ * Callback when focus is successfully restored.
35
+ */
36
+ onRestore?: (element: HTMLElement) => void;
37
+ /**
38
+ * Callback when focus restoration fails.
39
+ */
40
+ onRestoreFailed?: () => void;
41
+ /**
42
+ * Whether to prevent scrolling when restoring focus.
43
+ * @default true
44
+ */
45
+ preventScroll?: boolean;
46
+ }
47
+
48
+ export interface FocusRestoreResult {
49
+ /**
50
+ * Manually restore focus to the saved element.
51
+ */
52
+ restore: () => boolean;
53
+ /**
54
+ * Get the saved element (if any).
55
+ */
56
+ getSavedElement: () => HTMLElement | null;
57
+ /**
58
+ * Save the currently focused element.
59
+ */
60
+ saveCurrentFocus: () => void;
61
+ /**
62
+ * Clear the saved element without restoring.
63
+ */
64
+ clear: () => void;
65
+ }
66
+
67
+ // ============================================
68
+ // GLOBAL FOCUS STACK
69
+ // ============================================
70
+
71
+ // Stack to track focus history across scopes
72
+ const focusStack: HTMLElement[] = [];
73
+
74
+ /**
75
+ * Push an element onto the focus stack.
76
+ */
77
+ export function pushFocusStack(element: HTMLElement): void {
78
+ focusStack.push(element);
79
+ }
80
+
81
+ /**
82
+ * Pop the last element from the focus stack.
83
+ */
84
+ export function popFocusStack(): HTMLElement | undefined {
85
+ return focusStack.pop();
86
+ }
87
+
88
+ /**
89
+ * Get the current focus stack length.
90
+ */
91
+ export function getFocusStackLength(): number {
92
+ return focusStack.length;
93
+ }
94
+
95
+ /**
96
+ * Clear the entire focus stack.
97
+ */
98
+ export function clearFocusStack(): void {
99
+ focusStack.length = 0;
100
+ }
101
+
102
+ // ============================================
103
+ // UTILITIES
104
+ // ============================================
105
+
106
+ /**
107
+ * Gets the active element, accounting for shadow DOM.
108
+ */
109
+ function getActiveElement(doc: Document): HTMLElement | null {
110
+ let activeElement = doc.activeElement as HTMLElement | null;
111
+ while (activeElement?.shadowRoot?.activeElement) {
112
+ activeElement = activeElement.shadowRoot.activeElement as HTMLElement;
113
+ }
114
+ return activeElement;
115
+ }
116
+
117
+ /**
118
+ * Checks if an element is still valid for focus restoration.
119
+ */
120
+ function isValidForRestore(element: HTMLElement | null): boolean {
121
+ if (!element) return false;
122
+ if (!document.body.contains(element)) return false;
123
+ if (element.hasAttribute('disabled')) return false;
124
+ if (element.getAttribute('aria-disabled') === 'true') return false;
125
+ if (element.getAttribute('aria-hidden') === 'true') return false;
126
+ return true;
127
+ }
128
+
129
+ /**
130
+ * Attempts to restore focus with retries.
131
+ */
132
+ function tryRestoreFocus(
133
+ element: HTMLElement | null,
134
+ options: Required<Pick<FocusRestoreOptions, 'maxRetries' | 'retryDelay' | 'preventScroll' | 'onRestore' | 'onRestoreFailed'>>
135
+ ): void {
136
+ const { maxRetries, retryDelay, preventScroll, onRestore, onRestoreFailed } = options;
137
+ let attempts = 0;
138
+
139
+ const attempt = () => {
140
+ if (!element) {
141
+ onRestoreFailed?.();
142
+ return;
143
+ }
144
+
145
+ if (isValidForRestore(element)) {
146
+ if (preventScroll) {
147
+ focusSafely(element);
148
+ } else {
149
+ element.focus();
150
+ }
151
+ onRestore?.(element);
152
+ return;
153
+ }
154
+
155
+ attempts++;
156
+ if (attempts < maxRetries) {
157
+ setTimeout(attempt, retryDelay);
158
+ } else {
159
+ onRestoreFailed?.();
160
+ }
161
+ };
162
+
163
+ // Use requestAnimationFrame for the first attempt to ensure DOM is ready
164
+ requestAnimationFrame(attempt);
165
+ }
166
+
167
+ // ============================================
168
+ // HOOK
169
+ // ============================================
170
+
171
+ /**
172
+ * Creates a focus restoration manager.
173
+ *
174
+ * This hook saves the currently focused element when mounted and provides
175
+ * methods to restore focus later, with retry logic for reliability.
176
+ *
177
+ * @example
178
+ * ```tsx
179
+ * function Modal(props) {
180
+ * const focusRestore = createFocusRestore({
181
+ * restoreOnUnmount: true,
182
+ * onRestore: () => console.log('Focus restored'),
183
+ * });
184
+ *
185
+ * return (
186
+ * <div role="dialog">
187
+ * {props.children}
188
+ * <button onClick={() => focusRestore.restore()}>
189
+ * Close
190
+ * </button>
191
+ * </div>
192
+ * );
193
+ * }
194
+ * ```
195
+ *
196
+ * @example
197
+ * ```tsx
198
+ * // Manual focus management
199
+ * function Dropdown() {
200
+ * const focusRestore = createFocusRestore({ restoreOnUnmount: false });
201
+ *
202
+ * const onOpen = () => {
203
+ * focusRestore.saveCurrentFocus();
204
+ * // Focus dropdown content
205
+ * };
206
+ *
207
+ * const onClose = () => {
208
+ * focusRestore.restore();
209
+ * };
210
+ *
211
+ * return <div>...</div>;
212
+ * }
213
+ * ```
214
+ */
215
+ export function createFocusRestore(
216
+ options: FocusRestoreOptions = {}
217
+ ): FocusRestoreResult {
218
+ const {
219
+ restoreOnUnmount = true,
220
+ maxRetries = 3,
221
+ retryDelay = 50,
222
+ onRestore,
223
+ onRestoreFailed,
224
+ preventScroll = true,
225
+ } = options;
226
+
227
+ // During SSR, return no-op functions
228
+ if (isServer) {
229
+ return {
230
+ restore: () => false,
231
+ getSavedElement: () => null,
232
+ saveCurrentFocus: () => {},
233
+ clear: () => {},
234
+ };
235
+ }
236
+
237
+ let savedElement: HTMLElement | null = null;
238
+
239
+ // Save focus on mount
240
+ onMount(() => {
241
+ saveCurrentFocus();
242
+ });
243
+
244
+ // Restore focus on cleanup
245
+ onCleanup(() => {
246
+ if (restoreOnUnmount && savedElement) {
247
+ tryRestoreFocus(savedElement, {
248
+ maxRetries,
249
+ retryDelay,
250
+ preventScroll,
251
+ onRestore: onRestore ?? (() => {}),
252
+ onRestoreFailed: onRestoreFailed ?? (() => {}),
253
+ });
254
+ }
255
+ });
256
+
257
+ function saveCurrentFocus(): void {
258
+ const doc = typeof document !== 'undefined' ? document : null;
259
+ if (!doc) return;
260
+
261
+ const active = getActiveElement(doc);
262
+ if (active && active !== doc.body) {
263
+ savedElement = active;
264
+ pushFocusStack(active);
265
+ }
266
+ }
267
+
268
+ function restore(): boolean {
269
+ if (!savedElement) return false;
270
+
271
+ if (isValidForRestore(savedElement)) {
272
+ if (preventScroll) {
273
+ focusSafely(savedElement);
274
+ } else {
275
+ savedElement.focus();
276
+ }
277
+ onRestore?.(savedElement);
278
+ return true;
279
+ }
280
+
281
+ // Try the focus stack
282
+ while (focusStack.length > 0) {
283
+ const stackElement = popFocusStack();
284
+ if (stackElement && isValidForRestore(stackElement)) {
285
+ if (preventScroll) {
286
+ focusSafely(stackElement);
287
+ } else {
288
+ stackElement.focus();
289
+ }
290
+ onRestore?.(stackElement);
291
+ return true;
292
+ }
293
+ }
294
+
295
+ onRestoreFailed?.();
296
+ return false;
297
+ }
298
+
299
+ function getSavedElement(): HTMLElement | null {
300
+ return savedElement;
301
+ }
302
+
303
+ function clear(): void {
304
+ savedElement = null;
305
+ }
306
+
307
+ return {
308
+ restore,
309
+ getSavedElement,
310
+ saveCurrentFocus,
311
+ clear,
312
+ };
313
+ }
@@ -0,0 +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
+ }