@newswire/frames
is a greenfield take on responsive iframes in the spirit of Pym.js.
- π 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
amp-iframe
Browser | Supported |
---|---|
Safari | β |
Mozilla Firefox | β |
Google Chrome | β |
Opera | β |
Microsoft Edge | β |
Internet Explorer 11 | β |
Internet Explorer 10 and lower | βοΈ |
@newswire/frames
is available via npm
.
npm install @newswire/frames
You can also use it directly via unpkg.com.
<script src="https://unpkg.com/@newswire/frames/dist/index.umd.js"></script>
<!-- Now available at `window.newswireFrames` -->
You can also import it as a module via unpkg!
<script type="module">
import * as frames from 'https://unpkg.com/@newswire/frames/dist/index.mjs';
frames.autoInitFrames();
</script>
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:
<div id="embed-container">Loading...</div>
Then, in our script:
import { Framer } from '@newswire/frames';
const container = document.getElementById('embed-container');
const src = 'https://i-am-an-embed/';
const framer = new 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:
<div data-frame-src="https://i-am-an-embed/"></div>
<div data-frame-src="https://i-am-an-embed-too/"></div>
<div data-frame-src="https://i-am-an-embed-three/"></div>
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.
import { autoInitFrames } from '@newswire/frames';
// looks for any elements with `data-frame-src` that haven't been initialized yet, and sets them up
autoInitFrames();
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.
<div
data-frame-src="https://i-am-an-embed/"
data-frame-sandbox="allow-scripts allow-same-origin"
></div>
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.
import { initFrame } from '@newswire/frames';
// 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
initFrame();
You can also automatically set up long polling for height changes as well.
import { initFrameAndPoll } from '@newswire/frames';
// 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
initFrameAndPoll();
Alternatively, you can set and use function independently depending on the needs of your frame's content.
import {
sendFrameHeight,
sendHeightOnLoad,
sendHeightOnResize,
sendHeightOnPoll,
} from '@newswire/frames';
// 1. Sends the initial frame's content height
sendFrameHeight();
// 2. Sets up an one-time listener to send the height on load
sendHeightOnLoad();
// 3. Sets up a listener to send the height every time the frame resizes
sendHeightOnResize();
// 4. Sets up an interval to send a new height update every 150ms
sendHeightOnPoll(150);
// 1-3 is identical to initFrame()! 1-4 is identical to initFrameAndPoll()!
Typically using initFrame()
will be enough, but if you have code that will potentially change the height of the frame's content (like with an <input>
or <button>
press) and would rather not use polling, you can use sendFrameHeight()
to manually recalculate and send an update to the parent page.
- Framer
- autoInitFrames
- sendFrameHeight
- sendHeightOnLoad
- sendHeightOnResize
- sendHeightOnPoll
- initFrame
- initFrameAndPoll
The Framer object to be called in the host page. Effectively a wrapper around interactions with an embedded iframe.
options
object options used to prepare the iframeoptions.allowfullscreen
boolean? toggles theallowfullscreen
attribute (optional, defaultfalse
)options.container
Element the containing DOM element for the iframeoptions.name
string? sets thename
attributeoptions.referrerpolicy
string? sets thereferrerpolicy
attributeoptions.sandbox
string? sets thesandbox
attribute (optional, default'allow-scripts'
)options.src
string the URL to set as thesrc
of the iframeoptions.title
string? sets thetitle
attribute
Removes event listeners and removes the iframe from the container.
const framer = new Framer(...);
// tears down the Framer
framer.remove();
Returns void
Automatically initializes any frames that have not already been auto-activated.
// sets up all frames that have not been initialized yet
autoInitFrames();
Returns Array An array of all the created Framers
Sends the current document's height or provided value to the host window using postMessage.
height
number? The height to pass to the host page, is determined automatically if not passed (optional, defaultgetDocumentHeight()
)
// Uses the document's height to tell the host page
sendFrameHeight();
// Pass a height you've determined in another way
sendFrameHeight(500);
Returns void
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
sendHeightOnLoad();
Returns void
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
sendHeightOnResize();
Returns void
Sends height updates to the host page on an interval.
delay
number? How long to set the interval (optional, default300
)
// will call sendFrameHeight every 300ms
sendHeightOnPoll();
// will call sendFrameHeight every 150ms
sendHeightOnPoll(150);
Returns void
A helper for running the standard functions for setting up a frame.
Automatically calls an sendFrameHeight, sendHeightOnLoad and sendHeightOnResize.
initFrame();
Returns void
Initializes a frame, then sets up a poll to continue to update on an interval.
delay
number? An optional custom delay to pass to sendHeightOnPoll
// calls initFrame, then calls sendHeightOnPoll
initFrameAndPoll();
Returns void
MIT