Skip to content

Commit 6329fda

Browse files
authored
Merge pull request #58 from deibeljc/master
Added getParentElement function that can be easily overridden to add functionality
2 parents 0dc2a24 + 1ec81cf commit 6329fda

File tree

3 files changed

+1797
-1107
lines changed

3 files changed

+1797
-1107
lines changed

dist/InfiniteScroll.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ var InfiniteScroll = (function(_Component) {
148148
value: function detachScrollListener() {
149149
var scrollEl = window;
150150
if (this.props.useWindow === false) {
151-
scrollEl = this.scrollComponent.parentNode;
151+
scrollEl = this.getParentElement(this.scrollComponent);
152152
}
153153

154154
scrollEl.removeEventListener(
@@ -163,16 +163,31 @@ var InfiniteScroll = (function(_Component) {
163163
);
164164
},
165165
},
166+
{
167+
key: 'getParentElement',
168+
value: function getParentElement(el) {
169+
return el.parentNode;
170+
},
171+
},
172+
{
173+
key: 'filterProps',
174+
value: function filterProps(props) {
175+
return props;
176+
},
177+
},
166178
{
167179
key: 'attachScrollListener',
168180
value: function attachScrollListener() {
169-
if (!this.props.hasMore) {
181+
if (
182+
!this.props.hasMore ||
183+
!this.getParentElement(this.scrollComponent)
184+
) {
170185
return;
171186
}
172187

173188
var scrollEl = window;
174189
if (this.props.useWindow === false) {
175-
scrollEl = this.scrollComponent.parentNode;
190+
scrollEl = this.getParentElement(this.scrollComponent);
176191
}
177192

178193
scrollEl.addEventListener(
@@ -211,6 +226,7 @@ var InfiniteScroll = (function(_Component) {
211226
value: function scrollListener() {
212227
var el = this.scrollComponent;
213228
var scrollEl = window;
229+
var parentNode = this.getParentElement(el);
214230

215231
var offset = void 0;
216232
if (this.props.useWindow) {
@@ -230,15 +246,14 @@ var InfiniteScroll = (function(_Component) {
230246
(el.offsetHeight - scrollTop - window.innerHeight);
231247
}
232248
} else if (this.props.isReverse) {
233-
offset = el.parentNode.scrollTop;
249+
offset = parentNode.scrollTop;
234250
} else {
235251
offset =
236-
el.scrollHeight -
237-
el.parentNode.scrollTop -
238-
el.parentNode.clientHeight;
252+
el.scrollHeight - parentNode.scrollTop - parentNode.clientHeight;
239253
}
240254

241-
if (offset < Number(this.props.threshold)) {
255+
// Here we make sure the element is visible as well as checking the offset
256+
if (offset < Number(this.props.threshold) && el.offsetParent !== null) {
242257
this.detachScrollListener();
243258
// Call loadMore after detachScrollListener to allow for non-async loadMore functions
244259
if (typeof this.props.loadMore === 'function') {
@@ -261,20 +276,21 @@ var InfiniteScroll = (function(_Component) {
261276
value: function render() {
262277
var _this2 = this;
263278

264-
var _props = this.props,
265-
children = _props.children,
266-
element = _props.element,
267-
hasMore = _props.hasMore,
268-
initialLoad = _props.initialLoad,
269-
isReverse = _props.isReverse,
270-
loader = _props.loader,
271-
loadMore = _props.loadMore,
272-
pageStart = _props.pageStart,
273-
ref = _props.ref,
274-
threshold = _props.threshold,
275-
useCapture = _props.useCapture,
276-
useWindow = _props.useWindow,
277-
props = _objectWithoutProperties(_props, [
279+
var renderProps = this.filterProps(this.props);
280+
281+
var children = renderProps.children,
282+
element = renderProps.element,
283+
hasMore = renderProps.hasMore,
284+
initialLoad = renderProps.initialLoad,
285+
isReverse = renderProps.isReverse,
286+
loader = renderProps.loader,
287+
loadMore = renderProps.loadMore,
288+
pageStart = renderProps.pageStart,
289+
ref = renderProps.ref,
290+
threshold = renderProps.threshold,
291+
useCapture = renderProps.useCapture,
292+
useWindow = renderProps.useWindow,
293+
props = _objectWithoutProperties(renderProps, [
278294
'children',
279295
'element',
280296
'hasMore',

src/InfiniteScroll.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default class InfiniteScroll extends Component {
7171
detachScrollListener() {
7272
let scrollEl = window;
7373
if (this.props.useWindow === false) {
74-
scrollEl = this.scrollComponent.parentNode;
74+
scrollEl = this.getParentElement(this.scrollComponent);
7575
}
7676

7777
scrollEl.removeEventListener(
@@ -86,14 +86,22 @@ export default class InfiniteScroll extends Component {
8686
);
8787
}
8888

89+
getParentElement(el) {
90+
return el.parentNode;
91+
}
92+
93+
filterProps(props) {
94+
return props;
95+
}
96+
8997
attachScrollListener() {
90-
if (!this.props.hasMore) {
98+
if (!this.props.hasMore || !this.getParentElement(this.scrollComponent)) {
9199
return;
92100
}
93101

94102
let scrollEl = window;
95103
if (this.props.useWindow === false) {
96-
scrollEl = this.scrollComponent.parentNode;
104+
scrollEl = this.getParentElement(this.scrollComponent);
97105
}
98106

99107
scrollEl.addEventListener(
@@ -128,6 +136,7 @@ export default class InfiniteScroll extends Component {
128136
scrollListener() {
129137
const el = this.scrollComponent;
130138
const scrollEl = window;
139+
const parentNode = this.getParentElement(el);
131140

132141
let offset;
133142
if (this.props.useWindow) {
@@ -145,13 +154,13 @@ export default class InfiniteScroll extends Component {
145154
(el.offsetHeight - scrollTop - window.innerHeight);
146155
}
147156
} else if (this.props.isReverse) {
148-
offset = el.parentNode.scrollTop;
157+
offset = parentNode.scrollTop;
149158
} else {
150-
offset =
151-
el.scrollHeight - el.parentNode.scrollTop - el.parentNode.clientHeight;
159+
offset = el.scrollHeight - parentNode.scrollTop - parentNode.clientHeight;
152160
}
153161

154-
if (offset < Number(this.props.threshold)) {
162+
// Here we make sure the element is visible as well as checking the offset
163+
if (offset < Number(this.props.threshold) && el.offsetParent !== null) {
155164
this.detachScrollListener();
156165
// Call loadMore after detachScrollListener to allow for non-async loadMore functions
157166
if (typeof this.props.loadMore === 'function') {
@@ -168,6 +177,7 @@ export default class InfiniteScroll extends Component {
168177
}
169178

170179
render() {
180+
const renderProps = this.filterProps(this.props);
171181
const {
172182
children,
173183
element,
@@ -182,7 +192,7 @@ export default class InfiniteScroll extends Component {
182192
useCapture,
183193
useWindow,
184194
...props
185-
} = this.props;
195+
} = renderProps;
186196

187197
props.ref = node => {
188198
this.scrollComponent = node;

0 commit comments

Comments
 (0)