Skip to content

Instantly share code, notes, and snippets.

@eliseumds
Created June 14, 2016 19:52
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 eliseumds/23f9df3430457541b5aa6af525dbf192 to your computer and use it in GitHub Desktop.
Save eliseumds/23f9df3430457541b5aa6af525dbf192 to your computer and use it in GitHub Desktop.
<ElementQuery
import React, { Component, PropTypes } from 'react';
import ReactDOM from 'react-dom';
import isNumber from 'lodash/isNumber';
import elementResizeDetector from 'element-resize-detector';
let erd;
if (__CLIENT__) {
erd = elementResizeDetector({
strategy: 'scroll'
});
}
export default class ElementQuery extends Component {
static propTypes = {
minWidth: PropTypes.number,
maxWidth: PropTypes.number,
minHeight: PropTypes.number,
maxHeight: PropTypes.number,
children: PropTypes.any,
// "values" are used for server-side rendering
values: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
})
};
componentDidMount() {
this.$parent = ReactDOM.findDOMNode(this).parentElement;
erd.listenTo(this.$parent, this.onResize.bind(this));
}
componentWillUnmount() {
erd.removeAllListeners(this.$parent);
erd.uninstall(this.$parent);
}
onResize() {
this.forceUpdate();
}
isInsideRange(value, min, max) {
return (isNumber(min) ? value >= min : true) &&
(isNumber(max) ? value <= max : true);
}
matchesRules() {
const { values, minWidth, maxWidth, minHeight, maxHeight } = this.props;
if (__SERVER__ && values) {
return this.isInsideRange(values.width, minWidth, maxWidth) &&
this.isInsideRange(values.height, minHeight, maxHeight);
}
// TODO: should we always show the elements by default on the server?
if (!this.$parent) {
return true;
}
const width = this.$parent.offsetWidth;
const height = this.$parent.offsetHeight;
return this.isInsideRange(width, minWidth, maxWidth) &&
this.isInsideRange(height, minHeight, maxHeight);
}
render() {
if (this.matchesRules()) {
return this.props.children;
}
return <span />;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment