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

v0.0.3

Published

React component for iframe with message transfer

Downloads

5

Readme

React component a layer for message transfer between iframe and parent.

In React


// Parent page, which have a iframe page
import React, {Component} from 'react';
import {IFramer} from '@ali/iframer';

class Parent extends Component {
	render() {
		const src='https://the.url.of.iframe.page';
		return (
			<IFramer src={src} />
		);
	}
}
// Child page,which will iframed in parent page
import React, {Component} from 'react';
import {iframer} from '@ali/iframer';

@iframer
class Child extends Component {
	render() {
		return (
			<div style={{height: 1000}}>
				This is the iframe page.
			</div>
		);
	}
}

IFramer

React component, which is replace of iframe label IFramer component consist of message listener, message executor and message emitter IFramer component realized the syncHeight action inside of it, needn't the action again other you need custom syncHeight action.

  • src: string required, the url for iframe
  • style: object optional,the custom style for iframe label
  • width: string || number optional, the width of iframe, default is 100%
  • height: string || number optional, the height of iframe, default is 800
  • iframeName: string optional, the name for iframe page, which will send to iframe page within the message body as to field
  • actions: object optional, method set, the action in set will execute once the iframe send action message to parent page

IFramer.emit

Send message to iframe page, e.g.: this.iframe.emit({action: 'updateTimestamp', data: Date.now()}) the format of message the iframe page received is: {action: 'updateTimestamp', data: '150693657122', to: iframeName, source: 'IFramer'}

iframer

High Order Component, enhance the iframe component, consist of message listener, message executor and message emitter There are 3 types for using:

  • Decorator: add the decorator to iframe component directly
@iframer
class Child extends Component { ... }
  • Decorator with config: add the decorator with a config
const config = {
	dom: 'body', // get the DOM with document.querySelector(dom) if set, default DOM is the iframe page component
	targetOrigin: 'https://www.xxx.com' // default is '*'
}
  • Function
class Child extends Component { ... }
export iframer(Child, config);

iframer.emit

Send message to parent page: this.emit({action: 'updateTimestamp', data: Date.now()}) The format for message parent received: {action: 'updateTimestamp', data: '150693657122', from: iframeName, source: 'IFramer'}

Example

The requirement:

  1. The Parent page need embed a iframe page which we called Child
  2. Each page for Parent and Child have a state field with "timestamp" individually) for display
  3. There is a Button in Parent need update the timestamp in Child, in turn, same as Child

There is not some stupid requirement in real world, it's just a demo Method: Parent and Child realized the "updateTimestamp" action individually for themselves, the Parent will send action message to iframe to invoke the "updateTimestamp" method, the same as Child.

// Parent
import React, {Component} from 'react';
import {IFramer} from 'react-iframer';

class Parent extends Component {
	state = {
		timestamp: 0
	}

	/**
	 * update the timestamp field, for invoking by iframe
	 * there will have no effect if this method was supply
	 * @param timestamp, the data value in message
	 * @param message, the message body
	 */
	updateTimestamp = (timestamp, message) => {
		this.setState({timestamp})
	}

	/**
	 * send action message to iframe page to invoke method in iframe component
	 */
	invokeIframeMethod = (action) => {
		return () => {
			let timestamp = Date.now();
			this.iframe.emit({action, timestamp});
		};
	}

	render() {
		let {timestamp} = this.state;
		let src = 'http://www.iframe.url.page';

		let actions = {updateTimestamp: this.updateTimestamp};

		return (
			<div>
				<div>
				  <button type="primary"
					  onClick={this.invokeIframeMethod('updateTimestamp')}>
				  update the iframe (Child) timestamp
				  </button>
				</div>
				<div>{timestamp}</div>
				<div>
					<Iframer
					src={src}
					iframeName="Child" style={{height: 100}}
					ref={ref => {this.iframe = ref}} actions={actions} />
				</div>
			</div>
		)
	}
}
// Child
import React, {Component} from 'react';
import {iframer} from '@ali/iframer';
import {Button} from '@alife/next';

@iframer
class Child extends Component {
	state = {
		timestamp: 0
	};

	/**
	 * Update the timestamp field, for invoking by parent's action message
	 * @param timestamp, the data value in message
     * @param message, the message body
	 */
	updateTimestamp = (timestamp, message) => {
		this.setState({timestamp})
	}

	/**
	 * send action message to Parent page
	 */
	invokeParentMethod = (action) => {
		return () => {
			let timestamp = Date.now();
			this.props.emit({action, timestamp});
		};
	}

	render() {
		let {timestamp} = this.state;

		return (
			<div>
				<div>
				  <button onClick={this.invokeParentMethod('updateTimestamp')}>
				  Update Parent's timestamp
				  </button>
				</div>
				<div>{timestamp}</div>
			</div>
		)
	}

}