@@ -36,6 +36,25 @@ export default class Scrollbars extends Component {
36
36
constructor ( props , ...rest ) {
37
37
super ( props , ...rest ) ;
38
38
39
+ this . getScrollLeft = this . getScrollLeft . bind ( this ) ;
40
+ this . getScrollTop = this . getScrollTop . bind ( this ) ;
41
+ this . getScrollWidth = this . getScrollWidth . bind ( this ) ;
42
+ this . getScrollHeight = this . getScrollHeight . bind ( this ) ;
43
+ this . getClientWidth = this . getClientWidth . bind ( this ) ;
44
+ this . getClientHeight = this . getClientHeight . bind ( this ) ;
45
+ this . getValues = this . getValues . bind ( this ) ;
46
+ this . getThumbHorizontalWidth = this . getThumbHorizontalWidth . bind ( this ) ;
47
+ this . getThumbVerticalHeight = this . getThumbVerticalHeight . bind ( this ) ;
48
+ this . getScrollLeftForOffset = this . getScrollLeftForOffset . bind ( this ) ;
49
+ this . getScrollTopForOffset = this . getScrollTopForOffset . bind ( this ) ;
50
+
51
+ this . scrollLeft = this . scrollLeft . bind ( this ) ;
52
+ this . scrollTop = this . scrollTop . bind ( this ) ;
53
+ this . scrollToLeft = this . scrollToLeft . bind ( this ) ;
54
+ this . scrollToTop = this . scrollToTop . bind ( this ) ;
55
+ this . scrollToRight = this . scrollToRight . bind ( this ) ;
56
+ this . scrollToBottom = this . scrollToBottom . bind ( this ) ;
57
+
39
58
this . handleTrackMouseEnter = this . handleTrackMouseEnter . bind ( this ) ;
40
59
this . handleTrackMouseLeave = this . handleTrackMouseLeave . bind ( this ) ;
41
60
this . handleHorizontalTrackMouseDown = this . handleHorizontalTrackMouseDown . bind ( this ) ;
@@ -76,45 +95,38 @@ export default class Scrollbars extends Component {
76
95
}
77
96
78
97
getScrollLeft ( ) {
79
- const { view } = this . refs ;
80
- return view . scrollLeft ;
98
+ return this . view . scrollLeft ;
81
99
}
82
100
83
101
getScrollTop ( ) {
84
- const { view } = this . refs ;
85
- return view . scrollTop ;
102
+ return this . view . scrollTop ;
86
103
}
87
104
88
105
getScrollWidth ( ) {
89
- const { view } = this . refs ;
90
- return view . scrollWidth ;
106
+ return this . view . scrollWidth ;
91
107
}
92
108
93
109
getScrollHeight ( ) {
94
- const { view } = this . refs ;
95
- return view . scrollHeight ;
110
+ return this . view . scrollHeight ;
96
111
}
97
112
98
113
getClientWidth ( ) {
99
- const { view } = this . refs ;
100
- return view . clientWidth ;
114
+ return this . view . clientWidth ;
101
115
}
102
116
103
117
getClientHeight ( ) {
104
- const { view } = this . refs ;
105
- return view . clientHeight ;
118
+ return this . view . clientHeight ;
106
119
}
107
120
108
121
getValues ( ) {
109
- const { view } = this . refs ;
110
122
const {
111
123
scrollLeft,
112
124
scrollTop,
113
125
scrollWidth,
114
126
scrollHeight,
115
127
clientWidth,
116
128
clientHeight
117
- } = view ;
129
+ } = this . view ;
118
130
119
131
return {
120
132
left : ( scrollLeft / ( scrollWidth - clientWidth ) ) || 0 ,
@@ -130,9 +142,8 @@ export default class Scrollbars extends Component {
130
142
131
143
getThumbHorizontalWidth ( ) {
132
144
const { thumbSize, thumbMinSize } = this . props ;
133
- const { view, trackHorizontal } = this . refs ;
134
- const { scrollWidth, clientWidth } = view ;
135
- const trackWidth = getInnerWidth ( trackHorizontal ) ;
145
+ const { scrollWidth, clientWidth } = this . view ;
146
+ const trackWidth = getInnerWidth ( this . trackHorizontal ) ;
136
147
const width = Math . ceil ( clientWidth / scrollWidth * trackWidth ) ;
137
148
if ( trackWidth === width ) return 0 ;
138
149
if ( thumbSize ) return thumbSize ;
@@ -141,65 +152,56 @@ export default class Scrollbars extends Component {
141
152
142
153
getThumbVerticalHeight ( ) {
143
154
const { thumbSize, thumbMinSize } = this . props ;
144
- const { view, trackVertical } = this . refs ;
145
- const { scrollHeight, clientHeight } = view ;
146
- const trackHeight = getInnerHeight ( trackVertical ) ;
155
+ const { scrollHeight, clientHeight } = this . view ;
156
+ const trackHeight = getInnerHeight ( this . trackVertical ) ;
147
157
const height = Math . ceil ( clientHeight / scrollHeight * trackHeight ) ;
148
158
if ( trackHeight === height ) return 0 ;
149
159
if ( thumbSize ) return thumbSize ;
150
160
return Math . max ( height , thumbMinSize ) ;
151
161
}
152
162
153
163
getScrollLeftForOffset ( offset ) {
154
- const { view, trackHorizontal } = this . refs ;
155
- const { scrollWidth, clientWidth } = view ;
156
- const trackWidth = getInnerWidth ( trackHorizontal ) ;
164
+ const { scrollWidth, clientWidth } = this . view ;
165
+ const trackWidth = getInnerWidth ( this . trackHorizontal ) ;
157
166
const thumbWidth = this . getThumbHorizontalWidth ( ) ;
158
167
return offset / ( trackWidth - thumbWidth ) * ( scrollWidth - clientWidth ) ;
159
168
}
160
169
161
170
getScrollTopForOffset ( offset ) {
162
- const { view, trackVertical } = this . refs ;
163
- const { scrollHeight, clientHeight } = view ;
164
- const trackHeight = getInnerHeight ( trackVertical ) ;
171
+ const { scrollHeight, clientHeight } = this . view ;
172
+ const trackHeight = getInnerHeight ( this . trackVertical ) ;
165
173
const thumbHeight = this . getThumbVerticalHeight ( ) ;
166
174
return offset / ( trackHeight - thumbHeight ) * ( scrollHeight - clientHeight ) ;
167
175
}
168
176
169
177
scrollLeft ( left = 0 ) {
170
- const { view } = this . refs ;
171
- view . scrollLeft = left ;
178
+ this . view . scrollLeft = left ;
172
179
}
173
180
174
181
scrollTop ( top = 0 ) {
175
- const { view } = this . refs ;
176
- view . scrollTop = top ;
182
+ this . view . scrollTop = top ;
177
183
}
178
184
179
185
scrollToLeft ( ) {
180
- const { view } = this . refs ;
181
- view . scrollLeft = 0 ;
186
+ this . view . scrollLeft = 0 ;
182
187
}
183
188
184
189
scrollToTop ( ) {
185
- const { view } = this . refs ;
186
- view . scrollTop = 0 ;
190
+ this . view . scrollTop = 0 ;
187
191
}
188
192
189
193
scrollToRight ( ) {
190
- const { view } = this . refs ;
191
- view . scrollLeft = view . scrollWidth ;
194
+ this . view . scrollLeft = this . view . scrollWidth ;
192
195
}
193
196
194
197
scrollToBottom ( ) {
195
- const { view } = this . refs ;
196
- view . scrollTop = view . scrollHeight ;
198
+ this . view . scrollTop = this . view . scrollHeight ;
197
199
}
198
200
199
201
addListeners ( ) {
200
202
/* istanbul ignore if */
201
203
if ( typeof document === 'undefined' ) return ;
202
- const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this . refs ;
204
+ const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this ;
203
205
view . addEventListener ( 'scroll' , this . handleScroll ) ;
204
206
if ( ! getScrollbarWidth ( ) ) return ;
205
207
trackHorizontal . addEventListener ( 'mouseenter' , this . handleTrackMouseEnter ) ;
@@ -216,7 +218,7 @@ export default class Scrollbars extends Component {
216
218
removeListeners ( ) {
217
219
/* istanbul ignore if */
218
220
if ( typeof document === 'undefined' ) return ;
219
- const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this . refs ;
221
+ const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this ;
220
222
view . removeEventListener ( 'scroll' , this . handleScroll ) ;
221
223
if ( ! getScrollbarWidth ( ) ) return ;
222
224
trackHorizontal . removeEventListener ( 'mouseenter' , this . handleTrackMouseEnter ) ;
@@ -274,22 +276,20 @@ export default class Scrollbars extends Component {
274
276
275
277
handleHorizontalTrackMouseDown ( event ) {
276
278
event . preventDefault ( ) ;
277
- const { view } = this . refs ;
278
279
const { target, clientX } = event ;
279
280
const { left : targetLeft } = target . getBoundingClientRect ( ) ;
280
281
const thumbWidth = this . getThumbHorizontalWidth ( ) ;
281
282
const offset = Math . abs ( targetLeft - clientX ) - thumbWidth / 2 ;
282
- view . scrollLeft = this . getScrollLeftForOffset ( offset ) ;
283
+ this . view . scrollLeft = this . getScrollLeftForOffset ( offset ) ;
283
284
}
284
285
285
286
handleVerticalTrackMouseDown ( event ) {
286
287
event . preventDefault ( ) ;
287
- const { view } = this . refs ;
288
288
const { target, clientY } = event ;
289
289
const { top : targetTop } = target . getBoundingClientRect ( ) ;
290
290
const thumbHeight = this . getThumbVerticalHeight ( ) ;
291
291
const offset = Math . abs ( targetTop - clientY ) - thumbHeight / 2 ;
292
- view . scrollTop = this . getScrollTopForOffset ( offset ) ;
292
+ this . view . scrollTop = this . getScrollTopForOffset ( offset ) ;
293
293
}
294
294
295
295
handleHorizontalThumbMouseDown ( event ) {
@@ -333,21 +333,19 @@ export default class Scrollbars extends Component {
333
333
handleDrag ( event ) {
334
334
if ( this . prevPageX ) {
335
335
const { clientX } = event ;
336
- const { view, trackHorizontal } = this . refs ;
337
- const { left : trackLeft } = trackHorizontal . getBoundingClientRect ( ) ;
336
+ const { left : trackLeft } = this . trackHorizontal . getBoundingClientRect ( ) ;
338
337
const thumbWidth = this . getThumbHorizontalWidth ( ) ;
339
338
const clickPosition = thumbWidth - this . prevPageX ;
340
339
const offset = - trackLeft + clientX - clickPosition ;
341
- view . scrollLeft = this . getScrollLeftForOffset ( offset ) ;
340
+ this . view . scrollLeft = this . getScrollLeftForOffset ( offset ) ;
342
341
}
343
342
if ( this . prevPageY ) {
344
343
const { clientY } = event ;
345
- const { view, trackVertical } = this . refs ;
346
- const { top : trackTop } = trackVertical . getBoundingClientRect ( ) ;
344
+ const { top : trackTop } = this . trackVertical . getBoundingClientRect ( ) ;
347
345
const thumbHeight = this . getThumbVerticalHeight ( ) ;
348
346
const clickPosition = thumbHeight - this . prevPageY ;
349
347
const offset = - trackTop + clientY - clickPosition ;
350
- view . scrollTop = this . getScrollTopForOffset ( offset ) ;
348
+ this . view . scrollTop = this . getScrollTopForOffset ( offset ) ;
351
349
}
352
350
return false ;
353
351
}
@@ -388,22 +386,20 @@ export default class Scrollbars extends Component {
388
386
}
389
387
390
388
showTracks ( ) {
391
- const { trackHorizontal, trackVertical } = this . refs ;
392
389
clearTimeout ( this . hideTracksTimeout ) ;
393
- css ( trackHorizontal , { opacity : 1 } ) ;
394
- css ( trackVertical , { opacity : 1 } ) ;
390
+ css ( this . trackHorizontal , { opacity : 1 } ) ;
391
+ css ( this . trackVertical , { opacity : 1 } ) ;
395
392
}
396
393
397
394
hideTracks ( ) {
398
395
if ( this . dragging ) return ;
399
396
if ( this . scrolling ) return ;
400
397
if ( this . trackMouseOver ) return ;
401
398
const { autoHideTimeout } = this . props ;
402
- const { trackHorizontal, trackVertical } = this . refs ;
403
399
clearTimeout ( this . hideTracksTimeout ) ;
404
400
this . hideTracksTimeout = setTimeout ( ( ) => {
405
- css ( trackHorizontal , { opacity : 0 } ) ;
406
- css ( trackVertical , { opacity : 0 } ) ;
401
+ css ( this . trackHorizontal , { opacity : 0 } ) ;
402
+ css ( this . trackVertical , { opacity : 0 } ) ;
407
403
} , autoHideTimeout ) ;
408
404
}
409
405
@@ -439,17 +435,16 @@ export default class Scrollbars extends Component {
439
435
const { onUpdate, hideTracksWhenNotNeeded } = this . props ;
440
436
const values = this . getValues ( ) ;
441
437
if ( getScrollbarWidth ( ) ) {
442
- const { thumbHorizontal, thumbVertical, trackHorizontal, trackVertical } = this . refs ;
443
438
const { scrollLeft, clientWidth, scrollWidth } = values ;
444
- const trackHorizontalWidth = getInnerWidth ( trackHorizontal ) ;
439
+ const trackHorizontalWidth = getInnerWidth ( this . trackHorizontal ) ;
445
440
const thumbHorizontalWidth = this . getThumbHorizontalWidth ( ) ;
446
441
const thumbHorizontalX = scrollLeft / ( scrollWidth - clientWidth ) * ( trackHorizontalWidth - thumbHorizontalWidth ) ;
447
442
const thumbHorizontalStyle = {
448
443
width : thumbHorizontalWidth ,
449
444
transform : `translateX(${ thumbHorizontalX } px)`
450
445
} ;
451
446
const { scrollTop, clientHeight, scrollHeight } = values ;
452
- const trackVerticalHeight = getInnerHeight ( trackVertical ) ;
447
+ const trackVerticalHeight = getInnerHeight ( this . trackVertical ) ;
453
448
const thumbVerticalHeight = this . getThumbVerticalHeight ( ) ;
454
449
const thumbVerticalY = scrollTop / ( scrollHeight - clientHeight ) * ( trackVerticalHeight - thumbVerticalHeight ) ;
455
450
const thumbVerticalStyle = {
@@ -463,11 +458,11 @@ export default class Scrollbars extends Component {
463
458
const trackVerticalStyle = {
464
459
visibility : scrollHeight > clientHeight ? 'visible' : 'hidden'
465
460
} ;
466
- css ( trackHorizontal , trackHorizontalStyle ) ;
467
- css ( trackVertical , trackVerticalStyle ) ;
461
+ css ( this . trackHorizontal , trackHorizontalStyle ) ;
462
+ css ( this . trackVertical , trackVerticalStyle ) ;
468
463
}
469
- css ( thumbHorizontal , thumbHorizontalStyle ) ;
470
- css ( thumbVertical , thumbVerticalStyle ) ;
464
+ css ( this . thumbHorizontal , thumbHorizontalStyle ) ;
465
+ css ( this . thumbVertical , thumbVerticalStyle ) ;
471
466
}
472
467
if ( onUpdate ) onUpdate ( values ) ;
473
468
if ( typeof callback !== 'function' ) return ;
@@ -562,26 +557,26 @@ export default class Scrollbars extends Component {
562
557
} )
563
558
} ;
564
559
565
- return createElement ( tagName , { ...props , style : containerStyle , ref : ' container' } , [
560
+ return createElement ( tagName , { ...props , style : containerStyle , ref : ( ref ) => { this . container = ref ; } } , [
566
561
cloneElement (
567
562
renderView ( { style : viewStyle } ) ,
568
- { key : 'view' , ref : ' view' } ,
563
+ { key : 'view' , ref : ( ref ) => { this . view = ref ; } } ,
569
564
children
570
565
) ,
571
566
cloneElement (
572
567
renderTrackHorizontal ( { style : trackHorizontalStyle } ) ,
573
- { key : 'trackHorizontal' , ref : ' trackHorizontal' } ,
568
+ { key : 'trackHorizontal' , ref : ( ref ) => { this . trackHorizontal = ref ; } } ,
574
569
cloneElement (
575
570
renderThumbHorizontal ( { style : thumbHorizontalStyleDefault } ) ,
576
- { ref : ' thumbHorizontal' }
571
+ { ref : ( ref ) => { this . thumbHorizontal = ref ; } }
577
572
)
578
573
) ,
579
574
cloneElement (
580
575
renderTrackVertical ( { style : trackVerticalStyle } ) ,
581
- { key : 'trackVertical' , ref : ' trackVertical' } ,
576
+ { key : 'trackVertical' , ref : ( ref ) => { this . trackVertical = ref ; } } ,
582
577
cloneElement (
583
578
renderThumbVertical ( { style : thumbVerticalStyleDefault } ) ,
584
- { ref : ' thumbVertical' }
579
+ { ref : ( ref ) => { this . thumbVertical = ref ; } }
585
580
)
586
581
)
587
582
] ) ;
0 commit comments