Skip to content

Instantly share code, notes, and snippets.

@gribnoysup
Created January 16, 2017 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gribnoysup/75f4215b5a6f3bd95d307239a88297dd to your computer and use it in GitHub Desktop.
Save gribnoysup/75f4215b5a6f3bd95d307239a88297dd to your computer and use it in GitHub Desktop.
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