@riktajs/react 0.11.5 → 0.11.6

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/dist/index.d.cts CHANGED
@@ -1,6 +1,3 @@
1
- import * as react from 'react';
2
- import { FC } from 'react';
3
-
4
1
  /**
5
2
  * SSR data structure passed from server to client
6
3
  * via window.__SSR_DATA__
@@ -19,36 +16,6 @@ interface SsrData<T = unknown> {
19
16
  /** Additional metadata */
20
17
  meta?: Record<string, unknown>;
21
18
  }
22
- /**
23
- * Router context value interface
24
- */
25
- interface RouterContextValue {
26
- /** Current URL path */
27
- pathname: string;
28
- /** Current search params string (without ?) */
29
- search: string;
30
- /** Full URL */
31
- href: string;
32
- /** Navigate to a new URL */
33
- navigate: (url: string, options?: NavigateOptions) => void | Promise<void>;
34
- /** Extracted route params (e.g., { id: '123' } for /item/:id) */
35
- params: Record<string, string>;
36
- /** Update route params (used internally by RiktaProvider) */
37
- setParams: (params: Record<string, string>) => void;
38
- /** Whether a navigation is in progress */
39
- isNavigating?: boolean;
40
- }
41
- /**
42
- * Navigation options
43
- */
44
- interface NavigateOptions {
45
- /** Replace current history entry instead of pushing */
46
- replace?: boolean;
47
- /** Scroll to top after navigation */
48
- scroll?: boolean;
49
- /** Additional state to store in history */
50
- state?: unknown;
51
- }
52
19
  /**
53
20
  * Result type for server actions
54
21
  */
@@ -88,34 +55,6 @@ interface ActionState<TInput = unknown, TResult = unknown> {
88
55
  /** Reset action state */
89
56
  reset: () => void;
90
57
  }
91
- /**
92
- * Link component props
93
- */
94
- interface LinkProps extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
95
- /** Target URL */
96
- href: string;
97
- /** Replace history entry instead of push */
98
- replace?: boolean;
99
- /** Scroll to top after navigation */
100
- scroll?: boolean;
101
- /** Prefetch the linked page (future enhancement) */
102
- prefetch?: boolean;
103
- /** Additional state to pass to navigation */
104
- state?: unknown;
105
- /** Children elements */
106
- children: React.ReactNode;
107
- }
108
- /**
109
- * RiktaProvider props
110
- */
111
- interface RiktaProviderProps {
112
- /** Initial SSR data from server */
113
- ssrData?: SsrData;
114
- /** Initial route params extracted from URL */
115
- initialParams?: Record<string, string>;
116
- /** Children elements */
117
- children: React.ReactNode;
118
- }
119
58
  /**
120
59
  * Hydration state
121
60
  */
@@ -132,125 +71,147 @@ declare global {
132
71
  }
133
72
 
134
73
  /**
135
- * React context for router state
136
- * Provides navigation utilities and current location info
137
- */
138
- declare const RouterContext: react.Context<RouterContextValue>;
139
-
140
- /**
141
- * React context for SSR data
142
- * Holds the server-rendered data passed via window.__SSR_DATA__
143
- */
144
- declare const SsrContext: react.Context<SsrData<unknown> | null>;
145
-
146
- /**
147
- * RiktaProvider - Main provider component for Rikta React utilities
74
+ * Get SSR data from window.__SSR_DATA__
148
75
  *
149
- * Provides routing context, SSR data, and navigation utilities to the app.
150
- * Automatically fetches new page data during client-side navigation.
76
+ * This is a pure utility function (not a React hook) that reads
77
+ * the SSR data passed from the server. It can be called anywhere
78
+ * in your application without needing a Provider wrapper.
151
79
  *
152
- * @example
153
- * ```tsx
154
- * // In entry-client.tsx
155
- * import { RiktaProvider } from '@riktajs/react';
156
- *
157
- * hydrateRoot(
158
- * document.getElementById('root')!,
159
- * <RiktaProvider>
160
- * <App />
161
- * </RiktaProvider>
162
- * );
163
- * ```
164
- */
165
- declare const RiktaProvider: FC<RiktaProviderProps>;
166
-
167
- /**
168
- * Link component for client-side navigation
80
+ * The data is cached after first read for performance.
169
81
  *
170
- * Renders an anchor tag that uses the History API for navigation
171
- * instead of causing a full page reload.
82
+ * @returns SSR data object or null if not available
172
83
  *
173
84
  * @example
174
85
  * ```tsx
175
- * import { Link } from '@riktajs/react';
86
+ * import { getSsrData } from '@riktajs/react';
87
+ *
88
+ * interface PageData {
89
+ * title: string;
90
+ * items: Array<{ id: string; name: string }>;
91
+ * }
92
+ *
93
+ * function ItemList() {
94
+ * const ssrData = getSsrData<PageData>();
95
+ *
96
+ * if (!ssrData) {
97
+ * return <div>Loading...</div>;
98
+ * }
176
99
  *
177
- * function Nav() {
178
100
  * return (
179
- * <nav>
180
- * <Link href="/">Home</Link>
181
- * <Link href="/about">About</Link>
182
- * <Link href="/items/123">Item 123</Link>
183
- * </nav>
101
+ * <div>
102
+ * <h1>{ssrData.data.title}</h1>
103
+ * <ul>
104
+ * {ssrData.data.items.map(item => (
105
+ * <li key={item.id}>{item.name}</li>
106
+ * ))}
107
+ * </ul>
108
+ * </div>
184
109
  * );
185
110
  * }
186
111
  * ```
187
112
  *
188
113
  * @example
189
114
  * ```tsx
190
- * // With options
191
- * <Link href="/dashboard" replace scroll={false}>
192
- * Dashboard
193
- * </Link>
115
+ * // Access directly without needing Provider
116
+ * const ssrData = getSsrData<{ user: User }>();
117
+ * const user = ssrData?.data.user;
194
118
  * ```
195
119
  */
