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,196 @@
1
+ /**
2
+ * Standard Schema V1 type definitions and validation utilities.
3
+ *
4
+ * @remarks
5
+ * These types enable integration with Standard Schema-compliant validation
6
+ * libraries such as Zod, Valibot, ArkType, and others. Types are copied from
7
+ * the official @standard-schema/spec package to maintain zero dependencies.
8
+ *
9
+ * @see https://standardschema.dev/
10
+ *
11
+ * @packageDocumentation
12
+ */
13
+ /**
14
+ * The Standard Schema V1 interface.
15
+ *
16
+ * @remarks
17
+ * Any schema library implementing this interface can be used with ForgeFrame's
18
+ * prop validation system. The `~standard` property contains the schema metadata
19
+ * and validation function.
20
+ *
21
+ * @typeParam Input - The input type the schema accepts
22
+ * @typeParam Output - The output type after validation/transformation
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // Zod schemas implement StandardSchemaV1
27
+ * import { z } from 'zod';
28
+ * const schema: StandardSchemaV1<string, string> = z.string().email();
29
+ * ```
30
+ *
31
+ * @public
32
+ */
33
+ export interface StandardSchemaV1<Input = unknown, Output = Input> {
34
+ readonly '~standard': StandardSchemaV1Props<Input, Output>;
35
+ }
36
+ /**
37
+ * The properties of the `~standard` key on a Standard Schema.
38
+ *
39
+ * @typeParam Input - The input type the schema accepts
40
+ * @typeParam Output - The output type after validation/transformation
41
+ *
42
+ * @public
43
+ */
44
+ export interface StandardSchemaV1Props<Input = unknown, Output = Input> {
45
+ /** The version of the Standard Schema specification (always 1) */
46
+ readonly version: 1;
47
+ /** The name of the schema library (e.g., "zod", "valibot", "arktype") */
48
+ readonly vendor: string;
49
+ /** Optional type metadata for input and output types */
50
+ readonly types?: StandardSchemaV1Types<Input, Output>;
51
+ /** Validates an unknown value and returns a result */
52
+ readonly validate: (value: unknown) => StandardSchemaV1Result<Output> | Promise<StandardSchemaV1Result<Output>>;
53
+ }
54
+ /**
55
+ * Type metadata for Standard Schema input and output types.
56
+ *
57
+ * @remarks
58
+ * This is optional metadata used for type inference. The actual values
59
+ * are typically `undefined` at runtime - they exist purely for TypeScript.
60
+ *
61
+ * @typeParam Input - The input type
62
+ * @typeParam Output - The output type
63
+ *
64
+ * @public
65
+ */
66
+ export interface StandardSchemaV1Types<Input = unknown, Output = Input> {
67
+ /** The input type (for type inference only) */
68
+ readonly input: Input;
69
+ /** The output type (for type inference only) */
70
+ readonly output: Output;
71
+ }
72
+ /**
73
+ * The result of a Standard Schema validation.
74
+ *
75
+ * @typeParam Output - The output type on success
76
+ *
77
+ * @public
78
+ */
79
+ export type StandardSchemaV1Result<Output> = StandardSchemaV1SuccessResult<Output> | StandardSchemaV1FailureResult;
80
+ /**
81
+ * A successful validation result containing the validated/transformed value.
82
+ *
83
+ * @typeParam Output - The output type
84
+ *
85
+ * @public
86
+ */
87
+ export interface StandardSchemaV1SuccessResult<Output> {
88
+ /** The validated and potentially transformed value */
89
+ readonly value: Output;
90
+ /** Undefined on success */
91
+ readonly issues?: undefined;
92
+ }
93
+ /**
94
+ * A failed validation result containing validation issues.
95
+ *
96
+ * @public
97
+ */
98
+ export interface StandardSchemaV1FailureResult {
99
+ /** Array of validation issues */
100
+ readonly issues: ReadonlyArray<StandardSchemaV1Issue>;
101
+ }
102
+ /**
103
+ * A validation issue from a Standard Schema.
104
+ *
105
+ * @public
106
+ */
107
+ export interface StandardSchemaV1Issue {
108
+ /** Human-readable error message */
109
+ readonly message: string;
110
+ /** Path to the invalid value (for nested objects/arrays) */
111
+ readonly path?: ReadonlyArray<PropertyKey | StandardSchemaV1PathSegment>;
112
+ }
113
+ /**
114
+ * A path segment for nested validation issues.
115
+ *
116
+ * @public
117
+ */
118
+ export interface StandardSchemaV1PathSegment {
119
+ /** The property key or array index */
120
+ readonly key: PropertyKey;
121
+ }
122
+ /**
123
+ * Infers the input type from a Standard Schema.
124
+ *
125
+ * @typeParam Schema - The Standard Schema type
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * import { z } from 'zod';
130
+ * const schema = z.string().email();
131
+ * type Input = InferInput<typeof schema>; // string
132
+ * ```
133
+ *
134
+ * @public
135
+ */
136
+ export type InferInput<Schema extends StandardSchemaV1> = Schema extends StandardSchemaV1<infer I, unknown> ? I : never;
137
+ /**
138
+ * Infers the output type from a Standard Schema.
139
+ *
140
+ * @typeParam Schema - The Standard Schema type
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * import { z } from 'zod';
145
+ * const schema = z.string().transform(s => s.length);
146
+ * type Output = InferOutput<typeof schema>; // number
147
+ * ```
148
+ *
149
+ * @public
150
+ */
151
+ export type InferOutput<Schema extends StandardSchemaV1> = Schema extends StandardSchemaV1<unknown, infer O> ? O : never;
152
+ /**
153
+ * Type guard to check if a value is a Standard Schema.
154
+ *
155
+ * @param value - The value to check
156
+ * @returns True if the value implements StandardSchemaV1
157
+ *
158
+ * @example
159
+ * ```typescript
160
+ * import { z } from 'zod';
161
+ *
162
+ * const schema = z.string();
163
+ * if (isStandardSchema(schema)) {
164
+ * // schema is StandardSchemaV1
165
+ * }
166
+ * ```
167
+ *
168
+ * @public
169
+ */
170
+ export declare function isStandardSchema(value: unknown): value is StandardSchemaV1;
171
+ /**
172
+ * Validates a value using a Standard Schema (synchronous only).
173
+ *
174
+ * @remarks
175
+ * This function only supports synchronous validation. If the schema returns
176
+ * a Promise, an error is thrown with instructions to use a sync schema.
177
+ *
178
+ * @typeParam T - The expected output type
179
+ * @param schema - The Standard Schema to validate with
180
+ * @param value - The value to validate
181
+ * @param propName - The prop name (for error messages)
182
+ * @returns The validated and potentially transformed value
183
+ * @throws Error if validation fails or schema is async
184
+ *
185
+ * @example
186
+ * ```typescript
187
+ * import { z } from 'zod';
188
+ *
189
+ * const schema = z.string().email();
190
+ * const email = validateWithSchema(schema, 'user@example.com', 'email');
191
+ * // email is typed as string
192
+ * ```
193
+ *
194
+ * @internal
195
+ */
196
+ export declare function validateWithSchema<T>(schema: StandardSchemaV1<unknown, T>, value: unknown, propName: string): T;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * @packageDocumentation
3
+ * Props serialization module for cross-domain transfer.
4
+ *
5
+ * @remarks
6
+ * This module handles serializing and deserializing props for transfer
7
+ * between consumer and host windows across domain boundaries.
8
+ */
9
+ import type { PropsDefinition, SerializedProps } from '../types';
10
+ import { FunctionBridge } from '../communication/bridge';
11
+ import type { Messenger } from '../communication/messenger';
12
+ /**
13
+ * Serializes props for cross-domain transfer.
14
+ *
15
+ * @remarks
16
+ * Functions are converted to references, objects are JSON/base64/dotify encoded
17
+ * based on the prop definition's serialization setting.
18
+ *
19
+ * @typeParam P - The props type
20
+ * @param props - Props to serialize
21
+ * @param definitions - Prop definitions
22
+ * @param bridge - Function bridge for serializing functions
23
+ * @returns Serialized props ready for postMessage
24
+ *
25
+ * @public
26
+ */
27
+ export declare function serializeProps<P extends Record<string, unknown>>(props: P, definitions: PropsDefinition<P>, bridge: FunctionBridge): SerializedProps;
28
+ /**
29
+ * Deserializes props received from the consumer.
30
+ *
31
+ * @remarks
32
+ * Function references are converted back to callable functions that
33
+ * invoke the original via postMessage.
34
+ *
35
+ * @typeParam P - The props type
36
+ * @param serialized - Serialized props from consumer
37
+ * @param definitions - Prop definitions
38
+ * @param messenger - Messenger for function calls
39
+ * @param bridge - Function bridge for deserializing functions
40
+ * @param consumerWin - Consumer window reference
41
+ * @param consumerDomain - Consumer origin domain
42
+ * @returns Deserialized props
43
+ *
44
+ * @public
45
+ */
46
+ export declare function deserializeProps<P extends Record<string, unknown>>(serialized: SerializedProps, definitions: PropsDefinition<P>, messenger: Messenger, bridge: FunctionBridge, consumerWin: Window, consumerDomain: string): P;
47
+ /**
48
+ * Creates a deep clone of props.
49
+ *
50
+ * @remarks
51
+ * Functions are passed by reference, objects are deep cloned using
52
+ * structuredClone, and primitives are copied directly.
53
+ *
54
+ * @typeParam P - The props type
55
+ * @param props - Props to clone
56
+ * @returns Cloned props
57
+ *
58
+ * @public
59
+ */
60
+ export declare function cloneProps<P extends Record<string, unknown>>(props: P): P;
@@ -0,0 +1,213 @@
1
+ import type { Dimensions, IframeAttributes, IframeStyles } from '../types';
2
+ /**
3
+ * Configuration options for creating an iframe.
4
+ *
5
+ * @public
6
+ */
7
+ export interface IframeOptions {
8
+ /**
9
+ * The URL to load in the iframe.
10
+ */
11
+ url: string;
12
+ /**
13
+ * The name attribute for the iframe, used for targeting.
14
+ */
15
+ name: string;
16
+ /**
17
+ * The parent HTML element that will contain the iframe.
18
+ */
19
+ container: HTMLElement;
20
+ /**
21
+ * The width and height dimensions for the iframe.
22
+ */
23
+ dimensions: Dimensions;
24
+ /**
25
+ * Optional additional HTML attributes to set on the iframe element.
26
+ */
27
+ attributes?: IframeAttributes;
28
+ /**
29
+ * Optional CSS styles to apply to the iframe element.
30
+ */
31
+ style?: IframeStyles;
32
+ }
33
+ /**
34
+ * Creates an iframe element with the specified options and appends it to a container.
35
+ *
36
+ * @remarks
37
+ * This function creates a fully configured iframe with sensible defaults for
38
+ * security and appearance. It sets up sandbox restrictions, removes borders,
39
+ * and ensures cross-browser compatibility.
40
+ *
41
+ * The iframe is appended to the container before setting the `src` attribute,
42
+ * as some browsers require the iframe to be in the DOM before loading content.
43
+ *
44
+ * If no sandbox attribute is provided, a default secure sandbox policy is applied:
45
+ * `allow-scripts allow-same-origin allow-forms allow-popups allow-popups-to-escape-sandbox`
46
+ *
47
+ * @param options - Configuration options for the iframe
48
+ * @returns The created HTMLIFrameElement, already appended to the container
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * const iframe = createIframe({
53
+ * url: 'https://example.com/widget',
54
+ * name: 'my-widget',
55
+ * container: document.getElementById('widget-container')!,
56
+ * dimensions: { width: 400, height: 300 },
57
+ * attributes: { allow: 'payment' }
58
+ * });
59
+ * ```
60
+ *
61
+ * @public
62
+ */
63
+ export declare function createIframe(options: IframeOptions): HTMLIFrameElement;
64
+ /**
65
+ * Creates an iframe for displaying a prerender/loading state.
66
+ *
67
+ * @remarks
68
+ * This function creates a lightweight iframe intended to show a loading
69
+ * placeholder while the actual content iframe is being prepared. The iframe
70
+ * uses `srcdoc` with an empty HTML document, avoiding any external network requests.
71
+ *
72
+ * The prerender iframe uses a special reserved name `__forgeframe_prerender__`
73
+ * to distinguish it from content iframes.
74
+ *
75
+ * @param container - The parent HTML element to append the iframe to
76
+ * @param dimensions - The width and height for the prerender iframe
77
+ * @returns The created prerender HTMLIFrameElement
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * const prerenderIframe = createPrerenderIframe(
82
+ * document.getElementById('container')!,
83
+ * { width: 400, height: 300 }
84
+ * );
85
+ * ```
86
+ *
87
+ * @public
88
+ */
89
+ export declare function createPrerenderIframe(container: HTMLElement, dimensions: Dimensions): HTMLIFrameElement;
90
+ /**
91
+ * Destroys an iframe by clearing its source and removing it from the DOM.
92
+ *
93
+ * @remarks
94
+ * This function performs a clean teardown of an iframe by first navigating
95
+ * it to `about:blank` to stop any ongoing loading or scripts, then removing
96
+ * it from its parent node. Any errors during cleanup are silently ignored
97
+ * to ensure the cleanup process completes without throwing.
98
+ *
99
+ * @param iframe - The iframe element to destroy
100
+ *
101
+ * @example
102
+ * ```typescript
103
+ * const iframe = createIframe({ ... });
104
+ * // Later, when done with the iframe:
105
+ * destroyIframe(iframe);
106
+ * ```
107
+ *
108
+ * @public
109
+ */
110
+ export declare function destroyIframe(iframe: HTMLIFrameElement): void;
111
+ /**
112
+ * Resizes an iframe to the specified dimensions.
113
+ *
114
+ * @param iframe - The iframe element to resize
115
+ * @param dimensions - The new width and height to apply
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * resizeIframe(iframe, { width: 600, height: 400 });
120
+ * resizeIframe(iframe, { width: '100%', height: 'auto' });
121
+ * ```
122
+ *
123
+ * @public
124
+ */
125
+ export declare function resizeIframe(iframe: HTMLIFrameElement, dimensions: Dimensions): void;
126
+ /**
127
+ * Makes an iframe visible by resetting its display and visibility styles.
128
+ *
129
+ * @remarks
130
+ * This function clears the `display` style (reverting to default) and sets
131
+ * `visibility` to `'visible'`. Use this in conjunction with {@link hideIframe}
132
+ * to toggle iframe visibility.
133
+ *
134
+ * @param iframe - The iframe element to show
135
+ *
136
+ * @example
137
+ * ```typescript
138
+ * hideIframe(iframe);
139
+ * // Later:
140
+ * showIframe(iframe);
141
+ * ```
142
+ *
143
+ * @public
144
+ */
145
+ export declare function showIframe(iframe: HTMLIFrameElement): void;
146
+ /**
147
+ * Hides an iframe by setting display to none and visibility to hidden.
148
+ *
149
+ * @remarks
150
+ * This function sets both `display: none` and `visibility: hidden` to ensure
151
+ * the iframe is completely hidden and does not affect layout. Use
152
+ * {@link showIframe} to make the iframe visible again.
153
+ *
154
+ * @param iframe - The iframe element to hide
155
+ *
156
+ * @example
157
+ * ```typescript
158
+ * showIframe(iframe);
159
+ * // Later:
160
+ * hideIframe(iframe);
161
+ * ```
162
+ *
163
+ * @public
164
+ */
165
+ export declare function hideIframe(iframe: HTMLIFrameElement): void;
166
+ /**
167
+ * Attempts to focus an iframe and its content window.
168
+ *
169
+ * @remarks
170
+ * This function tries to focus both the iframe element itself and its
171
+ * `contentWindow`. Cross-origin restrictions may prevent focusing the
172
+ * content window, in which case errors are silently caught and ignored.
173
+ *
174
+ * @param iframe - The iframe element to focus
175
+ *
176
+ * @example
177
+ * ```typescript
178
+ * // Focus the iframe after user interaction
179
+ * button.addEventListener('click', () => {
180
+ * focusIframe(iframe);
181
+ * });
182
+ * ```
183
+ *
184
+ * @public
185
+ */
186
+ export declare function focusIframe(iframe: HTMLIFrameElement): void;
187
+ /**
188
+ * Retrieves the content dimensions from an iframe for auto-resize functionality.
189
+ *
190
+ * @remarks
191
+ * This function attempts to read the actual content dimensions from the iframe's
192
+ * document. It calculates the maximum of various dimension properties (`scrollWidth`,
193
+ * `offsetWidth`, `clientWidth`, etc.) from both the body and documentElement to
194
+ * ensure accurate measurements across different browsers.
195
+ *
196
+ * This function will return `null` if:
197
+ * - The iframe's content document is not accessible (cross-origin restrictions)
198
+ * - Any error occurs while reading dimensions
199
+ *
200
+ * @param iframe - The iframe element to measure
201
+ * @returns The content dimensions, or `null` if dimensions cannot be determined
202
+ *
203
+ * @example
204
+ * ```typescript
205
+ * const dimensions = getIframeContentDimensions(iframe);
206
+ * if (dimensions) {
207
+ * resizeIframe(iframe, dimensions);
208
+ * }
209
+ * ```
210
+ *
211
+ * @public
212
+ */
213
+ export declare function getIframeContentDimensions(iframe: HTMLIFrameElement): Dimensions | null;
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @packageDocumentation
3
+ *
4
+ * Rendering utilities for ForgeFrame.
5
+ *
6
+ * This module provides functions for creating and managing iframes, popups,
7
+ * and their associated templates. It handles the visual rendering layer
8
+ * of ForgeFrame components.
9
+ *
10
+ * @remarks
11
+ * The render module is divided into three main areas:
12
+ * - **Iframe management** - Creating, destroying, and manipulating iframes
13
+ * - **Popup management** - Opening, closing, and monitoring popup windows
14
+ * - **Templates** - Default templates and styling utilities for containers
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * import { createIframe, openPopup, defaultContainerTemplate } from '@forgeframe/render';
19
+ *
20
+ * // Create an iframe
21
+ * const iframe = createIframe({
22
+ * url: 'https://example.com',
23
+ * name: 'my-iframe',
24
+ * container: document.body,
25
+ * dimensions: { width: 400, height: 300 }
26
+ * });
27
+ *
28
+ * // Open a popup
29
+ * const popup = openPopup({
30
+ * url: 'https://example.com',
31
+ * name: 'my-popup',
32
+ * dimensions: { width: 600, height: 400 }
33
+ * });
34
+ * ```
35
+ */
36
+ export { createIframe, createPrerenderIframe, destroyIframe, resizeIframe, showIframe, hideIframe, focusIframe, getIframeContentDimensions, type IframeOptions, } from './iframe';
37
+ export { openPopup, closePopup, focusPopup, isPopupBlocked, watchPopupClose, resizePopup, PopupOpenError, type PopupOptions, } from './popup';
38
+ export { defaultContainerTemplate, defaultPrerenderTemplate, applyDimensions, createStyleElement, fadeIn, fadeOut, swapPrerenderContent, } from './templates';