|
1 |
| -react-monorepo-template |
| 1 | +react-iframe |
2 | 2 | ===
|
3 | 3 |
|
4 | 4 | [](https://github.com/uiwjs/react-monorepo-template/actions/workflows/ci.yml)
|
5 | 5 | [](https://uiwjs.github.io/react-monorepo-template/coverage/lcov-report/)
|
6 | 6 |
|
7 |
| -Simple [React](https://github.com/facebook/react) package development project example template. |
| 7 | +This component allows you to wrap your entire React application or each component in an [`<iframe>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe). |
8 | 8 |
|
9 |
| -## Directory Structure |
| 9 | +## Installation |
10 | 10 |
|
11 | 11 | ```bash
|
12 |
| -├── LICENSE |
13 |
| -├── README.md -> core/README.md |
14 |
| -├── core # 📦 package @uiw/react-monorepo-template |
15 |
| -│ ├── README.md |
16 |
| -│ ├── cjs # 🔄 Compiled cjs directory |
17 |
| -│ ├── esm # 🔄 Compiled esm directory |
18 |
| -│ ├── src # Package source directory |
19 |
| -│ ├── dist.css # 🔄 compile less to css |
20 |
| -│ ├── package.json # name => @uiw/react-monorepo-template |
21 |
| -│ └── tsconfig.json |
22 |
| -├── lerna.json |
23 |
| -├── package.json |
24 |
| -├── tsconfig.json |
25 |
| -├── test # ⛑ test case |
26 |
| -└── website # 🐝 Package example test, website |
27 |
| - ├── README.md |
28 |
| - ├── package.json |
29 |
| - ├── public |
30 |
| - ├── src |
31 |
| - └── tsconfig.json |
| 12 | +npm i @uiw/react-iframe |
32 | 13 | ```
|
33 | 14 |
|
34 |
| -## Development |
| 15 | +## Basic Usage |
35 | 16 |
|
36 |
| -1. Install |
| 17 | +```jsx mdx:preview |
| 18 | +import React from 'react'; |
| 19 | +import IFrame from '@uiw/react-iframe'; |
37 | 20 |
|
38 |
| -```bash |
39 |
| -npm install |
| 21 | +export default function Demo() { |
| 22 | + return ( |
| 23 | + <IFrame> |
| 24 | + <h1>Hello World!</h1> |
| 25 | + </IFrame> |
| 26 | + ); |
| 27 | +} |
40 | 28 | ```
|
41 | 29 |
|
42 |
| -2. Dependencies in the installation package and example |
| 30 | +## `head` |
43 | 31 |
|
44 |
| -```bash |
45 |
| -npm run hoist |
46 |
| -``` |
| 32 | +The `head` prop is a dom node that gets inserted before the children of the frame. |
47 | 33 |
|
48 |
| -3. To develop, run the self-reloading build: |
| 34 | +```jsx mdx:preview |
| 35 | +import React from 'react'; |
| 36 | +import IFrame from '@uiw/react-iframe'; |
49 | 37 |
|
50 |
| -```bash |
51 |
| -npm run build # Compile packages 📦 @uiw/react-monorepo-template |
52 |
| -npm run watch # Real-time compilation 📦 @uiw/react-monorepo-template |
| 38 | +const head = ( |
| 39 | + <style>{`body { background: red; }`}</style> |
| 40 | +); |
| 41 | + |
| 42 | +export default function Demo() { |
| 43 | + return ( |
| 44 | + <IFrame head={head}> |
| 45 | + <h1>Hello World!</h1> |
| 46 | + </IFrame> |
| 47 | + ); |
| 48 | +} |
53 | 49 | ```
|
54 | 50 |
|
55 |
| -4. Run Document Website Environment: |
| 51 | +## `initialContent` |
56 | 52 |
|
57 |
| -```bash |
58 |
| -npm run start |
| 53 | +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>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/head) tag, [`<script>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script) tags, etc). Note that it does not update if you change the prop. |
| 54 | + |
| 55 | +Defaults to `<!DOCTYPE html><html><head></head><body></body></html>` |
| 56 | + |
| 57 | +```jsx mdx:preview |
| 58 | +import React from 'react'; |
| 59 | +import IFrame from '@uiw/react-iframe'; |
| 60 | + |
| 61 | +const initialContent = '<!DOCTYPE html><html><head><title>React IFrame</title><meta name="keywords" content="react,iframe,component,development" /></head><body></body></html>'; |
| 62 | + |
| 63 | +export default function Demo() { |
| 64 | + return ( |
| 65 | + <IFrame initialContent={initialContent}> |
| 66 | + <div style={{ fontSize: 32, color: 'red' }}>Hello World!</div> |
| 67 | + </IFrame> |
| 68 | + ); |
| 69 | +} |
59 | 70 | ```
|
60 | 71 |
|
61 |
| -5. To contribute, please fork repos, add your patch and tests for it (in the `test/` folder) and submit a pull request. |
| 72 | +## `mountTarget` |
62 | 73 |
|
| 74 | +The `mountTarget` attribute is a css selector (`#target`/`.target`) that specifies the child within the initial content of the iframe to be mounted. |
| 75 | + |
| 76 | +```jsx mdx:preview |
| 77 | +import React from 'react'; |
| 78 | +import IFrame from '@uiw/react-iframe'; |
| 79 | + |
| 80 | +const initialContent = '<!DOCTYPE html><html><head></head><body><h1>i wont be changed</h1><div id="mountHere"></div></body></html>'; |
| 81 | + |
| 82 | +export default function Demo() { |
| 83 | + return ( |
| 84 | + <IFrame initialContent={initialContent} mountTarget="#mountHere"> |
| 85 | + <div style={{ fontSize: 32, color: 'red' }}>Hello World!</div> |
| 86 | + </IFrame> |
| 87 | + ); |
| 88 | +} |
63 | 89 | ```
|
64 |
| -npm run test |
| 90 | + |
| 91 | +## `ref` |
| 92 | + |
| 93 | +The ref prop provides a way to access inner [`<iframe>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) DOM node. |
| 94 | + |
| 95 | +```tsx mdx:preview |
| 96 | +import React, { useEffect, useState, Fragment } from 'react'; |
| 97 | +import IFrame from '@uiw/react-iframe'; |
| 98 | + |
| 99 | +export default function Demo() { |
| 100 | + const [iframeRef, setIframeRef] = useState(); |
| 101 | + const [count, setCount] = useState(0); |
| 102 | + |
| 103 | + useEffect(() => { |
| 104 | + if (iframeRef) { |
| 105 | + iframeRef.focus(); |
| 106 | + } |
| 107 | + }, [iframeRef]); |
| 108 | + |
| 109 | + const click = () => setCount(count + 1); |
| 110 | + return ( |
| 111 | + <Fragment> |
| 112 | + <button onClick={click} style={{ display: 'flex' }}>Click</button> |
| 113 | + <IFrame ref={(node) => node && setIframeRef(node)}> |
| 114 | + <div>Hello World!</div> |
| 115 | + <div style={{ fontSize: 32, color: 'red' }}>{count}</div> |
| 116 | + </IFrame> |
| 117 | + </Fragment> |
| 118 | + ); |
| 119 | +} |
65 | 120 | ```
|
66 | 121 |
|
| 122 | +## `src` |
67 | 123 |
|
68 |
| -### Using |
| 124 | +The ref prop provides a way to access inner [`<iframe>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe) DOM node. |
69 | 125 |
|
70 |
| -```jsx mdx:preview |
71 |
| -import React from "react"; |
72 |
| -import MonorepoTemplate from '@uiw/react-monorepo-template'; |
| 126 | +```tsx mdx:preview |
| 127 | +import React, { useEffect, useState, Fragment } from 'react'; |
| 128 | +import IFrame from '@uiw/react-iframe'; |
73 | 129 |
|
74 |
| -export default function App() { |
| 130 | +export default function Demo() { |
75 | 131 | return (
|
76 |
| - <div className="container"> |
77 |
| - <MonorepoTemplate> |
78 |
| - Example test |
79 |
| - </MonorepoTemplate> |
80 |
| - </div> |
| 132 | + <IFrame src="https://wangchujiang.com/" style={{ width: '100%', height: 320 }} /> |
81 | 133 | );
|
82 | 134 | }
|
83 | 135 | ```
|
| 136 | +## Props |
| 137 | + |
| 138 | +```ts |
| 139 | +export interface IFrameProps extends React.HTMLAttributes<HTMLIFrameElement> { |
| 140 | + head?: React.ReactNode; |
| 141 | + initialContent?: string; |
| 142 | + mountTarget?: string; |
| 143 | +} |
| 144 | +declare const IFrame: import("react").ForwardRefExoticComponent<IFrameProps & import("react").RefAttributes<HTMLIFrameElement>>; |
| 145 | +export default IFrame; |
| 146 | +``` |
| 147 | + |
| 148 | +## Development |
| 149 | + |
| 150 | +Runs the project in development mode. |
| 151 | + |
| 152 | +```bash |
| 153 | +# Step 1, run first, listen to the component compile and output the .js file |
| 154 | +# listen for compilation output type .d.ts file |
| 155 | +npm run watch |
| 156 | +# Step 2, development mode, listen to compile preview website instance |
| 157 | +npm run start |
| 158 | +``` |
| 159 | + |
| 160 | +Builds the app for production to the build folder. |
| 161 | + |
| 162 | +```bash |
| 163 | +npm run build |
| 164 | +``` |
| 165 | + |
| 166 | +The build is minified and the filenames include the hashes. |
| 167 | +Your app is ready to be deployed! |
| 168 | + |
84 | 169 |
|
85 | 170 | ## Contributors
|
86 | 171 |
|
87 | 172 | As always, thanks to our amazing contributors!
|
88 | 173 |
|
89 |
| -<a href="https://github.com/uiwjs/react-monorepo-template/graphs/contributors"> |
90 |
| - <img src="https://uiwjs.github.io/react-monorepo-template/CONTRIBUTORS.svg" /> |
| 174 | +<a href="https://github.com/uiwjs/react-iframe/graphs/contributors"> |
| 175 | + <img src="https://uiwjs.github.io/react-iframe/CONTRIBUTORS.svg" /> |
91 | 176 | </a>
|
92 | 177 |
|
93 |
| -Made with [github-action-contributors](https://github.com/jaywcjlove/github-action-contributors). |
| 178 | +Made with [contributors](https://github.com/jaywcjlove/github-action-contributors). |
94 | 179 |
|
95 | 180 | ## License
|
96 | 181 |
|
|
0 commit comments