@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.
- package/LICENSE +21 -0
- package/dist/actiongroup/createActionGroup.d.ts +29 -0
- package/dist/actiongroup/createActionGroup.d.ts.map +1 -0
- package/dist/actiongroup/index.d.ts +2 -0
- package/dist/actiongroup/index.d.ts.map +1 -0
- package/dist/autocomplete/createAutocomplete.d.ts +6 -2
- package/dist/autocomplete/createAutocomplete.d.ts.map +1 -1
- package/dist/breadcrumbs/createBreadcrumbs.d.ts +2 -0
- package/dist/breadcrumbs/createBreadcrumbs.d.ts.map +1 -1
- package/dist/button/createToggleButtonGroup.d.ts +32 -0
- package/dist/button/createToggleButtonGroup.d.ts.map +1 -0
- package/dist/button/index.d.ts +2 -0
- package/dist/button/index.d.ts.map +1 -1
- package/dist/calendar/createCalendarCell.d.ts +2 -0
- package/dist/calendar/createCalendarCell.d.ts.map +1 -1
- package/dist/calendar/createCalendarGrid.d.ts.map +1 -1
- package/dist/calendar/createRangeCalendarCell.d.ts +3 -1
- package/dist/calendar/createRangeCalendarCell.d.ts.map +1 -1
- package/dist/checkbox/createCheckboxGroup.d.ts +5 -1
- package/dist/checkbox/createCheckboxGroup.d.ts.map +1 -1
- package/dist/collections/index.d.ts +56 -0
- package/dist/collections/index.d.ts.map +1 -0
- package/dist/color/createColorArea.d.ts.map +1 -1
- package/dist/color/createColorSlider.d.ts.map +1 -1
- package/dist/color/createColorWheel.d.ts.map +1 -1
- package/dist/combobox/createComboBox.d.ts +6 -0
- package/dist/combobox/createComboBox.d.ts.map +1 -1
- package/dist/datepicker/createDatePicker.d.ts +6 -0
- package/dist/datepicker/createDatePicker.d.ts.map +1 -1
- package/dist/datepicker/createDateRangePicker.d.ts +40 -0
- package/dist/datepicker/createDateRangePicker.d.ts.map +1 -0
- package/dist/datepicker/createDateSegment.d.ts +1 -1
- package/dist/datepicker/createDateSegment.d.ts.map +1 -1
- package/dist/datepicker/createTimeSegment.d.ts +29 -0
- package/dist/datepicker/createTimeSegment.d.ts.map +1 -0
- package/dist/datepicker/index.d.ts +2 -0
- package/dist/datepicker/index.d.ts.map +1 -1
- package/dist/disclosure/createDisclosureGroup.d.ts +2 -1
- package/dist/disclosure/createDisclosureGroup.d.ts.map +1 -1
- package/dist/dnd/createDrag.d.ts.map +1 -1
- package/dist/dnd/createDraggableCollection.d.ts +4 -0
- package/dist/dnd/createDraggableCollection.d.ts.map +1 -1
- package/dist/dnd/createDraggableItem.d.ts.map +1 -1
- package/dist/dnd/createDrop.d.ts.map +1 -1
- package/dist/dnd/createDroppableCollection.d.ts +32 -1
- package/dist/dnd/createDroppableCollection.d.ts.map +1 -1
- package/dist/dnd/createDroppableItem.d.ts.map +1 -1
- package/dist/dnd/index.d.ts +1 -1
- package/dist/dnd/index.d.ts.map +1 -1
- package/dist/grid/createGrid.d.ts.map +1 -1
- package/dist/gridlist/createGridList.d.ts.map +1 -1
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4659 -3452
- package/dist/index.js.map +1 -7
- package/dist/index.ssr.js +4659 -3452
- package/dist/index.ssr.js.map +1 -7
- package/dist/interactions/createFocus.d.ts.map +1 -1
- package/dist/interactions/createFocusWithin.d.ts.map +1 -1
- package/dist/link/createLink.d.ts +10 -0
- package/dist/link/createLink.d.ts.map +1 -1
- package/dist/listbox/createListBox.d.ts +1 -0
- package/dist/listbox/createListBox.d.ts.map +1 -1
- package/dist/listbox/createOption.d.ts.map +1 -1
- package/dist/menu/createMenu.d.ts +1 -0
- package/dist/menu/createMenu.d.ts.map +1 -1
- package/dist/meter/createMeter.d.ts.map +1 -1
- package/dist/numberfield/createNumberField.d.ts +18 -0
- package/dist/numberfield/createNumberField.d.ts.map +1 -1
- package/dist/overlays/createModal.d.ts +16 -0
- package/dist/overlays/createModal.d.ts.map +1 -1
- package/dist/overlays/createOverlay.d.ts.map +1 -1
- package/dist/overlays/index.d.ts +1 -1
- package/dist/overlays/index.d.ts.map +1 -1
- package/dist/popover/createOverlayPosition.d.ts.map +1 -1
- package/dist/popover/createPopover.d.ts.map +1 -1
- package/dist/progress/createProgressBar.d.ts.map +1 -1
- package/dist/radio/createRadioGroup.d.ts +2 -2
- package/dist/radio/createRadioGroup.d.ts.map +1 -1
- package/dist/searchfield/createSearchField.d.ts.map +1 -1
- package/dist/select/createHiddenSelect.d.ts.map +1 -1
- package/dist/select/createSelect.d.ts.map +1 -1
- package/dist/slider/createSlider.d.ts.map +1 -1
- package/dist/table/createTable.d.ts.map +1 -1
- package/dist/tabs/createTabs.d.ts +1 -1
- package/dist/tabs/createTabs.d.ts.map +1 -1
- package/dist/tag/createTag.d.ts.map +1 -1
- package/dist/tag/createTagGroup.d.ts.map +1 -1
- package/dist/toast/createToast.d.ts +4 -0
- package/dist/toast/createToast.d.ts.map +1 -1
- package/dist/toast/createToastRegion.d.ts.map +1 -1
- package/dist/toolbar/createToolbar.d.ts.map +1 -1
- package/dist/tooltip/createTooltipTrigger.d.ts.map +1 -1
- package/dist/tree/createTree.d.ts.map +1 -1
- package/dist/tree/createTreeItem.d.ts.map +1 -1
- package/dist/tree/types.d.ts +4 -0
- package/dist/tree/types.d.ts.map +1 -1
- package/dist/utils/env.d.ts +1 -1
- package/dist/utils/env.d.ts.map +1 -1
- package/dist/utils/platform.d.ts.map +1 -1
- package/dist/visually-hidden/createVisuallyHidden.d.ts.map +1 -1
- package/package.json +8 -6
- package/src/actiongroup/createActionGroup.ts +324 -0
- package/src/actiongroup/index.ts +8 -0
- package/src/autocomplete/createAutocomplete.ts +32 -9
- package/src/breadcrumbs/createBreadcrumbs.ts +10 -15
- package/src/button/createButton.ts +1 -1
- package/src/button/createToggleButtonGroup.ts +128 -0
- package/src/button/index.ts +9 -0
- package/src/calendar/createCalendarCell.ts +6 -4
- package/src/calendar/createCalendarGrid.ts +27 -18
- package/src/calendar/createRangeCalendarCell.ts +26 -9
- package/src/checkbox/createCheckboxGroup.ts +21 -4
- package/src/collections/index.ts +242 -0
- package/src/color/createColorArea.ts +380 -314
- package/src/color/createColorField.ts +137 -137
- package/src/color/createColorSlider.ts +286 -197
- package/src/color/createColorSwatch.ts +40 -40
- package/src/color/createColorWheel.ts +218 -208
- package/src/color/index.ts +24 -24
- package/src/color/types.ts +116 -116
- package/src/combobox/createComboBox.ts +670 -647
- package/src/combobox/index.ts +6 -6
- package/src/datepicker/createDatePicker.ts +54 -16
- package/src/datepicker/createDateRangePicker.ts +246 -0
- package/src/datepicker/createDateSegment.ts +185 -31
- package/src/datepicker/createTimeSegment.ts +370 -0
- package/src/datepicker/index.ts +14 -0
- package/src/dialog/createDialog.ts +120 -120
- package/src/dialog/index.ts +2 -2
- package/src/dialog/types.ts +19 -19
- package/src/disclosure/createDisclosureGroup.ts +5 -2
- package/src/dnd/createDrag.ts +224 -209
- package/src/dnd/createDraggableCollection.ts +96 -63
- package/src/dnd/createDraggableItem.ts +259 -243
- package/src/dnd/createDrop.ts +322 -321
- package/src/dnd/createDroppableCollection.ts +682 -293
- package/src/dnd/createDroppableItem.ts +215 -213
- package/src/dnd/index.ts +55 -47
- package/src/dnd/types.ts +89 -89
- package/src/dnd/utils.ts +294 -294
- package/src/focus/createAutoFocus.ts +321 -321
- package/src/focus/createFocusRestore.ts +313 -313
- package/src/focus/createVirtualFocus.ts +396 -396
- package/src/form/createFormValidation.ts +224 -224
- package/src/form/index.ts +11 -11
- package/src/grid/createGrid.ts +3 -1
- package/src/gridlist/createGridList.ts +16 -0
- package/src/gridlist/createGridListItem.ts +1 -1
- package/src/i18n/NumberFormatter.ts +266 -266
- package/src/i18n/createCollator.ts +79 -79
- package/src/i18n/createDateFormatter.ts +83 -83
- package/src/i18n/createFilter.ts +131 -131
- package/src/i18n/createNumberFormatter.ts +52 -52
- package/src/i18n/index.ts +40 -40
- package/src/i18n/locale.tsx +188 -188
- package/src/i18n/utils.ts +99 -99
- package/src/index.ts +51 -0
- package/src/interactions/createFocus.ts +6 -5
- package/src/interactions/createFocusWithin.ts +6 -5
- package/src/interactions/createLongPress.ts +174 -174
- package/src/interactions/createMove.ts +289 -289
- package/src/interactions/createPress.ts +5 -5
- package/src/landmark/createLandmark.ts +377 -377
- package/src/landmark/index.ts +8 -8
- package/src/link/createLink.ts +23 -8
- package/src/listbox/createListBox.ts +308 -269
- package/src/listbox/createOption.ts +162 -151
- package/src/listbox/index.ts +12 -12
- package/src/live-announcer/announce.ts +322 -322
- package/src/live-announcer/index.ts +9 -9
- package/src/menu/createMenu.ts +405 -396
- package/src/menu/createMenuItem.ts +149 -149
- package/src/menu/createMenuTrigger.ts +88 -88
- package/src/menu/index.ts +18 -18
- package/src/meter/createMeter.ts +1 -6
- package/src/numberfield/createNumberField.ts +311 -268
- package/src/numberfield/index.ts +5 -5
- package/src/overlays/ariaHideOutside.ts +219 -219
- package/src/overlays/createInteractOutside.ts +149 -149
- package/src/overlays/createModal.tsx +238 -202
- package/src/overlays/createOverlay.ts +165 -155
- package/src/overlays/createOverlayTrigger.ts +85 -85
- package/src/overlays/createPreventScroll.ts +266 -266
- package/src/overlays/index.ts +48 -44
- package/src/popover/calculatePosition.ts +6 -6
- package/src/popover/createOverlayPosition.ts +7 -4
- package/src/popover/createPopover.ts +21 -7
- package/src/progress/createProgressBar.ts +6 -1
- package/src/radio/createRadioGroup.ts +88 -14
- package/src/searchfield/createSearchField.ts +241 -186
- package/src/searchfield/index.ts +2 -2
- package/src/select/createHiddenSelect.tsx +263 -236
- package/src/select/createSelect.ts +373 -395
- package/src/select/index.ts +14 -14
- package/src/slider/createSlider.ts +364 -349
- package/src/slider/index.ts +2 -2
- package/src/ssr/index.tsx +370 -370
- package/src/table/createTable.ts +3 -1
- package/src/table/createTableColumnHeader.ts +1 -1
- package/src/table/createTableRow.ts +1 -1
- package/src/tabs/createTabs.ts +80 -51
- package/src/tag/createTag.ts +135 -6
- package/src/tag/createTagGroup.ts +7 -2
- package/src/toast/createToast.ts +8 -2
- package/src/toast/createToastRegion.ts +0 -1
- package/src/toolbar/createToolbar.ts +75 -1
- package/src/tooltip/createTooltip.ts +79 -79
- package/src/tooltip/createTooltipTrigger.ts +226 -222
- package/src/tooltip/index.ts +6 -6
- package/src/tree/createTree.ts +261 -246
- package/src/tree/createTreeItem.ts +282 -233
- package/src/tree/createTreeSelectionCheckbox.ts +68 -68
- package/src/tree/index.ts +16 -16
- package/src/tree/types.ts +91 -87
- package/src/utils/env.ts +55 -54
- package/src/utils/platform.ts +16 -6
- package/src/visually-hidden/createVisuallyHidden.ts +139 -124
- 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
|
+
}
|