Coder Social home page Coder Social logo

uiwjs / react-iframe Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 2.0 8.42 MB

This component allows you to wrap your entire React application or each component in an <iframe>.

Home Page: https://uiwjs.github.io/react-iframe

License: MIT License

Shell 0.71% TypeScript 86.84% HTML 12.45%
iframe react react-component

react-iframe's Introduction

react-iframe

Build & Deploy Coverage Status npm version

This component allows you to wrap your entire React application or each component in an <iframe>.

Features

๐Ÿ“š Use Typescript to write, better code hints.
๐Ÿ“ฆ Zero dependencies.
๐Ÿ Encapsulation based on function components.
๐Ÿ„ Provides comprehensive code test coverage.
๐ŸŒ Complete official document demo preview.

Installation

npm i @uiw/react-iframe

Basic Usage

import React from 'react';
import IFrame from '@uiw/react-iframe';

export default function Demo() {
  return (
    <IFrame>
      <h1>Hello World!</h1>
    </IFrame>
  );
}

head

The head prop is a dom node that gets inserted before the children of the frame.

import React from 'react';
import IFrame from '@uiw/react-iframe';

const head = (
  <style>{`body { background: red; }`}</style>
);

export default function Demo() {
  return (
    <IFrame head={head}>
      <h1>Hello World!</h1>
    </IFrame>
  );
}

initialContent

The initialContent props is the initial html injected into frame. It is only injected once, but allows you to insert any html into the frame (e.g. a <head> tag, <script> tags, etc). Note that it does not update if you change the prop.

Defaults to <!DOCTYPE html><html><head></head><body></body></html>

import React from 'react';
import IFrame from '@uiw/react-iframe';

const initialContent = '<!DOCTYPE html><html><head><title>React IFrame</title><meta name="keywords" content="react,iframe,component,development" /></head><body></body></html>';

export default function Demo() {
  return (
    <IFrame initialContent={initialContent}>
      <div style={{ fontSize: 32, color: 'red' }}>Hello World!</div>
    </IFrame>
  );
}

mountTarget

The mountTarget attribute is a css selector (#target/.target) that specifies the child within the initial content of the iframe to be mounted.

import React from 'react';
import IFrame from '@uiw/react-iframe';

const initialContent = '<!DOCTYPE html><html><head></head><body><h1>i wont be changed</h1><div id="mountHere"></div></body></html>';

export default function Demo() {
  return (
    <IFrame initialContent={initialContent} mountTarget="#mountHere">
      <div style={{ fontSize: 32, color: 'red' }}>Hello World!</div>
    </IFrame>
  );
}

ref

The ref prop provides a way to access inner <iframe> DOM node.

import React, { useEffect, useState, Fragment } from 'react';
import IFrame from '@uiw/react-iframe';

export default function Demo() {
  const [iframeRef, setIframeRef] = useState();
  const [count, setCount] = useState(0);

  useEffect(() => {
    if (iframeRef) {
      iframeRef.focus();
    }
  }, [iframeRef]);

  const click = () => setCount(count + 1);
  return (
    <Fragment>
      <button onClick={click} style={{ display: 'flex' }}>Click</button>
      <IFrame ref={(node) => node && setIframeRef(node)}>
        <div>Hello World!</div>
        <div style={{ fontSize: 32, color: 'red' }}>{count}</div>
      </IFrame>
    </Fragment>
  );
}

src

Open a URL in an <iframe>.

import React, { useEffect, useState, Fragment } from 'react';
import IFrame from '@uiw/react-iframe';

export default function Demo() {
  return (
    <IFrame src="https://wangchujiang.com/" style={{ width: '100%', height: 320 }} />
  );
}

Accessing the iframe's window and document

The iframe's window and document may be accessed via the FrameContext.Consumer or the useFrame hook.

import React, { useEffect, useState, Fragment } from 'react';
import IFrame, { FrameContext } from '@uiw/react-iframe';

export default function Demo() {
  return (
    <IFrame>
      <FrameContext.Consumer>
        {({ document, window }) => {
          return (
            <div>
              <div>Hello World!</div>
            </div>
          )
        }}
      </FrameContext.Consumer>
    </IFrame>
  );
}

The example with useFrame hook:

import React, { useEffect, useState, Fragment } from 'react';
import IFrame, { useFrame } from '@uiw/react-iframe';

const InnerComponent = () => {
  // Hook returns iframe's window and document instances from Frame context
  const { document, window } = useFrame();
  return (
    <div>
      <div>Hello World!</div>
    </div>
  );
};

export default function Demo() {
  return (
    <IFrame>
      <InnerComponent />
    </IFrame>
  );
}

Event

import React, { useEffect, useState, Fragment } from 'react';
import IFrame, { useFrame } from '@uiw/react-iframe';

const InnerComponent = () => {
  // Hook returns iframe's window and document instances from Frame context
  const { document, window } = useFrame();
  return (
    <div>
      <div>Hello World!</div>
    </div>
  );
};

export default function Demo() {
  const onLoad = (evn) => {
    console.log("iframe loaded successfully!", evn)
  }
  return (
    <IFrame onLoad={onLoad}>
      <InnerComponent />
    </IFrame>
  );
}

Props

export interface IFrameProps extends React.HTMLAttributes<HTMLIFrameElement> {
  head?: React.ReactNode;
  initialContent?: string;
  mountTarget?: string;
}
declare const IFrame: import("react").ForwardRefExoticComponent<IFrameProps & import("react").RefAttributes<HTMLIFrameElement>>;
export default IFrame;

Development

Runs the project in development mode.

# Step 1, run first, listen to the component compile and output the .js file
# listen for compilation output type .d.ts file
npm run watch
# Step 2, development mode, listen to compile preview website instance
npm run start

Builds the app for production to the build folder.

npm run build

The build is minified and the filenames include the hashes. Your app is ready to be deployed!

Contributors

As always, thanks to our amazing contributors!

Made with contributors.

License

Licensed under the MIT License.

react-iframe's People

Contributors

jaywcjlove avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

react-iframe's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): update actions/checkout action to v4
  • chore(deps): update actions/setup-node action to v4

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-node v3
  • actions/checkout v3
  • actions/setup-node v3
  • peaceiris/actions-gh-pages v3
  • ncipollo/release-action v1
npm
core/package.json
  • @babel/runtime ^7.18.9
  • @types/react ^18.0.17
  • @types/react-dom ^18.0.6
  • @types/react-test-renderer ^18.0.0
  • react ^18.2.0
  • react-dom ^18.2.0
  • react-test-renderer ^18.2.0
  • react >=16.9.0
  • react-dom >=16.9.0
package.json
  • @kkt/less-modules ^7.5.2
  • @kkt/ncc ^1.0.15
  • @testing-library/react ^14.0.0
  • @testing-library/user-event ^14.4.3
  • husky ^8.0.1
  • jest ^29.6.4
  • jest-environment-jsdom ^29.6.0
  • jest-environment-node ^29.6.0
  • jest-watch-typeahead ^2.2.2
  • lerna ^7.1.4
  • lint-staged ^14.0.0
  • prettier ^3.0.1
  • tsbb ^4.1.14
  • node >=16.0.0
website/package.json
  • @uiw/react-markdown-preview-example ^1.5.3
  • react ^18.2.0
  • react-dom ^18.2.0
  • @types/react ^18.0.17
  • @types/react-dom ^18.0.6
  • kkt ^7.4.9
  • markdown-react-code-preview-loader ^2.1.2

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.