forgeframe 0.0.1

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,263 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Consumer component implementation module.
4
+ *
5
+ * @remarks
6
+ * This module contains the ConsumerComponent class which handles the consumer-side
7
+ * (the app embedding the component) rendering and communication with host components
8
+ * embedded in iframes or popups.
9
+ */
10
+ import type { ComponentOptions, ForgeFrameComponentInstance, Dimensions } from '../types';
11
+ import type { ContextType } from '../constants';
12
+ import { EventEmitter } from '../events/emitter';
13
+ /**
14
+ * Consumer-side component implementation.
15
+ *
16
+ * @remarks
17
+ * This class manages the lifecycle of a component from the consumer (embedding app) perspective.
18
+ * It handles rendering the component into iframes or popups, communicating with
19
+ * the host window via postMessage, and managing component state.
20
+ *
21
+ * @typeParam P - The props type passed to the component
22
+ * @typeParam X - The exports type that the host can expose to the consumer
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * const instance = new ConsumerComponent(options, { email: 'user@example.com' });
27
+ * await instance.render('#container');
28
+ * ```
29
+ *
30
+ * @public
31
+ */
32
+ export declare class ConsumerComponent<P extends Record<string, unknown>, X = unknown> implements ForgeFrameComponentInstance<P, X> {
33
+ /** Event emitter for lifecycle events. */
34
+ event: EventEmitter;
35
+ /** Arbitrary state storage for the component instance. */
36
+ state: Record<string, unknown>;
37
+ /** Data exported by the host component. */
38
+ exports?: X;
39
+ /** Data exported from the consumer by the host. */
40
+ consumerExports?: unknown;
41
+ /** @internal */
42
+ private _uid;
43
+ /**
44
+ * Unique instance identifier.
45
+ * @readonly
46
+ */
47
+ get uid(): string;
48
+ /** @internal */
49
+ private options;
50
+ /** @internal */
51
+ private props;
52
+ /** @internal */
53
+ private context;
54
+ /** @internal */
55
+ private messenger;
56
+ /** @internal */
57
+ private bridge;
58
+ /** @internal */
59
+ private cleanup;
60
+ /** @internal */
61
+ private hostWindow;
62
+ /** @internal */
63
+ private iframe;
64
+ /** @internal */
65
+ private container;
66
+ /** @internal */
67
+ private prerenderElement;
68
+ /** @internal */
69
+ private initPromise;
70
+ /** @internal */
71
+ private rendered;
72
+ /** @internal */
73
+ private destroyed;
74
+ /**
75
+ * Creates a new ConsumerComponent instance.
76
+ *
77
+ * @param options - Component configuration options
78
+ * @param props - Initial props to pass to the component
79
+ */
80
+ constructor(options: ComponentOptions<P>, props?: Partial<P>);
81
+ /**
82
+ * Builds the list of trusted domains for messenger communication.
83
+ * @internal
84
+ */
85
+ private buildTrustedDomains;
86
+ /**
87
+ * Renders the component into a DOM container.
88
+ *
89
+ * @remarks
90
+ * This is the primary method for displaying the component. It creates
91
+ * an iframe or popup, establishes communication with the host, and
92
+ * handles the prerender/render lifecycle.
93
+ *
94
+ * @param container - CSS selector or HTMLElement to render into
95
+ * @param context - Override the default rendering context (iframe or popup)
96
+ * @throws Error if component was already destroyed or rendered
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * await instance.render('#container');
101
+ * await instance.render(document.getElementById('target'), 'popup');
102
+ * ```
103
+ */
104
+ render(container?: string | HTMLElement, context?: ContextType): Promise<void>;
105
+ /**
106
+ * Renders the component into a container in a different window.
107
+ *
108
+ * @remarks
109
+ * Currently delegates to regular render. Full cross-window rendering
110
+ * would require additional complexity.
111
+ *
112
+ * @param _win - Target window (currently unused)
113
+ * @param container - CSS selector or HTMLElement to render into
114
+ * @param context - Override the default rendering context
115
+ */
116
+ renderTo(_win: Window, container?: string | HTMLElement, context?: ContextType): Promise<void>;
117
+ /**
118
+ * Closes and destroys the component.
119
+ *
120
+ * @remarks
121
+ * Emits the 'close' event before destruction. Safe to call multiple times.
122
+ */
123
+ close(): Promise<void>;
124
+ /**
125
+ * Focuses the component window.
126
+ *
127
+ * @remarks
128
+ * For iframes, focuses the iframe element. For popups, brings the window to front.
129
+ */
130
+ focus(): Promise<void>;
131
+ /**
132
+ * Resizes the component to the specified dimensions.
133
+ *
134
+ * @param dimensions - New width and height for the component
135
+ */
136
+ resize(dimensions: Dimensions): Promise<void>;
137
+ /**
138
+ * Shows the component if hidden.
139
+ *
140
+ * @remarks
141
+ * Only applicable to iframe context.
142
+ */
143
+ show(): Promise<void>;
144
+ /**
145
+ * Hides the component.
146
+ *
147
+ * @remarks
148
+ * Only applicable to iframe context.
149
+ */
150
+ hide(): Promise<void>;
151
+ /**
152
+ * Updates the component props and sends them to the host.
153
+ *
154
+ * @remarks
155
+ * Props are normalized and serialized before being sent to the host window.
156
+ *
157
+ * @param newProps - Partial props object to merge with existing props
158
+ */
159
+ updateProps(newProps: Partial<P>): Promise<void>;
160
+ /**
161
+ * Creates a clone of this instance with the same props.
162
+ *
163
+ * @returns A new unrendered component instance with identical configuration
164
+ */
165
+ clone(): ForgeFrameComponentInstance<P, X>;
166
+ /**
167
+ * Checks if the component is eligible to render based on the eligible option.
168
+ *
169
+ * @returns True if eligible or no eligibility check defined
170
+ */
171
+ isEligible(): boolean;
172
+ /**
173
+ * Normalizes component options with default values.
174
+ * @internal
175
+ */
176
+ private normalizeOptions;
177
+ /**
178
+ * Creates the prop context passed to prop callbacks and validators.
179
+ * @internal
180
+ */
181
+ private createPropContext;
182
+ /**
183
+ * Resolves a container selector or element to an HTMLElement.
184
+ * @internal
185
+ */
186
+ private resolveContainer;
187
+ /**
188
+ * Checks eligibility and throws if component cannot render.
189
+ * @internal
190
+ */
191
+ private checkEligibility;
192
+ /**
193
+ * Creates and displays the prerender (loading) content.
194
+ * @internal
195
+ */
196
+ private prerender;
197
+ /**
198
+ * Creates an iframe element without setting src (for prerender phase).
199
+ * The window name is set immediately as it carries the payload for the host.
200
+ * @internal
201
+ */
202
+ private createIframeElement;
203
+ /**
204
+ * Opens the host window (iframe or popup).
205
+ * @internal
206
+ */
207
+ private open;
208
+ /**
209
+ * Builds the URL for the host window including query parameters.
210
+ * @internal
211
+ */
212
+ private buildUrl;
213
+ /**
214
+ * Builds the window.name payload for the host window.
215
+ * @internal
216
+ */
217
+ private buildWindowName;
218
+ /**
219
+ * Builds component references for nested host components.
220
+ * @internal
221
+ */
222
+ private buildNestedHostRefs;
223
+ /**
224
+ * Creates the exports object sent to the host.
225
+ * @internal
226
+ */
227
+ private createConsumerExports;
228
+ /**
229
+ * Extracts the origin domain from the component URL.
230
+ * @internal
231
+ */
232
+ private getHostDomain;
233
+ /**
234
+ * Waits for the host to send the init message.
235
+ * @internal
236
+ */
237
+ private waitForHost;
238
+ /**
239
+ * Sets up message handlers for host communication.
240
+ * @internal
241
+ */
242
+ private setupMessageHandlers;
243
+ /**
244
+ * Registers cleanup handlers for the instance.
245
+ * @internal
246
+ */
247
+ private setupCleanup;
248
+ /**
249
+ * Handles errors by emitting events and calling callbacks.
250
+ * @internal
251
+ */
252
+ private handleError;
253
+ /**
254
+ * Calls a prop callback if it exists.
255
+ * @internal
256
+ */
257
+ private callPropCallback;
258
+ /**
259
+ * Destroys the component and cleans up all resources.
260
+ * @internal
261
+ */
262
+ private destroy;
263
+ }
@@ -0,0 +1,249 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Host component implementation module.
4
+ *
5
+ * @remarks
6
+ * This module contains the HostComponent class which runs inside the iframe
7
+ * or popup and handles communication with the consumer window. It also provides
8
+ * utilities for detecting host context and accessing hostProps.
9
+ */
10
+ import type { HostProps, WindowNamePayload, PropsDefinition } from '../types';
11
+ import { EventEmitter } from '../events/emitter';
12
+ /**
13
+ * Host-side component implementation.
14
+ *
15
+ * @remarks
16
+ * This class runs inside the iframe or popup window and manages communication
17
+ * with the consumer component. It provides the hostProps object with props and
18
+ * control methods.
19
+ *
20
+ * @typeParam P - The props type passed from the consumer
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * // In host window
25
+ * const { email, onLogin, close } = window.hostProps;
26
+ * console.log('Email:', email);
27
+ * onLogin({ id: 1, name: 'John' });
28
+ * close();
29
+ * ```
30
+ *
31
+ * @public
32
+ */
33
+ export declare class HostComponent<P extends Record<string, unknown>> {
34
+ private propDefinitions;
35
+ /** The hostProps object containing props and control methods passed from the consumer. */
36
+ hostProps: HostProps<P>;
37
+ /** Event emitter for lifecycle events. */
38
+ event: EventEmitter;
39
+ /** @internal */
40
+ private uid;
41
+ /** @internal */
42
+ private tag;
43
+ /** @internal */
44
+ private consumerWindow;
45
+ /** @internal */
46
+ private consumerDomain;
47
+ /** @internal */
48
+ private messenger;
49
+ /** @internal */
50
+ private bridge;
51
+ /** @internal */
52
+ private propsHandlers;
53
+ /** @internal */
54
+ private consumerProps;
55
+ /** @internal */
56
+ private initError;
57
+ /**
58
+ * Creates a new HostComponent instance.
59
+ *
60
+ * @param payload - The payload parsed from window.name
61
+ * @param propDefinitions - Optional prop definitions for deserialization
62
+ */
63
+ constructor(payload: WindowNamePayload<P>, propDefinitions?: PropsDefinition<P>);
64
+ /**
65
+ * Returns the hostProps object.
66
+ *
67
+ * @returns The hostProps object with props and control methods
68
+ */
69
+ getProps(): HostProps<P>;
70
+ /**
71
+ * Resolves the consumer window reference (iframe parent or popup opener).
72
+ * @internal
73
+ */
74
+ private resolveConsumerWindow;
75
+ /**
76
+ * Builds the hostProps object with deserialized props and control methods.
77
+ * @internal
78
+ */
79
+ private buildHostProps;
80
+ /**
81
+ * Sends initialization message to the consumer.
82
+ * @internal
83
+ */
84
+ private sendInit;
85
+ /**
86
+ * Returns the initialization error if one occurred.
87
+ *
88
+ * @returns The initialization error or null if successful
89
+ */
90
+ getInitError(): Error | null;
91
+ /**
92
+ * Requests the consumer to close this component.
93
+ * @internal
94
+ */
95
+ private close;
96
+ /**
97
+ * Focuses this window and notifies the consumer.
98
+ * @internal
99
+ */
100
+ private focus;
101
+ /**
102
+ * Requests the consumer to resize this component.
103
+ * @internal
104
+ */
105
+ private resize;
106
+ /**
107
+ * Requests the consumer to show this component.
108
+ * @internal
109
+ */
110
+ private show;
111
+ /**
112
+ * Requests the consumer to hide this component.
113
+ * @internal
114
+ */
115
+ private hide;
116
+ /**
117
+ * Subscribes to prop updates from the consumer.
118
+ * @internal
119
+ */
120
+ private onProps;
121
+ /**
122
+ * Reports an error to the consumer.
123
+ * @internal
124
+ */
125
+ private onError;
126
+ /**
127
+ * Exports data or methods to the consumer.
128
+ * @internal
129
+ */
130
+ private exportData;
131
+ /**
132
+ * Exports data to the consumer for bidirectional communication.
133
+ * @internal
134
+ */
135
+ private consumerExport;
136
+ /**
137
+ * Gets information about peer component instances.
138
+ * @internal
139
+ */
140
+ private getPeerInstances;
141
+ /**
142
+ * Builds nested component factories from refs passed by the consumer.
143
+ * @internal
144
+ */
145
+ private buildNestedComponents;
146
+ /**
147
+ * Sets up message handlers for consumer communication.
148
+ * @internal
149
+ */
150
+ private setupMessageHandlers;
151
+ /**
152
+ * Destroys the host component and cleans up resources.
153
+ */
154
+ destroy(): void;
155
+ }
156
+ /**
157
+ * Initializes the host component if running in a ForgeFrame window.
158
+ *
159
+ * @remarks
160
+ * This function detects if the current window was created by ForgeFrame
161
+ * and sets up the host component with hostProps. Returns null if not in
162
+ * a ForgeFrame host context.
163
+ *
164
+ * @typeParam P - The props type passed from the consumer
165
+ * @param propDefinitions - Optional prop definitions for deserialization
166
+ * @returns The host component instance or null if not in a host window
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * const host = initHost();
171
+ * if (host) {
172
+ * console.log('Running in ForgeFrame host context');
173
+ * console.log('Props:', host.hostProps);
174
+ * }
175
+ * ```
176
+ *
177
+ * @public
178
+ */
179
+ export declare function initHost<P extends Record<string, unknown>>(propDefinitions?: PropsDefinition<P>): HostComponent<P> | null;
180
+ /**
181
+ * Gets the current host component instance.
182
+ *
183
+ * @typeParam P - The props type passed from the consumer
184
+ * @returns The host component instance or null if not initialized
185
+ *
186
+ * @public
187
+ */
188
+ export declare function getHost<P extends Record<string, unknown>>(): HostComponent<P> | null;
189
+ /**
190
+ * Checks if the current window is a ForgeFrame host context.
191
+ *
192
+ * @remarks
193
+ * A "host" in ForgeFrame terminology is the embedded iframe or popup window
194
+ * that receives props from the consumer (the embedding app).
195
+ *
196
+ * @returns True if running inside a ForgeFrame iframe or popup
197
+ *
198
+ * @example
199
+ * ```typescript
200
+ * if (isHost()) {
201
+ * console.log('Running in ForgeFrame host');
202
+ * }
203
+ * ```
204
+ *
205
+ * @public
206
+ */
207
+ export declare function isHost(): boolean;
208
+ /**
209
+ * Checks if the current window is embedded by ForgeFrame.
210
+ *
211
+ * @remarks
212
+ * This is an alias for {@link isHost} that uses more intuitive terminology.
213
+ * "Embedded" means this window is running inside a ForgeFrame iframe or popup,
214
+ * receiving props from the consumer (the embedding app).
215
+ *
216
+ * @returns True if running inside a ForgeFrame iframe or popup
217
+ *
218
+ * @example
219
+ * ```typescript
220
+ * if (isEmbedded()) {
221
+ * const { amount, onSuccess } = window.hostProps;
222
+ * // Handle embedded context...
223
+ * }
224
+ * ```
225
+ *
226
+ * @public
227
+ */
228
+ export declare function isEmbedded(): boolean;
229
+ /**
230
+ * Gets the hostProps object from the window.
231
+ *
232
+ * @remarks
233
+ * This is a convenience function to access `window.hostProps`, which contains
234
+ * all props passed from the consumer plus built-in control methods.
235
+ *
236
+ * @typeParam P - The props type passed from the consumer
237
+ * @returns The hostProps object or undefined if not in a host context
238
+ *
239
+ * @example
240
+ * ```typescript
241
+ * const props = getHostProps();
242
+ * if (props) {
243
+ * props.onLogin({ id: 1, name: 'John' });
244
+ * }
245
+ * ```
246
+ *
247
+ * @public
248
+ */
249
+ export declare function getHostProps<P extends Record<string, unknown>>(): HostProps<P> | undefined;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Core module for ForgeFrame component creation and lifecycle management.
4
+ *
5
+ * @remarks
6
+ * This module provides the primary API for creating, managing, and destroying
7
+ * cross-domain components. It includes both consumer-side (embedding app) and
8
+ * host-side (embedded page) functionality.
9
+ */
10
+ export { create, getComponent, destroy, destroyByTag, destroyAll, unregisterComponent, clearComponents, } from './component';
11
+ export { ConsumerComponent } from './consumer';
12
+ export { HostComponent, initHost, getHost, isHost, isEmbedded, getHostProps, } from './host';
@@ -0,0 +1,18 @@
1
+ /**
2
+ * ForgeFrame Framework Integration Module
3
+ *
4
+ * @remarks
5
+ * This module provides framework-specific integrations for ForgeFrame
6
+ * components with popular UI frameworks like React. These handle the lifecycle
7
+ * management, prop synchronization, and rendering of cross-domain components
8
+ * within the target framework's component model.
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * import { createReactComponent, withReactComponent } from 'forgeframe/drivers';
13
+ * import type { ReactDriverOptions, ReactComponentProps } from 'forgeframe/drivers';
14
+ * ```
15
+ *
16
+ * @packageDocumentation
17
+ */
18
+ export { createReactComponent, withReactComponent, type ReactDriverOptions, type ReactComponentProps, type ReactComponentType, } from './react';