sethvincent / frames

πŸ–Ό A greenfield take on responsive iframes in the spirit of Pym.js.

Home Page:https://npm.im/@newswire/frames

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@newswire/frames

npm dependencies gzip size brotli size install size

@newswire/frames is a greenfield take on responsive iframes in the spirit of Pym.js.

Key features

  • 🐜 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

Supported browsers

Browser Supported
Safari βœ…
Mozilla Firefox βœ…
Google Chrome βœ…
Opera βœ…
Microsoft Edge βœ…
Internet Explorer 11 βœ…
Internet Explorer 10 and lower ⛔️

Installation

@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>

Usage

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:

<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>

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.

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.

API

Table of Contents

Framer

The Framer object to be called in the host page. Effectively a wrapper around interactions with an embedded iframe.

Parameters

  • options object options used to prepare the iframe
    • options.allowfullscreen boolean? toggles the allowfullscreen attribute (optional, default false)
    • options.container Element the containing DOM element for the iframe
    • options.name string? sets the name attribute
    • options.referrerpolicy string? sets the referrerpolicy attribute
    • options.sandbox string? sets the sandbox attribute (optional, default 'allow-scripts')
    • options.src string the URL to set as the src of the iframe
    • options.title string? sets the title attribute

remove

Removes event listeners and removes the iframe from the container.

Examples
const framer = new Framer(...);
// tears down the Framer
framer.remove();

Returns void

autoInitFrames

Automatically initializes any frames that have not already been auto-activated.

Examples

// sets up all frames that have not been initialized yet
autoInitFrames();

Returns Array An array of all the created Framers

sendFrameHeight

Sends the current document's height or provided value to the host window using postMessage.

Parameters

  • height number? The height to pass to the host page, is determined automatically if not passed (optional, default getDocumentHeight())

Examples

// Uses the document's height to tell the host page
sendFrameHeight();

// Pass a height you've determined in another way
sendFrameHeight(500);

Returns void

sendHeightOnLoad

Sets up an event listener for the load event that sends the new frame height to the host. Automatically removes itself once fired.

Examples

// once the frame's load event is fired, tell the host page its new height
sendHeightOnLoad();

Returns void

sendHeightOnResize

Sets up an event listener for the resize event that sends the new frame height to the host.

Examples

// every time the frame is resized, tell the host page what its new height is
sendHeightOnResize();

Returns void

sendHeightOnPoll

Sends height updates to the host page on an interval.

Parameters

  • delay number? How long to set the interval (optional, default 300)

Examples

// will call sendFrameHeight every 300ms
sendHeightOnPoll();

// will call sendFrameHeight every 150ms
sendHeightOnPoll(150);

Returns void

initFrame

A helper for running the standard functions for setting up a frame.

Automatically calls an sendFrameHeight, sendHeightOnLoad and sendHeightOnResize.

Examples

initFrame();

Returns void

initFrameAndPoll

Initializes a frame, then sets up a poll to continue to update on an interval.

Parameters

  • delay number? An optional custom delay to pass to sendHeightOnPoll

Examples

// calls initFrame, then calls sendHeightOnPoll
initFrameAndPoll();

Returns void

License

MIT

About

πŸ–Ό A greenfield take on responsive iframes in the spirit of Pym.js.

https://npm.im/@newswire/frames

License:MIT License


Languages

Language:JavaScript 100.0%