@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
package/src/ssr/index.tsx CHANGED
@@ -1,370 +1,370 @@
1
- /**
2
- * SSR utilities for Solidaria
3
- *
4
- * SolidJS has built-in SSR support with `isServer` and `createUniqueId()`.
5
- * These utilities provide a consistent API matching React-Aria's patterns
6
- * with additional features for hydration management.
7
- */
8
-
9
- import {
10
- type Accessor,
11
- type JSX,
12
- type ParentProps,
13
- createContext,
14
- createEffect,
15
- createMemo,
16
- createSignal,
17
- onCleanup,
18
- onMount,
19
- useContext,
20
- createUniqueId,
21
- } from 'solid-js';
22
- import { isServer } from 'solid-js/web';
23
-
24
- // ============================================
25
- // TYPES
26
- // ============================================
27
-
28
- export interface SSRProviderProps extends ParentProps {}
29
-
30
- export interface SSRContextValue {
31
- /** Whether currently rendering on the server. */
32
- isSSR: boolean;
33
- /** Prefix for generated IDs, allowing nested providers. */
34
- prefix: string;
35
- }
36
-
37
- // ============================================
38
- // CONTEXT
39
- // ============================================
40
-
41
- const SSRContext = createContext<SSRContextValue>({
42
- isSSR: isServer,
43
- prefix: '',
44
- });
45
-
46
- // ============================================
47
- // BASIC UTILITIES
48
- // ============================================
49
-
50
- /**
51
- * Returns whether the component is currently being server side rendered.
52
- * Can be used to delay browser-specific rendering until after hydration.
53
- *
54
- * Note: This returns a static boolean. For reactive hydration detection,
55
- * use `createHydrationState()`.
56
- */
57
- export function createIsSSR(): boolean {
58
- return isServer;
59
- }
60
-
61
- /**
62
- * Check if we can use DOM APIs.
63
- * This is useful for code that needs to run only in the browser.
64
- */
65
- export const canUseDOM = !isServer;
66
-
67
- /**
68
- * Generate a unique ID that is stable across server and client.
69
- * Uses SolidJS's built-in createUniqueId which handles SSR correctly.
70
- *
71
- * @param defaultId - Optional default ID to use instead of generating one.
72
- *
73
- * @example
74
- * ```tsx
75
- * function TextField(props) {
76
- * const inputId = createId(props.id);
77
- * return (
78
- * <>
79
- * <label for={inputId}>{props.label}</label>
80
- * <input id={inputId} />
81
- * </>
82
- * );
83
- * }
84
- * ```
85
- */
86
- export function createId(defaultId?: string): string {
87
- if (defaultId) {
88
- return defaultId;
89
- }
90
- const ctx = useContext(SSRContext);
91
- const uniqueId = createUniqueId();
92
- return ctx.prefix ? `solidaria-${ctx.prefix}-${uniqueId}` : `solidaria-${uniqueId}`;
93
- }
94
-
95
- // ============================================
96
- // SSR PROVIDER
97
- // ============================================
98
-
99
- /**
100
- * Provides SSR context to the component tree.
101
- *
102
- * While SolidJS handles most SSR scenarios automatically, this provider
103
- * can be useful for:
104
- * - Nested ID prefixes to avoid collisions in micro-frontends
105
- * - Explicit hydration boundary markers
106
- * - Testing SSR behavior
107
- *
108
- * @example
109
- * ```tsx
110
- * // Root of your app
111
- * <SSRProvider>
112
- * <App />
113
- * </SSRProvider>
114
- *
115
- * // With custom prefix for micro-frontend
116
- * <SSRProvider prefix="widget">
117
- * <Widget />
118
- * </SSRProvider>
119
- * ```
120
- */
121
- export function SSRProvider(props: SSRProviderProps & { prefix?: string }): JSX.Element {
122
- const parentContext = useContext(SSRContext);
123
-
124
- const value = createMemo<SSRContextValue>(() => ({
125
- isSSR: isServer,
126
- prefix: props.prefix
127
- ? parentContext.prefix
128
- ? `${parentContext.prefix}-${props.prefix}`
129
- : props.prefix
130
- : parentContext.prefix,
131
- }));
132
-
133
- return (
134
- <SSRContext.Provider value={value()}>
135
- {props.children}
136
- </SSRContext.Provider>
137
- );
138
- }
139
-
140
- // ============================================
141
- // HYDRATION STATE
142
- // ============================================
143
-
144
- /**
145
- * Tracks whether the component is currently hydrating.
146
- *
147
- * During server-side rendering, this returns `true`. After hydration
148
- * completes on the client, it switches to `false`. This is useful for
149
- * components that need to show different content during hydration.
150
- *
151
- * @example
152
- * ```tsx
153
- * function ClientOnlyComponent() {
154
- * const isHydrating = createHydrationState();
155
- *
156
- * return (
157
- * <Show when={!isHydrating()} fallback={<LoadingPlaceholder />}>
158
- * <InteractiveWidget />
159
- * </Show>
160
- * );
161
- * }
162
- * ```
163
- */
164
- export function createHydrationState(): Accessor<boolean> {
165
- // On the server, always return true
166
- if (isServer) {
167
- return () => true;
168
- }
169
-
170
- // On the client, track hydration state
171
- const [isHydrating, setIsHydrating] = createSignal(true);
172
-
173
- onMount(() => {
174
- setIsHydrating(false);
175
- });
176
-
177
- return isHydrating;
178
- }
179
-
180
- /**
181
- * Hook that returns `true` during SSR and initial hydration.
182
- * Use this to delay browser-specific code until hydration is complete.
183
- *
184
- * Unlike `createIsSSR()` which is static, this updates reactively
185
- * after hydration completes.
186
- *
187
- * @example
188
- * ```tsx
189
- * function BrowserOnlyFeature() {
190
- * const isSSR = useIsSSR();
191
- *
192
- * createEffect(() => {
193
- * if (!isSSR()) {
194
- * // Safe to access browser APIs here
195
- * window.localStorage.getItem('key');
196
- * }
197
- * });
198
- *
199
- * return <Show when={!isSSR()}>...</Show>;
200
- * }
201
- * ```
202
- */
203
- export function useIsSSR(): Accessor<boolean> {
204
- return createHydrationState();
205
- }
206
-
207
- // ============================================
208
- // SAFE BROWSER EFFECTS
209
- // ============================================
210
-
211
- /**
212
- * Creates an effect that only runs on the client after hydration.
213
- * This is a convenience wrapper that ensures browser-specific code
214
- * doesn't run during SSR.
215
- *
216
- * @param fn - The effect function to run
217
- *
218
- * @example
219
- * ```tsx
220
- * function Analytics() {
221
- * createBrowserEffect(() => {
222
- * // Safe to access window, document, localStorage, etc.
223
- * window.analytics.track('page_view');
224
- * });
225
- *
226
- * return null;
227
- * }
228
- * ```
229
- */
230
- export function createBrowserEffect(fn: () => void | (() => void)): void {
231
- if (isServer) {
232
- return;
233
- }
234
-
235
- createEffect(() => {
236
- const cleanup = fn();
237
- if (typeof cleanup === 'function') {
238
- onCleanup(cleanup);
239
- }
240
- });
241
- }
242
-
243
- /**
244
- * Creates a value that is computed only on the client.
245
- * On the server, returns the fallback value.
246
- *
247
- * @param fn - Function to compute the value on the client
248
- * @param fallback - Value to return during SSR
249
- *
250
- * @example
251
- * ```tsx
252
- * function WindowSize() {
253
- * const width = createBrowserValue(
254
- * () => window.innerWidth,
255
- * 0
256
- * );
257
- *
258
- * return <span>Width: {width()}</span>;
259
- * }
260
- * ```
261
- */
262
- export function createBrowserValue<T>(
263
- fn: () => T,
264
- fallback: T
265
- ): Accessor<T> {
266
- if (isServer) {
267
- return () => fallback;
268
- }
269
-
270
- const [value, setValue] = createSignal<T>(fallback);
271
-
272
- onMount(() => {
273
- setValue(() => fn());
274
- });
275
-
276
- return value;
277
- }
278
-
279
- // ============================================
280
- // WINDOW/DOCUMENT SAFE ACCESS
281
- // ============================================
282
-
283
- /**
284
- * Returns the window object if available, or undefined during SSR.
285
- * Useful for accessing browser globals safely.
286
- *
287
- * @example
288
- * ```tsx
289
- * const win = getWindow();
290
- * if (win) {
291
- * win.addEventListener('resize', handler);
292
- * }
293
- * ```
294
- */
295
- export function getWindow(): Window | undefined {
296
- if (typeof window !== 'undefined') {
297
- return window;
298
- }
299
- return undefined;
300
- }
301
-
302
- /**
303
- * Returns the document object if available, or undefined during SSR.
304
- * Useful for accessing document safely.
305
- *
306
- * @example
307
- * ```tsx
308
- * const doc = getDocument();
309
- * if (doc) {
310
- * doc.addEventListener('keydown', handler);
311
- * }
312
- * ```
313
- */
314
- export function getDocument(): Document | undefined {
315
- if (typeof document !== 'undefined') {
316
- return document;
317
- }
318
- return undefined;
319
- }
320
-
321
- /**
322
- * Returns the owner document of an element, with SSR safety.
323
- *
324
- * @param el - The element to get the owner document from
325
- */
326
- export function getOwnerDocument(el: Element | null | undefined): Document | undefined {
327
- return el?.ownerDocument ?? getDocument();
328
- }
329
-
330
- /**
331
- * Returns the owner window of an element, with SSR safety.
332
- *
333
- * @param el - The element to get the owner window from
334
- */
335
- export function getOwnerWindow(el: Element | null | undefined): Window | undefined {
336
- return getOwnerDocument(el)?.defaultView ?? getWindow();
337
- }
338
-
339
- // ============================================
340
- // PORTAL HELPERS
341
- // ============================================
342
-
343
- /**
344
- * Gets the appropriate container for portals, with SSR safety.
345
- * Returns the specified container, or document.body on the client,
346
- * or undefined during SSR.
347
- *
348
- * @param container - Optional custom container element
349
- *
350
- * @example
351
- * ```tsx
352
- * function Modal(props) {
353
- * const container = getPortalContainer(props.container);
354
- *
355
- * return (
356
- * <Show when={container}>
357
- * <Portal mount={container}>
358
- * {props.children}
359
- * </Portal>
360
- * </Show>
361
- * );
362
- * }
363
- * ```
364
- */
365
- export function getPortalContainer(container?: Element): Element | undefined {
366
- if (container) {
367
- return container;
368
- }
369
- return getDocument()?.body;
370
- }
1
+ /**
2
+ * SSR utilities for Solidaria
3
+ *
4
+ * SolidJS has built-in SSR support with `isServer` and `createUniqueId()`.
5
+ * These utilities provide a consistent API matching React-Aria's patterns
6
+ * with additional features for hydration management.
7
+ */
8
+
9
+ import {
10
+ type Accessor,
11
+ type JSX,
12
+ type ParentProps,
13
+ createContext,
14
+ createEffect,
15
+ createMemo,
16
+ createSignal,
17
+ onCleanup,
18
+ onMount,
19
+ useContext,
20
+ createUniqueId,
21
+ } from 'solid-js';
22
+ import { isServer } from 'solid-js/web';
23
+
24
+ // ============================================
25
+ // TYPES
26
+ // ============================================
27
+
28
+ export interface SSRProviderProps extends ParentProps {}
29
+
30
+ export interface SSRContextValue {
31
+ /** Whether currently rendering on the server. */
32
+ isSSR: boolean;
33
+ /** Prefix for generated IDs, allowing nested providers. */
34
+ prefix: string;
35
+ }
36
+
37
+ // ============================================
38
+ // CONTEXT
39
+ // ============================================
40
+
41
+ const SSRContext = createContext<SSRContextValue>({
42
+ isSSR: isServer,
43
+ prefix: '',
44
+ });
45
+
46
+ // ============================================
47
+ // BASIC UTILITIES
48
+ // ============================================
49
+
50
+ /**
51
+ * Returns whether the component is currently being server side rendered.
52
+ * Can be used to delay browser-specific rendering until after hydration.
53
+ *
54
+ * Note: This returns a static boolean. For reactive hydration detection,
55
+ * use `createHydrationState()`.
56
+ */
57
+ export function createIsSSR(): boolean {
58
+ return isServer;
59
+ }
60
+
61
+ /**
62
+ * Check if we can use DOM APIs.
63
+ * This is useful for code that needs to run only in the browser.
64
+ */
65
+ export const canUseDOM = !isServer;
66
+
67
+ /**
68
+ * Generate a unique ID that is stable across server and client.
69
+ * Uses SolidJS's built-in createUniqueId which handles SSR correctly.
70
+ *
71
+ * @param defaultId - Optional default ID to use instead of generating one.
72
+ *
73
+ * @example
74
+ * ```tsx
75
+ * function TextField(props) {
76
+ * const inputId = createId(props.id);
77
+ * return (
78
+ * <>
79
+ * <label for={inputId}>{props.label}</label>
80
+ * <input id={inputId} />
81
+ * </>
82
+ * );
83
+ * }
84
+ * ```
85
+ */
86
+ export function createId(defaultId?: string): string {
87
+ if (defaultId) {
88
+ return defaultId;
89
+ }
90
+ const ctx = useContext(SSRContext);
91
+ const uniqueId = createUniqueId();
92
+ return ctx.prefix ? `solidaria-${ctx.prefix}-${uniqueId}` : `solidaria-${uniqueId}`;
93
+ }
94
+
95
+ // ============================================
96
+ // SSR PROVIDER
97
+ // ============================================
98
+
99
+ /**
100
+ * Provides SSR context to the component tree.
101
+ *
102
+ * While SolidJS handles most SSR scenarios automatically, this provider
103
+ * can be useful for:
104
+ * - Nested ID prefixes to avoid collisions in micro-frontends
105
+ * - Explicit hydration boundary markers
106
+ * - Testing SSR behavior
107
+ *
108
+ * @example
109
+ * ```tsx
110
+ * // Root of your app
111
+ * <SSRProvider>
112
+ * <App />
113
+ * </SSRProvider>
114
+ *
115
+ * // With custom prefix for micro-frontend
116
+ * <SSRProvider prefix="widget">
117
+ * <Widget />
118
+ * </SSRProvider>
119
+ * ```
120
+ */
121
+ export function SSRProvider(props: SSRProviderProps & { prefix?: string }): JSX.Element {
122
+ const parentContext = useContext(SSRContext);
123
+
124
+ const value = createMemo<SSRContextValue>(() => ({
125
+ isSSR: isServer,
126
+ prefix: props.prefix
127
+ ? parentContext.prefix
128
+ ? `${parentContext.prefix}-${props.prefix}`
129
+ : props.prefix
130
+ : parentContext.prefix,
131
+ }));
132
+
133
+ return (
134
+ <SSRContext.Provider value={value()}>
135
+ {props.children}
136
+ </SSRContext.Provider>
137
+ );
138
+ }
139
+
140
+ // ============================================
141
+ // HYDRATION STATE
142
+ // ============================================
143
+
144
+ /**
145
+ * Tracks whether the component is currently hydrating.
146
+ *
147
+ * During server-side rendering, this returns `true`. After hydration
148
+ * completes on the client, it switches to `false`. This is useful for
149
+ * components that need to show different content during hydration.
150
+ *
151
+ * @example
152
+ * ```tsx
153
+ * function ClientOnlyComponent() {
154
+ * const isHydrating = createHydrationState();
155
+ *
156
+ * return (
157
+ * <Show when={!isHydrating()} fallback={<LoadingPlaceholder />}>
158
+ * <InteractiveWidget />
159
+ * </Show>
160
+ * );
161
+ * }
162
+ * ```
163
+ */
164
+ export function createHydrationState(): Accessor<boolean> {
165
+ // On the server, always return true
166
+ if (isServer) {
167
+ return () => true;
168
+ }
169
+
170
+ // On the client, track hydration state
171
+ const [isHydrating, setIsHydrating] = createSignal(true);
172
+
173
+ onMount(() => {
174
+ setIsHydrating(false);
175
+ });
176
+
177
+ return isHydrating;
178
+ }
179
+
180
+ /**
181
+ * Hook that returns `true` during SSR and initial hydration.
182
+ * Use this to delay browser-specific code until hydration is complete.
183
+ *
184
+ * Unlike `createIsSSR()` which is static, this updates reactively
185
+ * after hydration completes.
186
+ *
187
+ * @example
188
+ * ```tsx
189
+ * function BrowserOnlyFeature() {
190
+ * const isSSR = useIsSSR();
191
+ *
192
+ * createEffect(() => {
193
+ * if (!isSSR()) {
194
+ * // Safe to access browser APIs here
195
+ * window.localStorage.getItem('key');
196
+ * }
197
+ * });
198
+ *
199
+ * return <Show when={!isSSR()}>...</Show>;
200
+ * }
201
+ * ```
202
+ */
203
+ export function useIsSSR(): Accessor<boolean> {
204
+ return createHydrationState();
205
+ }
206
+
207
+ // ============================================
208
+ // SAFE BROWSER EFFECTS
209
+ // ============================================
210
+
211
+ /**
212
+ * Creates an effect that only runs on the client after hydration.
213
+ * This is a convenience wrapper that ensures browser-specific code
214
+ * doesn't run during SSR.
215
+ *
216
+ * @param fn - The effect function to run
217
+ *
218
+ * @example
219
+ * ```tsx
220
+ * function Analytics() {
221
+ * createBrowserEffect(() => {
222
+ * // Safe to access window, document, localStorage, etc.
223
+ * window.analytics.track('page_view');
224
+ * });
225
+ *
226
+ * return null;
227
+ * }
228
+ * ```
229
+ */
230
+ export function createBrowserEffect(fn: () => void | (() => void)): void {
231
+ if (isServer) {
232
+ return;
233
+ }
234
+
235
+ createEffect(() => {
236
+ const cleanup = fn();
237
+ if (typeof cleanup === 'function') {
238
+ onCleanup(cleanup);
239
+ }
240
+ });
241
+ }
242
+
243
+ /**
244
+ * Creates a value that is computed only on the client.
245
+ * On the server, returns the fallback value.
246
+ *
247
+ * @param fn - Function to compute the value on the client
248
+ * @param fallback - Value to return during SSR
249
+ *
250
+ * @example
251
+ * ```tsx
252
+ * function WindowSize() {
253
+ * const width = createBrowserValue(
254
+ * () => window.innerWidth,
255
+ * 0
256
+ * );
257
+ *
258
+ * return <span>Width: {width()}</span>;
259
+ * }
260
+ * ```
261
+ */
262
+ export function createBrowserValue<T>(
263
+ fn: () => T,
264
+ fallback: T
265
+ ): Accessor<T> {
266
+ if (isServer) {
267
+ return () => fallback;
268
+ }
269
+
270
+ const [value, setValue] = createSignal<T>(fallback);
271
+
272
+ onMount(() => {
273
+ setValue(() => fn());
274
+ });
275
+
276
+ return value;
277
+ }
278
+
279
+ // ============================================
280
+ // WINDOW/DOCUMENT SAFE ACCESS
281
+ // ============================================
282
+
283
+ /**
284
+ * Returns the window object if available, or undefined during SSR.
285
+ * Useful for accessing browser globals safely.
286
+ *
287
+ * @example
288
+ * ```tsx
289
+ * const win = getWindow();
290
+ * if (win) {
291
+ * win.addEventListener('resize', handler);
292
+ * }
293
+ * ```
294
+ */
295
+ export function getWindow(): Window | undefined {
296
+ if (typeof window !== 'undefined') {
297
+ return window;
298
+ }
299
+ return undefined;
300
+ }
301
+
302
+ /**
303
+ * Returns the document object if available, or undefined during SSR.
304
+ * Useful for accessing document safely.
305
+ *
306
+ * @example
307
+ * ```tsx
308
+ * const doc = getDocument();
309
+ * if (doc) {
310
+ * doc.addEventListener('keydown', handler);
311
+ * }
312
+ * ```
313
+ */
314
+ export function getDocument(): Document | undefined {
315
+ if (typeof document !== 'undefined') {
316
+ return document;
317
+ }
318
+ return undefined;
319
+ }
320
+
321
+ /**
322
+ * Returns the owner document of an element, with SSR safety.
323
+ *
324
+ * @param el - The element to get the owner document from
325
+ */
326
+ export function getOwnerDocument(el: Element | null | undefined): Document | undefined {
327
+ return el?.ownerDocument ?? getDocument();
328
+ }
329
+
330
+ /**
331
+ * Returns the owner window of an element, with SSR safety.
332
+ *
333
+ * @param el - The element to get the owner window from
334
+ */
335
+ export function getOwnerWindow(el: Element | null | undefined): Window | undefined {
336
+ return getOwnerDocument(el)?.defaultView ?? getWindow();
337
+ }
338
+
339
+ // ============================================
340
+ // PORTAL HELPERS
341
+ // ============================================
342
+
343
+ /**
344
+ * Gets the appropriate container for portals, with SSR safety.
345
+ * Returns the specified container, or document.body on the client,
346
+ * or undefined during SSR.
347
+ *
348
+ * @param container - Optional custom container element
349
+ *
350
+ * @example
351
+ * ```tsx
352
+ * function Modal(props) {
353
+ * const container = getPortalContainer(props.container);
354
+ *
355
+ * return (
356
+ * <Show when={container}>
357
+ * <Portal mount={container}>
358
+ * {props.children}
359
+ * </Portal>
360
+ * </Show>
361
+ * );
362
+ * }
363
+ * ```
364
+ */
365
+ export function getPortalContainer(container?: Element): Element | undefined {
366
+ if (container) {
367
+ return container;
368
+ }
369
+ return getDocument()?.body;
370
+ }