/** * iframe-resize (parent) * ---------------------- * A small, dependency-free, clean-room implementation of an iframe * auto-resizer parent. It attaches to one or more iframes and keeps their * height (and/or width) in sync with the size of their content, which is * reported by the matching child script running inside the iframe. * * This is API-compatible with the subset of `@iframe-resizer/parent` that this * project relies on: * - default export `iframeResize(options, target)` returning an array of * augmented iframe elements * - each returned element exposes `iFrameResizer.close()` * - options: `license`, `checkOrigin`, `direction`, `log`, `warningTimeout`, * `offsetSize`, `onResized`, `onReady`, `onMessage` * * It speaks a private postMessage protocol shared with the child module. Both * ends must be from this implementation (they are: we control both sides). */ import { type Direction } from "./iframe-resize-protocol"; export interface IframeResizeData { iframe: HTMLIFrameElement; height: number; width: number; type: string; } export interface IframeResizeMessageData { iframe: HTMLIFrameElement; message: unknown; } export interface IframeResizeOptions { /** Present for API parity with iframe-resizer; not validated. */ license?: string; /** * Origin checking for incoming messages. * - `true` (default): only accept messages from the iframe's own origin. * - `false`: accept messages from any origin. * - `string[]`: accept messages from any origin in the list (use "*" to * allow all, matching the child's `targetOrigin`). */ checkOrigin?: boolean | string[]; /** Which dimension(s) to resize. Defaults to "vertical" (height only). */ direction?: Direction; /** Enable verbose console logging. */ log?: boolean; /** Milliseconds to wait for the first child message before warning. */ warningTimeout?: number; /** Pixels added to (or removed from) the reported size. */ offsetSize?: number; /** Called after the iframe has been resized. */ onResized?: (data: IframeResizeData) => void; /** Called once the child has completed the initial handshake. */ onReady?: (iframe: HTMLIFrameElement) => void; /** Called for custom (non-resize) messages sent by the child. */ onMessage?: (data: IframeResizeMessageData) => void; } export interface IFrameResizerControl { /** Ask the child to send its current size. */ resize: () => void; /** Send an arbitrary message to the child. */ sendMessage: (message: unknown, targetOrigin?: string) => void; /** Tear down listeners/observers for this iframe. */ close: () => void; } export type IframeResizedElement = HTMLIFrameElement & { iFrameResizer: IFrameResizerControl; }; /** * Attach the auto-resizer to one or more iframes. * * @param options Resizer options (see {@link IframeResizeOptions}). * @param target An iframe element, a CSS selector, a NodeList, or an array of * elements. Non-iframe matches are ignored. * @returns An array of the augmented iframe elements, each exposing an * `iFrameResizer` control object. */ export default function iframeResize(options: IframeResizeOptions | undefined, target: string | HTMLIFrameElement | NodeListOf | Element[]): IframeResizedElement[];