196
- declare const Link: FC<LinkProps>;
120
+ declare function getSsrData<T = unknown>(): SsrData<T> | null;
121
+ /**
122
+ * Clear the SSR data cache
123
+ * Useful for testing or when SSR data changes dynamically
124
+ */
125
+ declare function clearSsrDataCache(): void;
126
+ /**
127
+ * Set SSR data manually (for testing or server-side rendering)
128
+ *
129
+ * @param data - SSR data to set, or null to clear
130
+ */
131
+ declare function setSsrData(data: SsrData | null): void;
197
132
 
198
133
  /**
199
- * Hook for programmatic navigation
134
+ * Options for navigation
135
+ */
136
+ interface NavigateOptions {
137
+ /** Replace current history entry instead of pushing (uses location.replace) */
138
+ replace?: boolean;
139
+ }
140
+ /**
141
+ * Navigate function type
200
142
  *
201
- * @returns Object with navigate function and current location info
143
+ * @param path - The path to navigate to
144
+ * @param paramsOrOptions - Either query params object or navigate options
145
+ * @param options - Navigate options (when params are provided)
146
+ */
147
+ type NavigateFn = {
148
+ (path: string, options?: NavigateOptions): void;
149
+ (path: string, params: Record<string, string | number | boolean | undefined | null>, options?: NavigateOptions): void;
150
+ };
151
+ /**
152
+ * Hook for programmatic navigation using native browser APIs
153
+ *
154
+ * This hook provides a simple way to navigate between pages using
155
+ * standard browser navigation (full page loads for SSR pages).
156
+ *
157
+ * @returns Navigate function
202
158
  *
203
159
  * @example
204
160
  * ```tsx
205
- * import { useNavigation } from '@riktajs/react';
161
+ * import { useNavigate } from '@riktajs/react';
206
162
  *
207
163
  * function MyComponent() {
208
- * const { navigate, pathname } = useNavigation();
164
+ * const navigate = useNavigate();
209
165
  *
210
- * const handleSubmit = async () => {
211
- * await saveData();
212
- * navigate('/success');
166
+ * const handleClick = () => {
167
+ * // Simple navigation
168
+ * navigate('/dashboard');
213
169
  * };
214
170
  *
215
- * return (
216
- * <button onClick={handleSubmit}>
217
- * Submit (current path: {pathname})
218
- * </button>
219
- * );
171
+ * const handleSearch = (query: string) => {
172
+ * // Navigation with query params
173
+ * navigate('/search', { q: query, page: 1 });
174
+ * // Results in: /search?q=query&page=1
175
+ * };
176
+ *
177
+ * const handleLogin = () => {
178
+ * // Replace current history entry (for redirects)
179
+ * navigate('/login', { replace: true });
180
+ * };
181
+ *
182
+ * const handleFilter = (filter: string) => {
183
+ * // Params + options
184
+ * navigate('/items', { filter, sort: 'name' }, { replace: true });
185
+ * };
186
+ *
187
+ * return <button onClick={handleClick}>Go to Dashboard</button>;
220
188
  * }
221
189
  * ```
222
190
  *
223
191
  * @example
224
192
  * ```tsx
225
- * // With options
226
- * const { navigate } = useNavigation();
227
- *
228
- * // Replace history entry (for redirects)
229
- * navigate('/login', { replace: true });
193
+ * // After form submission
194
+ * function CreateItemForm() {
195
+ * const navigate = useNavigate();
230
196
  *
231
- * // Don't scroll to top
232
- * navigate('/next', { scroll: false });
197
+ * const handleSubmit = async (data: FormData) => {
198
+ * const result = await createItem(data);
199
+ * if (result.success) {
200
+ * navigate(`/items/${result.id}`);
201
+ * }
202
+ * };
233
203
  *
234
- * // Pass state
235
- * navigate('/edit', { state: { from: 'list' } });
204
+ * return <form onSubmit={handleSubmit}>...</form>;
205
+ * }
236
206
  * ```
237
207
  */
238
- declare function useNavigation(): {
239
- /** Navigate to a new URL */
240
- navigate: (url: string, options?: NavigateOptions) => void;
241
- /** Current pathname */
242
- pathname: string;
243
- /** Current search string (without ?) */
244
- search: string;
245
- /** Full href */
246
- href: string;
247
- };
208
+ declare function useNavigate(): NavigateFn;
248
209
 
