unpkg/modules/client/main/WindowSize.js

36 lines
718 B
JavaScript
Raw Normal View History

2019-01-06 00:50:05 +00:00
import React from 'react';
import PropTypes from 'prop-types';
2019-01-06 00:50:05 +00:00
import { addEvent, removeEvent } from '../utils/dom';
2018-07-18 07:29:49 +00:00
2018-12-17 17:38:05 +00:00
const resizeEvent = 'resize';
2019-01-06 00:50:05 +00:00
export default class WindowSize extends React.Component {
handleWindowResize = () => {
2019-01-06 00:50:05 +00:00
if (this.props.onChange) {
this.props.onChange({
width: window.innerWidth,
height: window.innerHeight
2018-02-18 02:00:56 +00:00
});
2019-01-06 00:50:05 +00:00
}
2018-02-18 02:00:56 +00:00
};
componentDidMount() {
2018-07-18 07:29:49 +00:00
addEvent(window, resizeEvent, this.handleWindowResize);
}
componentWillUnmount() {
2018-07-18 07:29:49 +00:00
removeEvent(window, resizeEvent, this.handleWindowResize);
}
render() {
2018-02-18 02:00:56 +00:00
return null;
}
}
if (process.env.NODE_ENV !== 'production') {
WindowSize.propTypes = {
onChange: PropTypes.func
};
}