Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
cheton authored Sep 5, 2017
1 parent 580461a commit b87cfd1
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,44 @@ Demo: https://trendmicro-frontend.github.io/react-iframe

## Usage

### Fixed width and height

```js
<Iframe src="./index.html" />
<Iframe src="index.html" width="100%" height={240} />
```

### Resize iframe to fit content (same domain only)

If you want to avoid polling, use [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) and [resize-event](https://github.com/shama/resize-event) to detect the size of the iframe on content changes.

```js
<Iframe
ref={node => {
if (this.resizableIframeTimer) {
clearInterval(this.resizableIframeTimer);
this.resizableIframeTimer = null;
}

if (!node) {
return;
}

const iframe = ReactDOM.findDOMnode(node);
if (iframe && iframe.contentWindow) {
let prevHeight = 0;
this.resizableIframeTimer = setInterval(() => {
const nextHeight = iframe.contentWindow.document.body.offsetHeight;
if (prevHeight !== nextHeight) {
iframe.style.height = nextHeight + 'px';
prevHeight = nextHeight;
}
}, 200);
}
}}
src="index.html"
width="100%"
/>
```

## API

Expand Down

0 comments on commit b87cfd1

Please sign in to comment.