@proyecto-viviana/solidaria 0.2.5 → 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,405 @@
1
- /**
2
- * Provides the behavior and accessibility implementation for a menu component.
3
- * A menu displays a list of actions or options that a user can choose.
4
- * Based on @react-aria/menu useMenu.
5
- */
6
-
7
- import { createEffect, onCleanup, type JSX, type Accessor } from 'solid-js';
8
- import { createFocusWithin } from '../interactions/createFocusWithin';
9
- import { createLabel } from '../label/createLabel';
10
- import { createTypeSelect } from '../selection/createTypeSelect';
11
- import { filterDOMProps } from '../utils/filterDOMProps';
12
- import { mergeProps } from '../utils/mergeProps';
13
- import { createId } from '../ssr';
14
- import { access, type MaybeAccessor } from '../utils/reactivity';
15
- import { isDevEnv } from '../utils/env';
16
- import type { MenuState, Key, Collection } from '@proyecto-viviana/solid-stately';
17
-
18
- /**
19
- * Default number of items to skip for page up/down when DOM measurement is not available.
20
- */
21
- const DEFAULT_PAGE_SIZE = 10;
22
-
23
- /**
24
- * Find the next non-disabled key in a collection.
25
- */
26
- function findNextNonDisabledKey<T>(
27
- collection: Collection<T>,
28
- currentKey: Key | null,
29
- direction: 'next' | 'prev',
30
- isDisabled: (key: Key) => boolean,
31
- wrap: boolean
32
- ): Key | null {
33
- const getNextKey = direction === 'next'
34
- ? (key: Key) => collection.getKeyAfter(key)
35
- : (key: Key) => collection.getKeyBefore(key);
36
-
37
- const getFirstKey = direction === 'next'
38
- ? () => collection.getFirstKey()
39
- : () => collection.getLastKey();
40
-
41
- let nextKey = currentKey != null ? getNextKey(currentKey) : getFirstKey();
42
-
43
- // Skip disabled keys
44
- while (nextKey != null && isDisabled(nextKey)) {
45
- nextKey = getNextKey(nextKey);
46
- }
47
-
48
- // If we've reached the end and wrapping is enabled
49
- if (nextKey == null && wrap) {
50
- nextKey = getFirstKey();
51
- // Skip disabled keys from the start
52
- while (nextKey != null && isDisabled(nextKey)) {
53
- nextKey = getNextKey(nextKey);
54
- }
55
- }
56
-
57
- return nextKey;
58
- }
59
-
60
- export interface AriaMenuProps {
61
- /** An ID for the menu. */
62
- id?: string;
63
- /** Whether the menu is disabled. */
64
- isDisabled?: boolean;
65
- /** The label for the menu. */
66
- label?: JSX.Element;
67
- /** An accessible label for the menu when no visible label is provided. */
68
- 'aria-label'?: string;
69
- /** The ID of an element that labels the menu. */
70
- 'aria-labelledby'?: string;
71
- /** The ID of an element that describes the menu. */
72
- 'aria-describedby'?: string;
73
- /** Handler called when focus moves into the menu. */
74
- onFocus?: (e: FocusEvent) => void;
75
- /** Handler called when focus moves out of the menu. */
76
- onBlur?: (e: FocusEvent) => void;
77
- /** Handler called when the focus state changes. */
78
- onFocusChange?: (isFocused: boolean) => void;
79
- /** Handler called when an item is activated (pressed). */
80
- onAction?: (key: Key) => void;
81
- /** Handler called when the menu should close. */
82
- onClose?: () => void;
83
- /** Whether focus should automatically wrap around. */
84
- shouldFocusWrap?: boolean;
85
- /** Whether to auto-focus the first item when the menu opens. */
86
- autoFocus?: boolean | 'first' | 'last';
87
- /** Whether type-to-select is disabled. @default false */
88
- disallowTypeAhead?: boolean;
89
- }
90
-
91
- export interface MenuAria {
92
- /** Props for the menu element. */
93
- menuProps: JSX.HTMLAttributes<HTMLElement>;
94
- /** Props for the menu's label element (if any). */
95
- labelProps: JSX.HTMLAttributes<HTMLElement>;
96
- }
97
-
98
- // Shared data between menu and menu items
99
- const menuData = new WeakMap<object, MenuData>();
100
-
101
- interface MenuData {
102
- id: string;
103
- onAction?: (key: Key) => void;
104
- onClose?: () => void;
105
- }
106
-
107
- export function getMenuData(state: MenuState): MenuData | undefined {
108
- return menuData.get(state);
109
- }
110
-
111
- /**
112
- * Provides the behavior and accessibility implementation for a menu component.
113
- * A menu displays a list of actions or options that a user can choose.
114
- */
115
- export function createMenu<T>(
116
- props: MaybeAccessor<AriaMenuProps>,
117
- state: MenuState<T>,
118
- ref?: Accessor<HTMLElement | null>
119
- ): MenuAria {
120
- const getProps = () => access(props);
121
- const id = createId(getProps().id);
122
-
123
- // Development-time warning for missing accessibility labels
124
- if (isDevEnv()) {
125
- const p = getProps();
126
- if (!p.label && !p['aria-label'] && !p['aria-labelledby']) {
127
- console.warn(
128
- '[solidaria] A Menu requires an aria-label or aria-labelledby attribute for accessibility.'
129
- );
130
- }
131
- }
132
-
133
- // Filter DOM props
134
- const domProps = () => filterDOMProps(getProps() as unknown as Record<string, unknown>, { labelable: true });
135
-
136
- // Share data with child menu items
137
- createEffect(() => {
138
- const p = getProps();
139
- menuData.set(state, {
140
- id,
141
- onAction: p.onAction,
142
- onClose: p.onClose,
143
- });
144
-
145
- onCleanup(() => {
146
- menuData.delete(state);
147
- });
148
- });
149
-
150
- // Handle focus within
151
- const { focusWithinProps } = createFocusWithin({
152
- onFocusWithin: (e) => getProps().onFocus?.(e),
153
- onBlurWithin: (e) => getProps().onBlur?.(e),
154
- onFocusWithinChange: (isFocused) => {
155
- getProps().onFocusChange?.(isFocused);
156
- state.setFocused(isFocused);
157
- },
158
- });
159
-
160
- // Label handling
161
- const { labelProps, fieldProps } = createLabel({
162
- get id() {
163
- return id;
164
- },
165
- get label() {
166
- return getProps().label;
167
- },
168
- get 'aria-label'() {
169
- return getProps()['aria-label'];
170
- },
171
- get 'aria-labelledby'() {
172
- return getProps()['aria-labelledby'];
173
- },
174
- labelElementType: 'span',
175
- });
176
-
177
- // Type-to-select
178
- const { typeSelectProps } = createTypeSelect({
179
- collection: () => state.collection(),
180
- focusedKey: () => state.focusedKey(),
181
- onFocusedKeyChange: (key) => state.setFocusedKey(key),
182
- isKeyDisabled: (key) => state.isDisabled(key),
183
- get isDisabled() {
184
- return getProps().disallowTypeAhead ?? false;
185
- },
186
- });
187
-
188
- // Keyboard navigation
189
- const onKeyDown: JSX.EventHandler<HTMLElement, KeyboardEvent> = (e) => {
190
- if (getProps().isDisabled) return;
191
-
192
- const collection = state.collection();
193
- const p = getProps();
194
- const wrap = p.shouldFocusWrap ?? false;
195
-
196
- // Use state.isDisabled which properly checks the disabledKeys accessor
197
- const isDisabled = (key: Key) => state.isDisabled(key);
198
-
199
- switch (e.key) {
200
- case 'ArrowDown': {
201
- e.preventDefault();
202
- const currentKey = state.focusedKey();
203
- const nextKey = findNextNonDisabledKey(collection, currentKey, 'next', isDisabled, wrap);
204
- if (nextKey != null) {
205
- state.setFocusedKey(nextKey);
206
- }
207
- break;
208
- }
209
- case 'ArrowUp': {
210
- e.preventDefault();
211
- const currentKey = state.focusedKey();
212
- const prevKey = findNextNonDisabledKey(collection, currentKey, 'prev', isDisabled, wrap);
213
- if (prevKey != null) {
214
- state.setFocusedKey(prevKey);
215
- }
216
- break;
217
- }
218
- case 'Home': {
219
- e.preventDefault();
220
- // Find first non-disabled key
221
- let firstKey = collection.getFirstKey();
222
- while (firstKey != null && isDisabled(firstKey)) {
223
- firstKey = collection.getKeyAfter(firstKey);
224
- }
225
- if (firstKey != null) {
226
- state.setFocusedKey(firstKey);
227
- }
228
- break;
229
- }
230
- case 'End': {
231
- e.preventDefault();
232
- // Find last non-disabled key
233
- let lastKey = collection.getLastKey();
234
- while (lastKey != null && isDisabled(lastKey)) {
235
- lastKey = collection.getKeyBefore(lastKey);
236
- }
237
- if (lastKey != null) {
238
- state.setFocusedKey(lastKey);
239
- }
240
- break;
241
- }
242
- case ' ':
243
- case 'Enter': {
244
- e.preventDefault();
245
- const focusedKey = state.focusedKey();
246
- // Don't activate disabled items
247
- if (focusedKey != null && !isDisabled(focusedKey)) {
248
- p.onAction?.(focusedKey);
249
- p.onClose?.();
250
- }
251
- break;
252
- }
253
- case 'Escape': {
254
- e.preventDefault();
255
- p.onClose?.();
256
- break;
257
- }
258
- case 'PageDown': {
259
- e.preventDefault();
260
- const currentKey = state.focusedKey();
261
- const el = ref?.();
262
-
263
- if (el) {
264
- // Use DOM measurements to calculate how many items fit in a page
265
- const visibleHeight = el.clientHeight;
266
- let traveled = 0;
267
- let targetKey = currentKey;
268
-
269
- while (targetKey != null && traveled < visibleHeight) {
270
- const nextKey = collection.getKeyAfter(targetKey);
271
- if (nextKey == null) break;
272
-
273
- // Try to measure the item height
274
- const itemElement = el.querySelector(`[data-key="${targetKey}"]`);
275
- traveled += itemElement?.clientHeight ?? 32;
276
-
277
- // Skip disabled items
278
- if (!isDisabled(nextKey)) {
279
- targetKey = nextKey;
280
- } else {
281
- // Skip over disabled items without counting them
282
- const afterDisabled = findNextNonDisabledKey(collection, nextKey, 'next', isDisabled, false);
283
- if (afterDisabled != null) {
284
- targetKey = afterDisabled;
285
- } else {
286
- break;
287
- }
288
- }
289
- }
290
-
291
- if (targetKey != null && targetKey !== currentKey) {
292
- state.setFocusedKey(targetKey);
293
- }
294
- } else {
295
- // Fallback: move by DEFAULT_PAGE_SIZE items
296
- let count = DEFAULT_PAGE_SIZE;
297
- let targetKey = currentKey;
298
-
299
- while (count > 0 && targetKey != null) {
300
- const nextKey = findNextNonDisabledKey(collection, targetKey, 'next', isDisabled, false);
301
- if (nextKey == null) break;
302
- targetKey = nextKey;
303
- count--;
304
- }
305
-
306
- if (targetKey != null) {
307
- state.setFocusedKey(targetKey);
308
- }
309
- }
310
- break;
311
- }
312
- case 'PageUp': {
313
- e.preventDefault();
314
- const currentKey = state.focusedKey();
315
- const el = ref?.();
316
-
317
- if (el) {
318
- // Use DOM measurements to calculate how many items fit in a page
319
- const visibleHeight = el.clientHeight;
320
- let traveled = 0;
321
- let targetKey = currentKey;
322
-
323
- while (targetKey != null && traveled < visibleHeight) {
324
- const prevKey = collection.getKeyBefore(targetKey);
325
- if (prevKey == null) break;
326
-
327
- // Try to measure the item height
328
- const itemElement = el.querySelector(`[data-key="${targetKey}"]`);
329
- traveled += itemElement?.clientHeight ?? 32;
330
-
331
- // Skip disabled items
332
- if (!isDisabled(prevKey)) {
333
- targetKey = prevKey;
334
- } else {
335
- // Skip over disabled items without counting them
336
- const beforeDisabled = findNextNonDisabledKey(collection, prevKey, 'prev', isDisabled, false);
337
- if (beforeDisabled != null) {
338
- targetKey = beforeDisabled;
339
- } else {
340
- break;
341
- }
342
- }
343
- }
344
-
345
- if (targetKey != null && targetKey !== currentKey) {
346
- state.setFocusedKey(targetKey);
347
- }
348
- } else {
349
- // Fallback: move by DEFAULT_PAGE_SIZE items
350
- let count = DEFAULT_PAGE_SIZE;
351
- let targetKey = currentKey;
352
-
353
- while (count > 0 && targetKey != null) {
354
- const prevKey = findNextNonDisabledKey(collection, targetKey, 'prev', isDisabled, false);
355
- if (prevKey == null) break;
356
- targetKey = prevKey;
357
- count--;
358
- }
359
-
360
- if (targetKey != null) {
361
- state.setFocusedKey(targetKey);
362
- }
363
- }
364
- break;
365
- }
366
- }
367
- };
368
-
369
- return {
370
- get labelProps() {
371
- return labelProps as JSX.HTMLAttributes<HTMLElement>;
372
- },
373
- get menuProps() {
374
- const p = getProps();
375
-
376
- const baseProps = mergeProps(
377
- domProps(),
378
- focusWithinProps as Record<string, unknown>,
379
- fieldProps as Record<string, unknown>,
380
- {
381
- role: 'menu',
382
- tabIndex: p.isDisabled ? undefined : 0,
383
- 'aria-disabled': p.isDisabled || undefined,
384
- onKeyDown,
385
- } as Record<string, unknown>
386
- );
387
-
388
- // Add type-select props if enabled
389
- if (!p.disallowTypeAhead) {
390
- return mergeProps(baseProps, typeSelectProps as Record<string, unknown>) as JSX.HTMLAttributes<HTMLElement>;
391
- }
392
-
393
- return baseProps as JSX.HTMLAttributes<HTMLElement>;
394
- },
395
- };
396
- }
1
+ /**
2
+ * Provides the behavior and accessibility implementation for a menu component.
3
+ * A menu displays a list of actions or options that a user can choose.
4
+ * Based on @react-aria/menu useMenu.
5
+ */
6
+
7
+ import { createEffect, onCleanup, type JSX, type Accessor } from 'solid-js';
8
+ import { createFocusWithin } from '../interactions/createFocusWithin';
9
+ import { createLabel } from '../label/createLabel';
10
+ import { createTypeSelect } from '../selection/createTypeSelect';
11
+ import { filterDOMProps } from '../utils/filterDOMProps';
12
+ import { mergeProps } from '../utils/mergeProps';
13
+ import { createId } from '../ssr';
14
+ import { access, type MaybeAccessor } from '../utils/reactivity';
15
+ import { isDevEnv } from '../utils/env';
16
+ import type { MenuState, Key, Collection } from '@proyecto-viviana/solid-stately';
17
+
18
+ /**
19
+ * Default number of items to skip for page up/down when DOM measurement is not available.
20
+ */
21
+ const DEFAULT_PAGE_SIZE = 10;
22
+
23
+ /**
24
+ * Find the next non-disabled key in a collection.
25
+ */
26
+ function findNextNonDisabledKey<T>(
27
+ collection: Collection<T>,
28
+ currentKey: Key | null,
29
+ direction: 'next' | 'prev',
30
+ isDisabled: (key: Key) => boolean,
31
+ wrap: boolean
32
+ ): Key | null {
33
+ const getNextKey = direction === 'next'
34
+ ? (key: Key) => collection.getKeyAfter(key)
35
+ : (key: Key) => collection.getKeyBefore(key);
36
+
37
+ const getFirstKey = direction === 'next'
38
+ ? () => collection.getFirstKey()
39
+ : () => collection.getLastKey();
40
+
41
+ let nextKey = currentKey != null ? getNextKey(currentKey) : getFirstKey();
42
+
43
+ // Skip disabled keys
44
+ while (nextKey != null && isDisabled(nextKey)) {
45
+ nextKey = getNextKey(nextKey);
46
+ }
47
+
48
+ // If we've reached the end and wrapping is enabled
49
+ if (nextKey == null && wrap) {
50
+ nextKey = getFirstKey();
51
+ // Skip disabled keys from the start
52
+ while (nextKey != null && isDisabled(nextKey)) {
53
+ nextKey = getNextKey(nextKey);
54
+ }
55
+ }
56
+
57
+ return nextKey;
58
+ }
59
+
60
+ export interface AriaMenuProps {
61
+ /** An ID for the menu. */
62
+ id?: string;
63
+ /** Whether the menu is disabled. */
64
+ isDisabled?: boolean;
65
+ /** The label for the menu. */
66
+ label?: JSX.Element;
67
+ /** An accessible label for the menu when no visible label is provided. */
68
+ 'aria-label'?: string;
69
+ /** The ID of an element that labels the menu. */
70
+ 'aria-labelledby'?: string;
71
+ /** The ID of an element that describes the menu. */
72
+ 'aria-describedby'?: string;
73
+ /** Handler called when focus moves into the menu. */
74
+ onFocus?: (e: FocusEvent) => void;
75
+ /** Handler called when focus moves out of the menu. */
76
+ onBlur?: (e: FocusEvent) => void;
77
+ /** Handler called when the focus state changes. */
78
+ onFocusChange?: (isFocused: boolean) => void;
79
+ /** Handler called when an item is activated (pressed). */
80
+ onAction?: (key: Key) => void;
81
+ /** Handler called when the menu should close. */
82
+ onClose?: () => void;
83
+ /** Whether focus should automatically wrap around. */
84
+ shouldFocusWrap?: boolean;
85
+ /** Whether to auto-focus the first item when the menu opens. */
86
+ autoFocus?: boolean | 'first' | 'last';
87
+ /** Whether type-to-select is disabled. @default false */
88
+ disallowTypeAhead?: boolean;
89
+ }
90
+
91
+ export interface MenuAria {
92
+ /** Props for the menu element. */
93
+ menuProps: JSX.HTMLAttributes<HTMLElement>;
94
+ /** Props for the menu's label element (if any). */
95
+ labelProps: JSX.HTMLAttributes<HTMLElement>;
96
+ }
97
+
98
+ // Shared data between menu and menu items
99
+ const menuData = new WeakMap<object, MenuData>();
100
+
101
+ interface MenuData {
102
+ id: string;
103
+ onAction?: (key: Key) => void;
104
+ onClose?: () => void;
105
+ isDisabled?: boolean;
106
+ }
107
+
108
+ export function getMenuData(state: MenuState): MenuData | undefined {
109
+ return menuData.get(state);
110
+ }
111
+
112
+ /**
113
+ * Provides the behavior and accessibility implementation for a menu component.
114
+ * A menu displays a list of actions or options that a user can choose.
115
+ */
116
+ export function createMenu<T>(
117
+ props: MaybeAccessor<AriaMenuProps>,
118
+ state: MenuState<T>,
119
+ ref?: Accessor<HTMLElement | null>
120
+ ): MenuAria {
121
+ const getProps = () => access(props);
122
+ const id = createId(getProps().id);
123
+
124
+ // Development-time warning for missing accessibility labels
125
+ if (isDevEnv()) {
126
+ const p = getProps();
127
+ if (!p.label && !p['aria-label'] && !p['aria-labelledby']) {
128
+ console.warn(
129
+ '[solidaria] A Menu requires an aria-label or aria-labelledby attribute for accessibility.'
130
+ );
131
+ }
132
+ }
133
+
134
+ // Filter DOM props
135
+ const domProps = () => filterDOMProps(getProps() as unknown as Record<string, unknown>, { labelable: true });
136
+
137
+ const updateSharedData = () => {
138
+ const p = getProps();
139
+ menuData.set(state, {
140
+ id,
141
+ onAction: p.onAction,
142
+ onClose: p.onClose,
143
+ isDisabled: p.isDisabled,
144
+ });
145
+ };
146
+
147
+ // Ensure menu items created in the same render pass can read parent metadata.
148
+ updateSharedData();
149
+
150
+ // Share data with child menu items
151
+ createEffect(() => {
152
+ updateSharedData();
153
+
154
+ onCleanup(() => {
155
+ menuData.delete(state);
156
+ });
157
+ });
158
+
159
+ // Handle focus within
160
+ const { focusWithinProps } = createFocusWithin({
161
+ onFocusWithin: (e) => getProps().onFocus?.(e),
162
+ onBlurWithin: (e) => getProps().onBlur?.(e),
163
+ onFocusWithinChange: (isFocused) => {
164
+ getProps().onFocusChange?.(isFocused);
165
+ state.setFocused(isFocused);
166
+ },
167
+ });
168
+
169
+ // Label handling
170
+ const { labelProps, fieldProps } = createLabel({
171
+ get id() {
172
+ return id;
173
+ },
174
+ get label() {
175
+ return getProps().label;
176
+ },
177
+ get 'aria-label'() {
178
+ return getProps()['aria-label'];
179
+ },
180
+ get 'aria-labelledby'() {
181
+ return getProps()['aria-labelledby'];
182
+ },
183
+ labelElementType: 'span',
184
+ });
185
+
186
+ // Type-to-select
187
+ const { typeSelectProps } = createTypeSelect({
188
+ collection: () => state.collection(),
189
+ focusedKey: () => state.focusedKey(),
190
+ onFocusedKeyChange: (key) => state.setFocusedKey(key),
191
+ isKeyDisabled: (key) => state.isDisabled(key),
192
+ get isDisabled() {
193
+ return getProps().disallowTypeAhead ?? false;
194
+ },
195
+ });
196
+
197
+ // Keyboard navigation
198
+ const onKeyDown: JSX.EventHandler<HTMLElement, KeyboardEvent> = (e) => {
199
+ if (getProps().isDisabled) return;
200
+
201
+ const collection = state.collection();
202
+ const p = getProps();
203
+ const wrap = p.shouldFocusWrap ?? false;
204
+
205
+ // Use state.isDisabled which properly checks the disabledKeys accessor
206
+ const isDisabled = (key: Key) => state.isDisabled(key);
207
+
208
+ switch (e.key) {
209
+ case 'ArrowDown': {
210
+ e.preventDefault();
211
+ const currentKey = state.focusedKey();
212
+ const nextKey = findNextNonDisabledKey(collection, currentKey, 'next', isDisabled, wrap);
213
+ if (nextKey != null) {
214
+ state.setFocusedKey(nextKey);
215
+ }
216
+ break;
217
+ }
218
+ case 'ArrowUp': {
219
+ e.preventDefault();
220
+ const currentKey = state.focusedKey();
221
+ const prevKey = findNextNonDisabledKey(collection, currentKey, 'prev', isDisabled, wrap);
222
+ if (prevKey != null) {
223
+ state.setFocusedKey(prevKey);
224
+ }
225
+ break;
226
+ }
227
+ case 'Home': {
228
+ e.preventDefault();
229
+ // Find first non-disabled key
230
+ let firstKey = collection.getFirstKey();
231
+ while (firstKey != null && isDisabled(firstKey)) {
232
+ firstKey = collection.getKeyAfter(firstKey);
233
+ }
234
+ if (firstKey != null) {
235
+ state.setFocusedKey(firstKey);
236
+ }
237
+ break;
238
+ }
239
+ case 'End': {
240
+ e.preventDefault();
241
+ // Find last non-disabled key
242
+ let lastKey = collection.getLastKey();
243
+ while (lastKey != null && isDisabled(lastKey)) {
244
+ lastKey = collection.getKeyBefore(lastKey);
245
+ }
246
+ if (lastKey != null) {
247
+ state.setFocusedKey(lastKey);
248
+ }
249
+ break;
250
+ }
251
+ case ' ':
252
+ case 'Enter': {
253
+ e.preventDefault();
254
+ const focusedKey = state.focusedKey();
255
+ // Don't activate disabled items
256
+ if (focusedKey != null && !isDisabled(focusedKey)) {
257
+ p.onAction?.(focusedKey);
258
+ p.onClose?.();
259
+ }
260
+ break;
261
+ }
262
+ case 'Escape': {
263
+ e.preventDefault();
264
+ p.onClose?.();
265
+ break;
266
+ }
267
+ case 'PageDown': {
268
+ e.preventDefault();
269
+ const currentKey = state.focusedKey();
270
+ const el = ref?.();
271
+
272
+ if (el) {
273
+ // Use DOM measurements to calculate how many items fit in a page
274
+ const visibleHeight = el.clientHeight;
275
+ let traveled = 0;
276
+ let targetKey = currentKey;
277
+
278
+ while (targetKey != null && traveled < visibleHeight) {
279
+ const nextKey = collection.getKeyAfter(targetKey);
280
+ if (nextKey == null) break;
281
+
282
+ // Try to measure the item height
283
+ const itemElement = el.querySelector(`[data-key="${targetKey}"]`);
284
+ traveled += itemElement?.clientHeight ?? 32;
285
+
286
+ // Skip disabled items
287
+ if (!isDisabled(nextKey)) {
288
+ targetKey = nextKey;
289
+ } else {
290
+ // Skip over disabled items without counting them
291
+ const afterDisabled = findNextNonDisabledKey(collection, nextKey, 'next', isDisabled, false);
292
+ if (afterDisabled != null) {
293
+ targetKey = afterDisabled;
294
+ } else {
295
+ break;
296
+ }
297
+ }
298
+ }
299
+
300
+ if (targetKey != null && targetKey !== currentKey) {
301
+ state.setFocusedKey(targetKey);
302
+ }
303
+ } else {
304
+ // Fallback: move by DEFAULT_PAGE_SIZE items
305
+ let count = DEFAULT_PAGE_SIZE;
306
+ let targetKey = currentKey;
307
+
308
+ while (count > 0 && targetKey != null) {
309
+ const nextKey = findNextNonDisabledKey(collection, targetKey, 'next', isDisabled, false);
310
+ if (nextKey == null) break;
311
+ targetKey = nextKey;
312
+ count--;
313
+ }
314
+
315
+ if (targetKey != null) {
316
+ state.setFocusedKey(targetKey);
317
+ }
318
+ }
319
+ break;
320
+ }
321
+ case 'PageUp': {
322
+ e.preventDefault();
323
+ const currentKey = state.focusedKey();
324
+ const el = ref?.();
325
+
326
+ if (el) {
327
+ // Use DOM measurements to calculate how many items fit in a page
328
+ const visibleHeight = el.clientHeight;
329
+ let traveled = 0;
330
+ let targetKey = currentKey;
331
+
332
+ while (targetKey != null && traveled < visibleHeight) {
333
+ const prevKey = collection.getKeyBefore(targetKey);
334
+ if (prevKey == null) break;
335
+
336
+ // Try to measure the item height
337
+ const itemElement = el.querySelector(`[data-key="${targetKey}"]`);
338
+ traveled += itemElement?.clientHeight ?? 32;
339
+
340
+ // Skip disabled items
341
+ if (!isDisabled(prevKey)) {
342
+ targetKey = prevKey;
343
+ } else {
344
+ // Skip over disabled items without counting them
345
+ const beforeDisabled = findNextNonDisabledKey(collection, prevKey, 'prev', isDisabled, false);
346
+ if (beforeDisabled != null) {
347
+ targetKey = beforeDisabled;
348
+ } else {
349
+ break;
350
+ }
351
+ }
352
+ }
353
+
354
+ if (targetKey != null && targetKey !== currentKey) {
355
+ state.setFocusedKey(targetKey);
356
+ }
357
+ } else {
358
+ // Fallback: move by DEFAULT_PAGE_SIZE items
359
+ let count = DEFAULT_PAGE_SIZE;
360
+ let targetKey = currentKey;
361
+
362
+ while (count > 0 && targetKey != null) {
363
+ const prevKey = findNextNonDisabledKey(collection, targetKey, 'prev', isDisabled, false);
364
+ if (prevKey == null) break;
365
+ targetKey = prevKey;
366
+ count--;
367
+ }
368
+
369
+ if (targetKey != null) {
370
+ state.setFocusedKey(targetKey);
371
+ }
372
+ }
373
+ break;
374
+ }
375
+ }
376
+ };
377
+
378
+ return {
379
+ get labelProps() {
380
+ return labelProps as JSX.HTMLAttributes<HTMLElement>;
381
+ },
382
+ get menuProps() {
383
+ const p = getProps();
384
+
385
+ const baseProps = mergeProps(
386
+ domProps(),
387
+ focusWithinProps as Record<string, unknown>,
388
+ fieldProps as Record<string, unknown>,
389
+ {
390
+ role: 'menu',
391
+ tabIndex: p.isDisabled ? undefined : 0,
392
+ 'aria-disabled': p.isDisabled || undefined,
393
+ onKeyDown,
394
+ } as Record<string, unknown>
395
+ );
396
+
397
+ // Add type-select props if enabled
398
+ if (!p.disallowTypeAhead) {
399
+ return mergeProps(baseProps, typeSelectProps as Record<string, unknown>) as JSX.HTMLAttributes<HTMLElement>;
400
+ }
401
+
402
+ return baseProps as JSX.HTMLAttributes<HTMLElement>;
403
+ },
404
+ };
405
+ }