npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-listenable-frame

v1.0.0

Published

React component - wrapper for standard `<iframe>` component which provides listening of frame messages and posting messages to this frame.

Downloads

1

Readme

react-listenable-frame

React component - wrapper for standard <iframe> component which provides listening of frame messages and posting messages to this frame.

Installaction

By NPM:

npm install react-listenable-frame --save

Or by Yarn:

yarn add react-listenable-frame

Usage

Quick example

Connecting a frame that announces its readiness and exchanges ping messages with the parent window:

Component code:

import React, { useRef, useCallback, useState } from 'react';
import ReactListenableFrame from 'react-listenable-frame';

const MyComponent = () => {
    const senderRef = useRef(null);
    const [ready, setReady] = useState(false);

    const onMessage = useCallback((event) => {
        if (event.data === 'ready') {
            setReady(true);
        } else if (event.data === 'pong') {
            alert('Frame received "ping" message');
        }
    }, [setReady]);

    const onPingClick = useCallback(() => {
        if (senderRef.current) {
            senderRef.current('ping', '*');
        }
    }, []);

    return (
        <div>
            <h1>Listenable frame</h1>
            <ReactListenableFrame
                title="MyFrame"
                src="/my-frame.html"
                senderRef={senderRef}
                onMessage={onMessage}
            />
            <button disabled={!ready} onClick={onPingClick}>
                Ping listenable frame 
            </button>
        </div>
    );
};

export default MyComponent;

Frame code

window.addEventListener('load', () => {
    window.addEventListener('message', (event) => {
        if (event.data === 'ping') {
            window.parent.postMessage('pong', '*');
        }
    });
    window.parent.postMessage('ready', '*');
});

Component properties

  • senderRef - mutable React Ref object (result of useRef hook). The current property will be a wrapper function for postMessage method of frame element. This is the one way to send messages to the created frame;
  • onMessage - callback function for "message" which calls only for created frame element;

The rest of the properties are equivalent to those of the <iframe> component.

Component showcase

You can explore a sample application that demonstrates the capabilities of this package.

Package bundles

The package is available in three bundles:

  • CommonJS module (for Node.JS or browserify);
  • ES6 module (for webpack, rollup or modern Node.JS versions);
  • Transpiled and minified browser bundle (For use in the browser and, in the future, on the CDN);

CommonJS module

By default, Node.JS (and browserify) will use CommonJS module.

This code:

const ReactListenableFrame = require('react-listenable-frame');

is equivalent to:

const ReactListenableFrame = require('react-listenable-frame/lib/react-listenable-frame.js');

ES6 Module

You can use ES6 Module.

For rollup and webpack this record:

import ReactListenableFrame from 'react-listenable-frame';

is equivalent to:

import ReactListenableFrame from 'react-listenable-frame/lib/react-listenable-frame.mjs';

Browser bundle

The package is still not presented on CDN servers, but you can take a ready-made bundle for the browser:

<your-project-dir>/node_modules/react-listenable-frame/lib/react-listenable-frame.min.js

And the source map, if you want:

<your-project-dir>/node_modules/react-listenable-frame/lib/react-listenable-frame.min.js.map

Compoment will be available as ReactListenableFrame global variable.