-
Notifications
You must be signed in to change notification settings - Fork 320
/
index.js
279 lines (242 loc) · 8.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import React, { Component } from "react";
import PropTypes from "prop-types";
import throttle from './utils/throttle';
export default class InfiniteScroll extends Component {
constructor(props) {
super();
this.state = {
showLoader: false,
lastScrollTop: 0,
actionTriggered: false,
pullToRefreshThresholdBreached: false
};
// variables to keep track of pull down behaviour
this.startY = 0;
this.currentY = 0;
this.dragging = false;
// will be populated in componentDidMount
// based on the height of the pull down element
this.maxPullDownDistance = 0;
this.onScrollListener = this.onScrollListener.bind(this);
this.throttledOnScrollListener = throttle(this.onScrollListener, 150).bind(
this
);
this.onStart = this.onStart.bind(this);
this.onMove = this.onMove.bind(this);
this.onEnd = this.onEnd.bind(this);
this.getScrollableTarget = this.getScrollableTarget.bind(this);
}
componentDidMount() {
this._scrollableNode = this.getScrollableTarget();
this.el = this.props.height
? this._infScroll
: this._scrollableNode || window;
this.el.addEventListener("scroll", this.throttledOnScrollListener);
if (
typeof this.props.initialScrollY === "number" &&
this.el.scrollHeight > this.props.initialScrollY
) {
this.el.scrollTo(0, this.props.initialScrollY);
}
if (this.props.pullDownToRefresh) {
this.el.addEventListener("touchstart", this.onStart);
this.el.addEventListener("touchmove", this.onMove);
this.el.addEventListener("touchend", this.onEnd);
this.el.addEventListener("mousedown", this.onStart);
this.el.addEventListener("mousemove", this.onMove);
this.el.addEventListener("mouseup", this.onEnd);
// get BCR of pullDown element to position it above
this.maxPullDownDistance = this._pullDown.firstChild.getBoundingClientRect().height;
this.forceUpdate();
if (typeof this.props.refreshFunction !== "function") {
throw new Error(
`Mandatory prop "refreshFunction" missing.
Pull Down To Refresh functionality will not work
as expected. Check README.md for usage'`
);
}
}
}
componentWillUnmount() {
this.el.removeEventListener("scroll", this.throttledOnScrollListener);
if (this.props.pullDownToRefresh) {
this.el.removeEventListener("touchstart", this.onStart);
this.el.removeEventListener("touchmove", this.onMove);
this.el.removeEventListener("touchend", this.onEnd);
this.el.removeEventListener("mousedown", this.onStart);
this.el.removeEventListener("mousemove", this.onMove);
this.el.removeEventListener("mouseup", this.onEnd);
}
}
componentWillReceiveProps(props) {
// do nothing when dataLength is unchanged
if (this.props.dataLength === props.dataLength) return;
// update state when new data was sent in
this.setState({
showLoader: false,
actionTriggered: false,
pullToRefreshThresholdBreached: false
});
}
getScrollableTarget() {
if (this.props.scrollableTarget instanceof HTMLElement) return this.props.scrollableTarget;
if (typeof this.props.scrollableTarget === 'string') {
return document.getElementById(this.props.scrollableTarget);
}
if (this.props.scrollableTarget === null) {
console.warn(`You are trying to pass scrollableTarget but it is null. This might
happen because the element may not have been added to DOM yet.
See https://github.com/ankeetmaini/react-infinite-scroll-component/issues/59 for more info.
`);
}
return null;
}
onStart(evt) {
if (this.state.lastScrollTop) return;
this.dragging = true;
this.startY = evt.pageY || evt.touches[0].pageY;
this.currentY = this.startY;
this._infScroll.style.willChange = "transform";
this._infScroll.style.transition = `transform 0.2s cubic-bezier(0,0,0.31,1)`;
}
onMove(evt) {
if (!this.dragging) return;
this.currentY = evt.pageY || evt.touches[0].pageY;
// user is scrolling down to up
if (this.currentY < this.startY) return;
if (this.currentY - this.startY >= this.props.pullDownToRefreshThreshold) {
this.setState({
pullToRefreshThresholdBreached: true
});
}
// so you can drag upto 1.5 times of the maxPullDownDistance
if (this.currentY - this.startY > this.maxPullDownDistance * 1.5) return;
this._infScroll.style.overflow = "visible";
this._infScroll.style.transform = `translate3d(0px, ${this.currentY -
this.startY}px, 0px)`;
}
onEnd(evt) {
this.startY = 0;
this.currentY = 0;
this.dragging = false;
if (this.state.pullToRefreshThresholdBreached) {
this.props.refreshFunction && this.props.refreshFunction();
}
requestAnimationFrame(() => {
this._infScroll.style.overflow = "auto";
this._infScroll.style.transform = "none";
this._infScroll.style.willChange = "none";
});
}
isElementAtBottom(target, scrollThreshold = 0.8) {
const clientHeight =
target === document.body || target === document.documentElement
? window.screen.availHeight
: target.clientHeight;
return (
target.scrollTop + clientHeight >= scrollThreshold * target.scrollHeight
);
}
onScrollListener(event) {
if (typeof this.props.onScroll === "function") {
// Execute this callback in next tick so that it does not affect the
// functionality of the library.
setTimeout(() => this.props.onScroll(event), 0);
}
let target =
this.props.height || this._scrollableNode
? event.target
: document.documentElement.scrollTop
? document.documentElement
: document.body;
// return immediately if the action has already been triggered,
// prevents multiple triggers.
if (this.state.actionTriggered) return;
let atBottom = this.isElementAtBottom(target, this.props.scrollThreshold);
// call the `next` function in the props to trigger the next data fetch
if (atBottom && this.props.hasMore) {
this.setState({ actionTriggered: true, showLoader: true });
this.props.next();
}
this.setState({ lastScrollTop: target.scrollTop });
}
render() {
const style = {
height: this.props.height || "auto",
overflow: "auto",
WebkitOverflowScrolling: "touch",
...this.props.style
};
const hasChildren =
this.props.hasChildren ||
!!(this.props.children && this.props.children.length);
// because heighted infiniteScroll visualy breaks
// on drag down as overflow becomes visible
const outerDivStyle =
this.props.pullDownToRefresh && this.props.height
? { overflow: "auto" }
: {};
return (
<div style={outerDivStyle}>
<div
className="infinite-scroll-component"
ref={infScroll => (this._infScroll = infScroll)}
style={style}
>
{this.props.pullDownToRefresh && (
<div
style={{ position: "relative" }}
ref={pullDown => (this._pullDown = pullDown)}
>
<div
style={{
position: "absolute",
left: 0,
right: 0,
top: -1 * this.maxPullDownDistance
}}
>
{!this.state.pullToRefreshThresholdBreached &&
this.props.pullDownToRefreshContent}
{this.state.pullToRefreshThresholdBreached &&
this.props.releaseToRefreshContent}
</div>
</div>
)}
{this.props.children}
{!this.state.showLoader &&
!hasChildren &&
this.props.hasMore &&
this.props.loader}
{this.state.showLoader && this.props.hasMore && this.props.loader}
{!this.props.hasMore && this.props.endMessage}
</div>
</div>
);
}
}
InfiniteScroll.defaultProps = {
pullDownToRefreshContent: <h3>Pull down to refresh</h3>,
releaseToRefreshContent: <h3>Release to refresh</h3>,
pullDownToRefreshThreshold: 100,
disableBrowserPullToRefresh: true
};
InfiniteScroll.propTypes = {
next: PropTypes.func,
hasMore: PropTypes.bool,
children: PropTypes.node,
loader: PropTypes.node.isRequired,
scrollThreshold: PropTypes.number,
endMessage: PropTypes.node,
style: PropTypes.object,
height: PropTypes.number,
scrollableTarget: PropTypes.node,
hasChildren: PropTypes.bool,
pullDownToRefresh: PropTypes.bool,
pullDownToRefreshContent: PropTypes.node,
releaseToRefreshContent: PropTypes.node,
pullDownToRefreshThreshold: PropTypes.number,
refreshFunction: PropTypes.func,
onScroll: PropTypes.func,
dataLength: PropTypes.number.isRequired
};