Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save AdamMcCormick/d5f718d2e9569acdf7def25e8266bb2a to your computer and use it in GitHub Desktop.
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.
/**
* 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;
@curran
Copy link

curran commented Jul 22, 2019

Very nice! I've adapted this as a React hook https://gist.github.com/curran/0e30c621fe4fc612bf7feb0938a68e4d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment