1 Star 0 Fork 0

uiw/react-iframe

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

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>
  );
}

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.

MIT License Copyright (c) 2022 uiw Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

这个组件允许你将整个React应用程序或每个组件包装在一个<iframe>中。 展开 收起
TypeScript 等 3 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/uiw/react-iframe.git
git@gitee.com:uiw/react-iframe.git
uiw
react-iframe
react-iframe
main

搜索帮助