249
210
  /**
250
- * Hook to access route parameters
211
+ * Hook to access route parameters from SSR data
251
212
  *
252
213
  * Route parameters are extracted from the URL path by the server
253
- * and passed via SSR data. They're stored in the RouterContext.
214
+ * and passed via SSR data. This hook reads them from the SSR data.
254
215
  *
255
216
  * @returns Object with route parameter values
256
217
  *
@@ -279,7 +240,11 @@ declare function useNavigation(): {
279
240
  declare function useParams<T extends Record<string, string> = Record<string, string>>(): T;
280
241
 
281
242
  /**
282
- * Hook to access and manipulate URL search parameters
243
+ * Hook to access and update URL search parameters using native browser APIs
244
+ *
245
+ * This hook provides a simple interface for reading and modifying URL query
246
+ * parameters. When updating params, it triggers a full page navigation to
247
+ * ensure SSR data is refreshed.
283
248
  *
284
249
  * @returns Tuple of [URLSearchParams, setSearchParams function]
285
250
  *
@@ -311,8 +276,30 @@ declare function useParams<T extends Record<string, string> = Record<string, str
311
276
  * );
312
277
  * }
313
278
  * ```
279
+ *
280
+ * @example
281
+ * ```tsx
282
+ * // Merge with existing params
283
+ * function FilterComponent() {
284
+ * const [searchParams, setSearchParams] = useSearchParams();
285
+ *
286
+ * const updateFilter = (filter: string) => {
287
+ * // Get current params and update
288
+ * const params = Object.fromEntries(searchParams.entries());
289
+ * setSearchParams({ ...params, filter });
290
+ * };
291
+ *
292
+ * return (
293
+ * <select onChange={(e) => updateFilter(e.target.value)}>
294
+ * <option value="">All</option>
295
+ * <option value="active">Active</option>
296
+ * <option value="inactive">Inactive</option>
297
+ * </select>
298
+ * );
299
+ * }
300
+ * ```
314
301
  */
315
- declare function useSearchParams(): [URLSearchParams, (params: Record<string, string> | URLSearchParams) => void];
302
+ declare function useSearchParams(): [URLSearchParams, (params: Record<string, string | number | boolean | undefined | null> | URLSearchParams) => void];
316
303
 
317
304
  /**
318
305
  * Location object returned by useLocation
@@ -328,7 +315,10 @@ interface Location {
328
315
  searchParams: URLSearchParams;
329
316
  }
330
317
  /**
331
- * Hook to access current location information
318
+ * Hook to access current location information using native browser APIs
319
+ *
320
+ * This hook reads directly from `window.location` and provides a convenient
321
+ * interface for accessing URL information.
332
322
  *
333
323
  * @returns Location object with pathname, search, href, and searchParams
334
324
  *
@@ -358,6 +348,21 @@ interface Location {
358
348
  * return filter ? <span>Filtered by: {filter}</span> : null;
359
349
  * }
360
350
  * ```
351
+ *
352
+ * @example
353
+ * ```tsx
354
+ * // Use pathname for conditional rendering
355
+ * function Navigation() {
356
+ * const { pathname } = useLocation();
357
+ *
358
+ * return (
359
+ * <nav>
360
+ * <a href="/" className={pathname === '/' ? 'active' : ''}>Home</a>
361
+ * <a href="/about" className={pathname === '/about' ? 'active' : ''}>About</a>
362
+ * </nav>
363
+ * );
364
+ * }
365
+ * ```
361
366
  */
362
367
  declare function useLocation(): Location;
363
368
 
@@ -367,6 +372,9 @@ declare function useLocation(): Location;
367
372
  * SSR data is passed via window.__SSR_DATA__ and contains
368
373
  * the initial data rendered on the server.
369
374
  *
375
+ * Note: This is a simple wrapper around getSsrData() for
376
+ * familiarity. You can also use getSsrData() directly.
377
+ *
370
378
  * @returns SSR data object or null if not available
371
379
  *
372
380
  * @example
@@ -613,4 +621,4 @@ interface UseActionOptions<TResult = unknown> {
613
621
  */
614
622
  declare function useAction<TInput = unknown, TResult = unknown>(url: string, options?: UseActionOptions<TResult>): ActionState<TInput, TResult>;
615
623
 
616
- export { type ActionResult, type ActionState, type FetchState, type HydrationState, Link, type LinkProps, type Location, type NavigateOptions, RiktaProvider, type RiktaProviderProps, RouterContext, type RouterContextValue, SsrContext, type SsrData, type UseActionOptions, type UseFetchOptions, useAction, useFetch, useHydration, useLocation, useNavigation, useParams, useSearchParams, useSsrData };
624
+ export { type ActionResult, type ActionState, type FetchState, type HydrationState, type Location, type NavigateFn, type NavigateOptions, type SsrData, type UseActionOptions, type UseFetchOptions, clearSsrDataCache, getSsrData, setSsrData, useAction, useFetch, useHydration, useLocation, useNavigate, useParams, useSearchParams, useSsrData };