1
- import React , { Component , ReactNode , CSSProperties } from " react" ;
2
- import { throttle } from " throttle-debounce" ;
3
- import { ThresholdUnits , parseThreshold } from " ./utils/threshold" ;
1
+ import React , { Component , ReactNode , CSSProperties } from ' react' ;
2
+ import { throttle } from ' throttle-debounce' ;
3
+ import { ThresholdUnits , parseThreshold } from ' ./utils/threshold' ;
4
4
5
5
type Fn = ( ) => any ;
6
6
interface Props {
@@ -37,7 +37,7 @@ export default class InfiniteScroll extends Component<Props, State> {
37
37
38
38
this . state = {
39
39
showLoader : false ,
40
- pullToRefreshThresholdBreached : false
40
+ pullToRefreshThresholdBreached : false ,
41
41
} ;
42
42
43
43
this . onScrollListener = this . onScrollListener . bind ( this ) ;
@@ -59,13 +59,13 @@ export default class InfiniteScroll extends Component<Props, State> {
59
59
private _pullDown : HTMLDivElement | undefined ;
60
60
61
61
// variables to keep track of pull down behaviour
62
- private startY : number = 0 ;
63
- private currentY : number = 0 ;
64
- private dragging : boolean = false ;
62
+ private startY = 0 ;
63
+ private currentY = 0 ;
64
+ private dragging = false ;
65
65
66
66
// will be populated in componentDidMount
67
67
// based on the height of the pull down element
68
- private maxPullDownDistance : number = 0 ;
68
+ private maxPullDownDistance = 0 ;
69
69
70
70
componentDidMount ( ) {
71
71
this . _scrollableNode = this . getScrollableTarget ( ) ;
@@ -74,13 +74,13 @@ export default class InfiniteScroll extends Component<Props, State> {
74
74
: this . _scrollableNode || window ;
75
75
76
76
if ( this . el ) {
77
- this . el . addEventListener ( " scroll" , e =>
77
+ this . el . addEventListener ( ' scroll' , e =>
78
78
this . throttledOnScrollListener ( e as MouseEvent )
79
79
) ;
80
80
}
81
81
82
82
if (
83
- typeof this . props . initialScrollY === " number" &&
83
+ typeof this . props . initialScrollY === ' number' &&
84
84
this . el &&
85
85
this . el instanceof HTMLElement &&
86
86
this . el . scrollHeight > this . props . initialScrollY
@@ -89,13 +89,13 @@ export default class InfiniteScroll extends Component<Props, State> {
89
89
}
90
90
91
91
if ( this . props . pullDownToRefresh && this . el ) {
92
- this . el . addEventListener ( " touchstart" , this . onStart ) ;
93
- this . el . addEventListener ( " touchmove" , this . onMove ) ;
94
- this . el . addEventListener ( " touchend" , this . onEnd ) ;
92
+ this . el . addEventListener ( ' touchstart' , this . onStart ) ;
93
+ this . el . addEventListener ( ' touchmove' , this . onMove ) ;
94
+ this . el . addEventListener ( ' touchend' , this . onEnd ) ;
95
95
96
- this . el . addEventListener ( " mousedown" , this . onStart ) ;
97
- this . el . addEventListener ( " mousemove" , this . onMove ) ;
98
- this . el . addEventListener ( " mouseup" , this . onEnd ) ;
96
+ this . el . addEventListener ( ' mousedown' , this . onStart ) ;
97
+ this . el . addEventListener ( ' mousemove' , this . onMove ) ;
98
+ this . el . addEventListener ( ' mouseup' , this . onEnd ) ;
99
99
100
100
// get BCR of pullDown element to position it above
101
101
this . maxPullDownDistance =
@@ -106,7 +106,7 @@ export default class InfiniteScroll extends Component<Props, State> {
106
106
0 ;
107
107
this . forceUpdate ( ) ;
108
108
109
- if ( typeof this . props . refreshFunction !== " function" ) {
109
+ if ( typeof this . props . refreshFunction !== ' function' ) {
110
110
throw new Error (
111
111
`Mandatory prop "refreshFunction" missing.
112
112
Pull Down To Refresh functionality will not work
@@ -118,18 +118,18 @@ export default class InfiniteScroll extends Component<Props, State> {
118
118
119
119
componentWillUnmount ( ) {
120
120
if ( this . el ) {
121
- this . el . removeEventListener ( " scroll" , e =>
121
+ this . el . removeEventListener ( ' scroll' , e =>
122
122
this . throttledOnScrollListener ( e as MouseEvent )
123
123
) ;
124
124
125
125
if ( this . props . pullDownToRefresh ) {
126
- this . el . removeEventListener ( " touchstart" , this . onStart ) ;
127
- this . el . removeEventListener ( " touchmove" , this . onMove ) ;
128
- this . el . removeEventListener ( " touchend" , this . onEnd ) ;
126
+ this . el . removeEventListener ( ' touchstart' , this . onStart ) ;
127
+ this . el . removeEventListener ( ' touchmove' , this . onMove ) ;
128
+ this . el . removeEventListener ( ' touchend' , this . onEnd ) ;
129
129
130
- this . el . removeEventListener ( " mousedown" , this . onStart ) ;
131
- this . el . removeEventListener ( " mousemove" , this . onMove ) ;
132
- this . el . removeEventListener ( " mouseup" , this . onEnd ) ;
130
+ this . el . removeEventListener ( ' mousedown' , this . onStart ) ;
131
+ this . el . removeEventListener ( ' mousemove' , this . onMove ) ;
132
+ this . el . removeEventListener ( ' mouseup' , this . onEnd ) ;
133
133
}
134
134
}
135
135
}
@@ -146,14 +146,14 @@ export default class InfiniteScroll extends Component<Props, State> {
146
146
// update state when new data was sent in
147
147
this . setState ( {
148
148
showLoader : false ,
149
- pullToRefreshThresholdBreached : false
149
+ pullToRefreshThresholdBreached : false ,
150
150
} ) ;
151
151
}
152
152
153
153
getScrollableTarget ( ) {
154
154
if ( this . props . scrollableTarget instanceof HTMLElement )
155
155
return this . props . scrollableTarget ;
156
- if ( typeof this . props . scrollableTarget === " string" ) {
156
+ if ( typeof this . props . scrollableTarget === ' string' ) {
157
157
return document . getElementById ( this . props . scrollableTarget ) ;
158
158
}
159
159
if ( this . props . scrollableTarget === null ) {
@@ -178,7 +178,7 @@ export default class InfiniteScroll extends Component<Props, State> {
178
178
this . currentY = this . startY ;
179
179
180
180
if ( this . _infScroll ) {
181
- this . _infScroll . style . willChange = " transform" ;
181
+ this . _infScroll . style . willChange = ' transform' ;
182
182
this . _infScroll . style . transition = `transform 0.2s cubic-bezier(0,0,0.31,1)` ;
183
183
}
184
184
} ;
@@ -200,15 +200,15 @@ export default class InfiniteScroll extends Component<Props, State> {
200
200
Number ( this . props . pullDownToRefreshThreshold )
201
201
) {
202
202
this . setState ( {
203
- pullToRefreshThresholdBreached : true
203
+ pullToRefreshThresholdBreached : true ,
204
204
} ) ;
205
205
}
206
206
207
207
// so you can drag upto 1.5 times of the maxPullDownDistance
208
208
if ( this . currentY - this . startY > this . maxPullDownDistance * 1.5 ) return ;
209
209
210
210
if ( this . _infScroll ) {
211
- this . _infScroll . style . overflow = " visible" ;
211
+ this . _infScroll . style . overflow = ' visible' ;
212
212
this . _infScroll . style . transform = `translate3d(0px, ${ this . currentY -
213
213
this . startY } px, 0px)`;
214
214
}
@@ -227,9 +227,9 @@ export default class InfiniteScroll extends Component<Props, State> {
227
227
requestAnimationFrame ( ( ) => {
228
228
// this._infScroll
229
229
if ( this . _infScroll ) {
230
- this . _infScroll . style . overflow = " auto" ;
231
- this . _infScroll . style . transform = " none" ;
232
- this . _infScroll . style . willChange = " none" ;
230
+ this . _infScroll . style . overflow = ' auto' ;
231
+ this . _infScroll . style . transform = ' none' ;
232
+ this . _infScroll . style . willChange = ' none' ;
233
233
}
234
234
} ) ;
235
235
} ;
@@ -258,13 +258,13 @@ export default class InfiniteScroll extends Component<Props, State> {
258
258
}
259
259
260
260
onScrollListener ( event : MouseEvent ) {
261
- if ( typeof this . props . onScroll === " function" ) {
261
+ if ( typeof this . props . onScroll === ' function' ) {
262
262
// Execute this callback in next tick so that it does not affect the
263
263
// functionality of the library.
264
264
setTimeout ( ( ) => this . props . onScroll && this . props . onScroll ( event ) , 0 ) ;
265
265
}
266
266
267
- let target =
267
+ const target =
268
268
this . props . height || this . _scrollableNode
269
269
? ( event . target as HTMLElement )
270
270
: document . documentElement . scrollTop
@@ -275,7 +275,7 @@ export default class InfiniteScroll extends Component<Props, State> {
275
275
// prevents multiple triggers.
276
276
if ( this . actionTriggered ) return ;
277
277
278
- let atBottom = this . isElementAtBottom ( target , this . props . scrollThreshold ) ;
278
+ const atBottom = this . isElementAtBottom ( target , this . props . scrollThreshold ) ;
279
279
280
280
// call the `next` function in the props to trigger the next data fetch
281
281
if ( atBottom && this . props . hasMore ) {
@@ -289,10 +289,10 @@ export default class InfiniteScroll extends Component<Props, State> {
289
289
290
290
render ( ) {
291
291
const style = {
292
- height : this . props . height || " auto" ,
293
- overflow : " auto" ,
294
- WebkitOverflowScrolling : " touch" ,
295
- ...this . props . style
292
+ height : this . props . height || ' auto' ,
293
+ overflow : ' auto' ,
294
+ WebkitOverflowScrolling : ' touch' ,
295
+ ...this . props . style ,
296
296
} as CSSProperties ;
297
297
const hasChildren =
298
298
this . props . hasChildren ||
@@ -306,26 +306,26 @@ export default class InfiniteScroll extends Component<Props, State> {
306
306
// on drag down as overflow becomes visible
307
307
const outerDivStyle =
308
308
this . props . pullDownToRefresh && this . props . height
309
- ? { overflow : " auto" }
309
+ ? { overflow : ' auto' }
310
310
: { } ;
311
311
return (
312
312
< div style = { outerDivStyle } >
313
313
< div
314
- className = { `infinite-scroll-component ${ this . props . className || "" } ` }
314
+ className = { `infinite-scroll-component ${ this . props . className || '' } ` }
315
315
ref = { ( infScroll : HTMLDivElement ) => ( this . _infScroll = infScroll ) }
316
316
style = { style }
317
317
>
318
318
{ this . props . pullDownToRefresh && (
319
319
< div
320
- style = { { position : " relative" } }
320
+ style = { { position : ' relative' } }
321
321
ref = { ( pullDown : HTMLDivElement ) => ( this . _pullDown = pullDown ) }
322
322
>
323
323
< div
324
324
style = { {
325
- position : " absolute" ,
325
+ position : ' absolute' ,
326
326
left : 0 ,
327
327
right : 0 ,
328
- top : - 1 * this . maxPullDownDistance
328
+ top : - 1 * this . maxPullDownDistance ,
329
329
} }
330
330
>
331
331
{ this . state . pullToRefreshThresholdBreached
0 commit comments