Forked from OrganicPanda/hacky-scrollbar-resize-listener.js
Last active
July 22, 2019 09:28
-
-
Save AdamMcCormick/d5f718d2e9569acdf7def25e8266bb2a to your computer and use it in GitHub Desktop.
A sham that will throw a window resize event even when scrollbars are added/removed (this is not something the standard window resize event does). Tested in IE9+, Chrome & Firefox latest.
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
/** | |
* A drop-in component to listen for resizes or scroll-bar show/hide | |
* | |
* Based on https://gist.github.com/OrganicPanda/8222636 | |
*/ | |
import React from 'react' | |
var ScrollBarAdapter = React.createClass({ | |
onResize() { | |
if (this.props.onResize) { | |
this.props.onResize(); | |
return; | |
} | |
try { | |
var evt = new UIEvent('resize'); | |
window.dispatchEvent(evt); | |
} catch(e) {} | |
}, | |
componentDidMount() { | |
this.refs.frame.contentWindow.addEventListener('resize', this.onResize, false); | |
}, | |
componentWillUnmount() { | |
this.refs.frame.contentWindow.removeEventListener('resize', this.onResize); | |
}, | |
render(){ | |
var styles = { | |
height: 0, | |
margin: 0, | |
padding: 0, | |
overflow: "hidden", | |
borderWidth: 0, | |
position: "absolute", | |
backgroundColor: "transparent", | |
width: "100%" | |
}; | |
return ( | |
<iframe classNames="ScrollBarAdapter" ref="frame" style={styles} /> | |
); | |
} | |
}); | |
module.exports = ScrollBarAdapter; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Very nice! I've adapted this as a React hook https://gist.github.com/curran/0e30c621fe4fc612bf7feb0938a68e4d