Skip to content

Commit

Permalink
Merge pull request #1 from tehandyb/master
Browse files Browse the repository at this point in the history
Smoothed out the jitter by including full margin in height calculations
  • Loading branch information
jackcompton committed May 29, 2015
2 parents 390cdd4 + 6a489d9 commit dfb2841
Show file tree
Hide file tree
Showing 5 changed files with 25 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
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
},
"license": "Apache 2.0",
"dependencies": {
"element-size": "^1.1.1",
"lodash": "^3.9.3",
"react": "^0.12.0"
},
"devDependencies": {
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 dfb2841

Please sign in to comment.