Created
October 27, 2017 19:38
-
-
Save stevenkaspar/be4f8121b670f9701f5de5286ab5ac7c to your computer and use it in GitHub Desktop.
React Iframe for Writing to contentDocument
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This has a specific use case for writing to the contentDocument allowing for a separated scope | |
* | |
* There are probably other ways to do this with ShadowDom but this works well for my purposes | |
* | |
* USAGE: | |
* | |
* <CustomIframe html={<head><title>Doc Title</title></head><body>Hello React User</body>} /> | |
* | |
*/ | |
import React, { Component } from 'react' | |
export default class CustomIframe extends Component { | |
constructor() { | |
super() | |
this.iframe_ref = null | |
this.writeHTML = this.writeHTML.bind(this) | |
} | |
writeHTML(frame){ | |
if(!frame) { | |
return | |
} | |
this.iframe_ref = frame | |
let doc = frame.contentDocument | |
doc.open() | |
doc.write(this.props.html) | |
doc.close() | |
frame.style.width = '100%' | |
frame.style.height = `${frame.contentWindow.document.body.scrollHeight}px` | |
} | |
render(){ | |
return ( | |
<iframe src='about:blank' scrolling='no' frameBorder='0' ref={this.writeHTML}></iframe> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not working