diff --git a/README.md b/README.md index 502f648..b0b1fa7 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,80 @@ class Form extends Component { } ``` +### iframes & document context +In cases were you need to render the portal inside of an `iframe`, you need to provide +the prop `getDocument` which is a method that return the `document` as the context of the portal. + +Here is an example: + +```js + +const INITIAL_CONTENT = '
'; + +class Iframe extends React.Component { + node = null; + initialContentRendered = false; + + getDocument = () => this.node.contentDocument; + + componentDidMount() { + const doc = this.getDocument(); + if (doc && doc.readyState === 'complete') { + this.forceUpdate(); + } else { + this.node.addEventListener('load', this.handleLoad); + } + } + + componentWillUnmount() { + this.node.removeEventListener('load', this.handleLoad); + } + + handleLoad = () => { + this.forceUpdate(); + }; + + + renderContent() { + if (!this.isMounted()) { + return null; + } + + const doc = this.getDocument(); + + if (!this.initialContentRendered) { + doc.open('text/html', 'replace'); + doc.write(INITIAL_CONTENT); + doc.close(); + this.initialContentRendered = true; + } + + return ( +