- 🐜 1 kilobyte gzipped for both parent and frame code
- 🌴 Tree-shakable by default - import only what you need to achieve responsiveness
- ⚡️ Speaks AMP and is compatible with
|Internet Explorer 11||✅|
|Internet Explorer 10 and lower||⛔️|
@newswire/frames is available via
npm install @newswire/frames
You can also use it directly via unpkg.com.
<!-- Now available at `window.newswireFrames` -->
You can also import it as a module via unpkg!
From the host page (framer or parent)
The page that contains the embeds needs to use the
Framer class to set up instances for each embed.
Assume we have the following markup in our HTML:
Then, in our script:
;const container = document;const src = '';const framer = container src ;// Now the iframe has been added to the page and is listening for height changes notifications from within the iframe
A popular feature of Pym.js was the ability to automatically initialize embeds that had matching attibutes on their container elements.
@newswire/frames also includes this capability.
Assume we have the following markup in our HTML:
Then in our script, we can skip the fanfare of setting up a
Framer for each one and use the
data-frame-src attribute to find them.
;// looks for any elements with `data-frame-src` that haven't been initialized yet, and sets them up;
If you're needing to pass any of the other options to
Framer when you're automatically creating the embeds, you can add matching data attributes that the initializer will pick up and pass along.
From the embedded page (frame or child)
While the code to setup the host page is similar to Pym's
Parent class, the methods for making the iframed page communicate with the host page are a little different.
Want to set it and forget it? You can import a function that sets up listeners and sends the initial height of the frame's content.
;// 1. Sends the initial frame's content height// 2. Sets up an one-time istener to send the height on load// 3. Sets up a listener to send the height every time the frame resizes;
You can also automatically set up long polling for height changes as well.
;// 1. Sends the initial frame's content height// 2. Sets up an one-time listener to send the height on load// 3. Sets up a listener to send the height every time the frame resizes// 4. Sets up an interval to send a new height update every 300ms;
Alternatively, you can set and use function independently depending on the needs of your frame's content.
;// 1. Sends the initial frame's content height;// 2. Sets up an one-time listener to send the height on load;// 3. Sets up a listener to send the height every time the frame resizes;// 4. Sets up an interval to send a new height update every 150ms;// 1-3 is identical to initFrame()! 1-4 is identical to initFrameAndPoll()!
initFrame() will be enough, but if you have code that will potentially change the height of the frame's content (like with an
<button> press) and would rather not use polling, you can use
sendFrameHeight() to manually recalculate and send an update to the parent page.
Table of Contents
The Framer object to be called in the host page. Effectively a wrapper around interactions with an embedded iframe.
optionsobject options used to prepare the iframe
options.allowfullscreenboolean? toggles the
allowfullscreenattribute (optional, default
options.containerElement the containing DOM element for the iframe
options.namestring? sets the
options.referrerpolicystring? sets the
options.sandboxstring? sets the
sandboxattribute (optional, default
options.srcstring the URL to set as the
srcof the iframe
options.titlestring? sets the
Removes event listeners and removes the iframe from the container.
const framer = ...;// tears down the Framerframer;
Automatically initializes any frames that have not already been auto-activated.
// sets up all frames that have not been initialized yet;
Returns Array An array of all the created Framers
Sends the current document's height or provided value to the host window using postMessage.
heightnumber? The height to pass to the host page, is determined automatically if not passed (optional, default
// Uses the document's height to tell the host page;// Pass a height you've determined in another way;
Sets up an event listener for the load event that sends the new frame height to the host. Automatically removes itself once fired.
// once the frame's load event is fired, tell the host page its new height;
Sets up an event listener for the resize event that sends the new frame height to the host.
// every time the frame is resized, tell the host page what its new height is;
Sends height updates to the host page on an interval.
delaynumber? How long to set the interval (optional, default
// will call sendFrameHeight every 300ms;// will call sendFrameHeight every 150ms;
A helper for running the standard functions for setting up a frame.
Automatically calls an sendFrameHeight, sendHeightOnLoad and sendHeightOnResize.
Initializes a frame, then sets up a poll to continue to update on an interval.
delaynumber? An optional custom delay to pass to sendHeightOnPoll
// calls initFrame, then calls sendHeightOnPoll;