Skip to content

Commit

Permalink
changed child height calculation to include element margin. no more j…
Browse files Browse the repository at this point in the history
…itter
  • Loading branch information
tehandyb committed May 28, 2015
1 parent 1233289 commit 6a489d9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 6 deletions.
11 changes: 8 additions & 3 deletions dist/LazyRender.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";

var React = require('react/addons');
var elementSize = require("element-size");
var cloneWithProps = React.addons.cloneWithProps;

var LazyRender = React.createClass({displayName: 'LazyRender',
var LazyRender = React.createClass({displayName: "LazyRender",
propTypes: {
children: React.PropTypes.array.isRequired,
maxHeight: React.PropTypes.number.isRequired,
Expand Down Expand Up @@ -55,6 +56,11 @@ var LazyRender = React.createClass({displayName: 'LazyRender',
}
},

getElementHeight: function(element) {
var marginTop = parseInt(window.getComputedStyle(element).marginTop);
return elementSize(element)[1] - marginTop; //remove one margin since the margins are shared by adjacent elements
},

componentWillReceiveProps: function(nextProps) {
var childrenTop = Math.floor(this.state.scrollTop / this.state.childHeight);
var childrenBottom = (nextProps.children.length - childrenTop -
Expand Down Expand Up @@ -87,8 +93,7 @@ var LazyRender = React.createClass({displayName: 'LazyRender',
componentDidMount: function() {
var firstChild = this.refs['child-0'];
var el = firstChild.getDOMNode();
var childHeight = (el.style.height ? el.style.height.replace('px', '') :
null) || el.clientHeight;
var childHeight = this.getElementHeight(el);

var height = this.getHeight(
this.props.children.length,
Expand Down
7 changes: 7 additions & 0 deletions example/css/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,10 @@ p.info {
margin-right: 10px;
}
}

.list-item {
height: 20px;
padding: 5px 10px;
background-color: #E3F3FB;
margin: 5px 0px;
}
2 changes: 1 addition & 1 deletion example/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var Index = React.createClass({
render: function() {
var rows = _.range(this.state.limit).map(function(row) {
var index = row % 50;
return <div style={{ height: 20, padding: "5px 10px" }}>
return <div className="list-item">
#{row + 1} {NAMES[index]}
</div>;
});
Expand Down
9 changes: 7 additions & 2 deletions src/LazyRender.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

var React = require('react/addons');
var elementSize = require("element-size");
var cloneWithProps = React.addons.cloneWithProps;

var LazyRender = React.createClass({
Expand Down Expand Up @@ -55,6 +56,11 @@ var LazyRender = React.createClass({
}
},

getElementHeight: function(element) {
var marginTop = parseInt(window.getComputedStyle(element).marginTop);
return elementSize(element)[1] - marginTop; //remove one margin since the margins are shared by adjacent elements
},

componentWillReceiveProps: function(nextProps) {
var childrenTop = Math.floor(this.state.scrollTop / this.state.childHeight);
var childrenBottom = (nextProps.children.length - childrenTop -
Expand Down Expand Up @@ -87,8 +93,7 @@ var LazyRender = React.createClass({
componentDidMount: function() {
var firstChild = this.refs['child-0'];
var el = firstChild.getDOMNode();
var childHeight = (el.style.height ? el.style.height.replace('px', '') :
null) || el.clientHeight;
var childHeight = this.getElementHeight(el);

var height = this.getHeight(
this.props.children.length,
Expand Down

0 comments on commit 6a489d9

Please sign in to comment.