reactolith 1.0.19

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.
@@ -0,0 +1,236 @@
1
+ import { Root } from 'react-dom/client';
2
+ import React, { ElementType, PropsWithChildren, ReactNode } from 'react';
3
+ import { Href } from '@react-types/shared';
4
+ import * as react_jsx_runtime from 'react/jsx-runtime';
5
+
6
+ type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
7
+ type Handler$1<Args extends readonly unknown[]> = (...args: Args) => void;
8
+ type RouterEventMap = {
9
+ "nav:started": [input: URL | string, init: RequestInit, pushState: boolean];
10
+ "nav:ended": [
11
+ input: URL | string,
12
+ init: RequestInit,
13
+ pushState: boolean,
14
+ response: Response,
15
+ html: string,
16
+ finalUrl: string
17
+ ];
18
+ "render:success": [
19
+ input: URL | string,
20
+ init: RequestInit,
21
+ pushState: boolean,
22
+ response: Response,
23
+ html: string,
24
+ finalUrl: string
25
+ ];
26
+ "render:failed": [
27
+ input: URL | string,
28
+ init: RequestInit,
29
+ pushState: boolean,
30
+ response: Response,
31
+ html: string,
32
+ finalUrl: string
33
+ ];
34
+ };
35
+ declare class Router {
36
+ private readonly app;
37
+ private readonly fetch;
38
+ private listeners;
39
+ constructor(app: App, doc?: Document, fetchImpl?: FetchLike);
40
+ private ensureSet;
41
+ protected emit<K extends keyof RouterEventMap>(type: K, ...args: RouterEventMap[K]): void;
42
+ on<K extends keyof RouterEventMap>(type: K, handler: Handler$1<RouterEventMap[K]>): () => void;
43
+ off<K extends keyof RouterEventMap>(type: K, handler: Handler$1<RouterEventMap[K]>): void;
44
+ visit(input: URL | string, init?: RequestInit, pushState?: boolean): Promise<{
45
+ result: boolean;
46
+ response: Response;
47
+ html: string;
48
+ finalUrl: string;
49
+ }>;
50
+ onClick(event: MouseEvent): Promise<void>;
51
+ onSubmit(event: SubmitEvent): Promise<void>;
52
+ navigate(path: Href): Promise<void>;
53
+ }
54
+
55
+ type MercureConfig = {
56
+ hubUrl: string;
57
+ withCredentials?: boolean;
58
+ };
59
+ declare class App {
60
+ readonly element: HTMLElement;
61
+ readonly router: Router;
62
+ readonly component: ElementType;
63
+ mercureConfig?: MercureConfig;
64
+ private readonly appProvider;
65
+ private readonly selector;
66
+ private readonly root;
67
+ private readonly doc;
68
+ constructor(component: ElementType, appProvider?: ElementType<PropsWithChildren<{
69
+ app: App;
70
+ }>>, selector?: ((doc: Document) => HTMLElement | null) | string, root?: Root, doc?: Document, fetchImp?: FetchLike);
71
+ render(document: string | Document): boolean;
72
+ renderElement(element: HTMLElement): void;
73
+ unmount(): void;
74
+ }
75
+
76
+ type Handler<Args extends readonly unknown[]> = (...args: Args) => void;
77
+ type MercureEventMap = {
78
+ "sse:connected": [url: string];
79
+ "sse:disconnected": [url: string];
80
+ "sse:message": [event: MessageEvent, html: string];
81
+ "render:success": [event: MessageEvent, html: string];
82
+ "render:failed": [event: MessageEvent, html: string];
83
+ "refetch:started": [event: MessageEvent];
84
+ "refetch:success": [event: MessageEvent, html: string];
85
+ "refetch:failed": [event: MessageEvent, error: Error];
86
+ "sse:error": [error: Event];
87
+ };
88
+ type MercureOptions = {
89
+ /** The Mercure hub URL to connect to */
90
+ hubUrl: string;
91
+ /** Optional: Last-Event-ID for reconnection */
92
+ lastEventId?: string;
93
+ /** Optional: Whether to include credentials (cookies) */
94
+ withCredentials?: boolean;
95
+ };
96
+ declare class Mercure {
97
+ private readonly app;
98
+ private eventSource;
99
+ private listeners;
100
+ private currentUrl;
101
+ private options;
102
+ private routerUnsubscribe;
103
+ constructor(app: App);
104
+ private ensureSet;
105
+ protected emit<K extends keyof MercureEventMap>(type: K, ...args: MercureEventMap[K]): void;
106
+ on<K extends keyof MercureEventMap>(type: K, handler: Handler<MercureEventMap[K]>): () => void;
107
+ off<K extends keyof MercureEventMap>(type: K, handler: Handler<MercureEventMap[K]>): void;
108
+ /**
109
+ * Subscribe to a Mercure hub for real-time updates.
110
+ * Automatically subscribes to the current pathname and re-subscribes on route changes.
111
+ */
112
+ subscribe(options: MercureOptions): void;
113
+ /**
114
+ * Connect to EventSource with current pathname as topic
115
+ */
116
+ private connectToCurrentPath;
117
+ /**
118
+ * Close the SSE connection
119
+ */
120
+ close(): void;
121
+ /**
122
+ * Check if currently connected
123
+ */
124
+ get connected(): boolean;
125
+ /**
126
+ * Get the current connection URL
127
+ */
128
+ get url(): string | null;
129
+ /**
130
+ * Get the last event ID (useful for reconnection)
131
+ */
132
+ get lastEventId(): string | undefined;
133
+ }
134
+
135
+ declare const ReactolithComponent: React.ForwardRefExoticComponent<React.HTMLAttributes<HTMLElement> & {
136
+ element?: HTMLElement;
137
+ component: React.ElementType;
138
+ } & React.RefAttributes<HTMLElement>>;
139
+
140
+ declare function useApp(): App;
141
+ declare const AppProvider: React.FC<PropsWithChildren<{
142
+ app: App;
143
+ }>>;
144
+
145
+ type RenderFailedPayload = {
146
+ /** whatever your router emits, kept as unknown to avoid tight coupling */
147
+ input: unknown;
148
+ init: unknown;
149
+ pushState: unknown;
150
+ response: unknown;
151
+ html: unknown;
152
+ finalUrl: unknown;
153
+ };
154
+ type RenderError = RenderFailedPayload & {
155
+ /** increments for every error so consumers can re-open dialogs */
156
+ id: number;
157
+ /** when the error was captured */
158
+ timestamp: number;
159
+ };
160
+ type RouterContextType = {
161
+ router: Router;
162
+ loading: boolean;
163
+ /** last render error (if any) */
164
+ lastError: RenderError | null;
165
+ /** clear currently shown error (e.g., when dialog is closed) */
166
+ clearError: () => void;
167
+ };
168
+ declare function RouterProvider({ children }: {
169
+ children: React.ReactNode;
170
+ }): react_jsx_runtime.JSX.Element;
171
+ declare function useRouter(): RouterContextType;
172
+
173
+ /**
174
+ * Subscribe to a Mercure topic and receive live JSON data updates.
175
+ *
176
+ * @template T - The type of data expected from the topic
177
+ * @param topic - The Mercure topic to subscribe to
178
+ * @param initialValue - The initial value before any updates
179
+ * @returns The current value from the topic
180
+ *
181
+ * @example
182
+ * ```tsx
183
+ * // Simple usage with type inference
184
+ * const count = useMercureTopic('/notifications/count', 0);
185
+ *
186
+ * // With explicit type
187
+ * const status = useMercureTopic<'online' | 'offline'>('/status', 'offline');
188
+ *
189
+ * // With complex type
190
+ * interface Stats { visitors: number; sales: number; }
191
+ * const stats = useMercureTopic<Stats>('/stats', { visitors: 0, sales: 0 });
192
+ * ```
193
+ */
194
+ declare function useMercureTopic<T>(topic: string, initialValue: T): T;
195
+
196
+ /**
197
+ * Generic hook for subscribing to a Mercure topic and receiving raw message data.
198
+ * This is a low-level hook that handles EventSource connection management.
199
+ *
200
+ * @param topic - The Mercure topic to subscribe to
201
+ * @param onMessage - Callback when a message is received
202
+ * @param onError - Optional callback when an error occurs
203
+ *
204
+ * @internal This is a low-level hook. Use useMercureTopic or MercureLive instead.
205
+ */
206
+ declare function useMercureEventSource(topic: string, onMessage: (data: string) => void, onError?: (error: Event) => void): void;
207
+
208
+ /**
209
+ * Component that subscribes to a Mercure topic and renders HTML updates.
210
+ *
211
+ * Use this for partial page updates that should be reflected across all pages,
212
+ * like sidebars, notification areas, or live feeds.
213
+ *
214
+ * @example
215
+ * ```tsx
216
+ * <MercureLive topic="/sidebar">
217
+ * <sidebar-component>Initial content</sidebar-component>
218
+ * </MercureLive>
219
+ * ```
220
+ *
221
+ * Backend can push updates to this region:
222
+ * ```php
223
+ * $update = new Update('/sidebar', $sidebarHtml);
224
+ * $hub->publish($update);
225
+ * ```
226
+ */
227
+ interface MercureLiveProps {
228
+ /** The Mercure topic to subscribe to */
229
+ topic: string;
230
+ /** Initial content to display before any updates */
231
+ children: ReactNode;
232
+ }
233
+ declare function MercureLive({ topic, children }: MercureLiveProps): react_jsx_runtime.JSX.Element;
234
+
235
+ export { App, AppProvider, Mercure, MercureLive, ReactolithComponent, Router, RouterProvider, useApp, useMercureEventSource, useMercureTopic, useRouter };
236
+ export type { MercureConfig, MercureLiveProps, MercureOptions };