Created
January 16, 2017 10:26
-
-
Save gribnoysup/75f4215b5a6f3bd95d307239a88297dd to your computer and use it in GitHub Desktop.
This file contains 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
import React, {Component} from 'react' | |
export default class ResizeListener extends Component { | |
static defaultProps = { | |
onResize: Function.prototype, | |
getDOMNode: Function.prototype | |
} | |
constructor(...args) { | |
super(...args) | |
this.isPositionChanged = false | |
this.handleResize = this.handleResize.bind(this) | |
} | |
handleResize(event) { | |
this.props.onResize(event) | |
} | |
componentDidMount() { | |
if (this.iframeNode !== null) { | |
const parent = this.iframeNode.parentNode | |
if (window.getComputedStyle(parent).position !== 'relative') { | |
parent.style.position = 'relative' | |
this.isPositionChanged = true | |
} | |
this.iframeNode.contentWindow.addEventListener('resize', this.handleResize) | |
} | |
} | |
componentWillUnmount() { | |
if (this.isPositionChanged && this.iframeNode) { | |
const parent = this.iframeNode.parentNode | |
parent.style.position = '' | |
} | |
this.iframeNode.contentWindow.removeEventListener('resize', this.handleResize) | |
} | |
render() { | |
return ( | |
<iframe | |
ref={(ref) => { | |
this.props.getDOMNode(ref) | |
this.iframeNode = ref | |
}} | |
style={{ | |
position: 'absolute', | |
top: 0, | |
left: 0, | |
width: '100%', | |
height: '100%', | |
marginLeft: '-99999px', | |
visibility: 'hidden', | |
pointerEvents: 'none', | |
border: 'none' | |
}} | |
/> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment