Skip to content

Commit bc0fe12

Browse files
author
Malte Wessel
committed
2 parents b96c917 + 6cf5174 commit bc0fe12

File tree

12 files changed

+165
-180
lines changed

12 files changed

+165
-180
lines changed

docs/customization.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class CustomScrollbars extends Component {
5252
return (
5353
<Scrollbars
5454
renderTrackHorizontal={({ style, ...props }) =>
55-
<div {...props} style={{ ...style, backgroundColor: 'blue' }}>
55+
<div {...props} style={{ ...style, backgroundColor: 'blue' }}/>
5656
}>
5757
{this.props.children}
5858
</Scrollbars>

docs/usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class App extends Component {
4444
// Called when scrolling starts
4545
onScrollStart={this.handleScrollStart}
4646
// Called when scrolling stops
47-
onScrollStop={this.handlenScrollStop}>
47+
onScrollStop={this.handlenScrollStop}
4848
// Called when ever the component is updated. Runs inside the animation frame
49-
onUpdate={this.handleUpdate}
49+
onUpdate={this.handleUpdate}>
5050
<p>Some great content...</p>
5151
</Scrollbars>
5252
);

src/Scrollbars/index.js

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,25 @@ export default class Scrollbars extends Component {
3636
constructor(props, ...rest) {
3737
super(props, ...rest);
3838

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+
3958
this.handleTrackMouseEnter = this.handleTrackMouseEnter.bind(this);
4059
this.handleTrackMouseLeave = this.handleTrackMouseLeave.bind(this);
4160
this.handleHorizontalTrackMouseDown = this.handleHorizontalTrackMouseDown.bind(this);
@@ -76,45 +95,38 @@ export default class Scrollbars extends Component {
7695
}
7796

7897
getScrollLeft() {
79-
const { view } = this.refs;
80-
return view.scrollLeft;
98+
return this.view.scrollLeft;
8199
}
82100

83101
getScrollTop() {
84-
const { view } = this.refs;
85-
return view.scrollTop;
102+
return this.view.scrollTop;
86103
}
87104

88105
getScrollWidth() {
89-
const { view } = this.refs;
90-
return view.scrollWidth;
106+
return this.view.scrollWidth;
91107
}
92108

93109
getScrollHeight() {
94-
const { view } = this.refs;
95-
return view.scrollHeight;
110+
return this.view.scrollHeight;
96111
}
97112

98113
getClientWidth() {
99-
const { view } = this.refs;
100-
return view.clientWidth;
114+
return this.view.clientWidth;
101115
}
102116

103117
getClientHeight() {
104-
const { view } = this.refs;
105-
return view.clientHeight;
118+
return this.view.clientHeight;
106119
}
107120

108121
getValues() {
109-
const { view } = this.refs;
110122
const {
111123
scrollLeft,
112124
scrollTop,
113125
scrollWidth,
114126
scrollHeight,
115127
clientWidth,
116128
clientHeight
117-
} = view;
129+
} = this.view;
118130

119131
return {
120132
left: (scrollLeft / (scrollWidth - clientWidth)) || 0,
@@ -130,9 +142,8 @@ export default class Scrollbars extends Component {
130142

131143
getThumbHorizontalWidth() {
132144
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);
136147
const width = Math.ceil(clientWidth / scrollWidth * trackWidth);
137148
if (trackWidth === width) return 0;
138149
if (thumbSize) return thumbSize;
@@ -141,65 +152,56 @@ export default class Scrollbars extends Component {
141152

142153
getThumbVerticalHeight() {
143154
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);
147157
const height = Math.ceil(clientHeight / scrollHeight * trackHeight);
148158
if (trackHeight === height) return 0;
149159
if (thumbSize) return thumbSize;
150160
return Math.max(height, thumbMinSize);
151161
}
152162

153163
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);
157166
const thumbWidth = this.getThumbHorizontalWidth();
158167
return offset / (trackWidth - thumbWidth) * (scrollWidth - clientWidth);
159168
}
160169

161170
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);
165173
const thumbHeight = this.getThumbVerticalHeight();
166174
return offset / (trackHeight - thumbHeight) * (scrollHeight - clientHeight);
167175
}
168176

169177
scrollLeft(left = 0) {
170-
const { view } = this.refs;
171-
view.scrollLeft = left;
178+
this.view.scrollLeft = left;
172179
}
173180

174181
scrollTop(top = 0) {
175-
const { view } = this.refs;
176-
view.scrollTop = top;
182+
this.view.scrollTop = top;
177183
}
178184

179185
scrollToLeft() {
180-
const { view } = this.refs;
181-
view.scrollLeft = 0;
186+
this.view.scrollLeft = 0;
182187
}
183188

184189
scrollToTop() {
185-
const { view } = this.refs;
186-
view.scrollTop = 0;
190+
this.view.scrollTop = 0;
187191
}
188192

189193
scrollToRight() {
190-
const { view } = this.refs;
191-
view.scrollLeft = view.scrollWidth;
194+
this.view.scrollLeft = this.view.scrollWidth;
192195
}
193196

194197
scrollToBottom() {
195-
const { view } = this.refs;
196-
view.scrollTop = view.scrollHeight;
198+
this.view.scrollTop = this.view.scrollHeight;
197199
}
198200

199201
addListeners() {
200202
/* istanbul ignore if */
201203
if (typeof document === 'undefined') return;
202-
const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this.refs;
204+
const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this;
203205
view.addEventListener('scroll', this.handleScroll);
204206
if (!getScrollbarWidth()) return;
205207
trackHorizontal.addEventListener('mouseenter', this.handleTrackMouseEnter);
@@ -216,7 +218,7 @@ export default class Scrollbars extends Component {
216218
removeListeners() {
217219
/* istanbul ignore if */
218220
if (typeof document === 'undefined') return;
219-
const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this.refs;
221+
const { view, trackHorizontal, trackVertical, thumbHorizontal, thumbVertical } = this;
220222
view.removeEventListener('scroll', this.handleScroll);
221223
if (!getScrollbarWidth()) return;
222224
trackHorizontal.removeEventListener('mouseenter', this.handleTrackMouseEnter);
@@ -274,22 +276,20 @@ export default class Scrollbars extends Component {
274276

275277
handleHorizontalTrackMouseDown(event) {
276278
event.preventDefault();
277-
const { view } = this.refs;
278279
const { target, clientX } = event;
279280
const { left: targetLeft } = target.getBoundingClientRect();
280281
const thumbWidth = this.getThumbHorizontalWidth();
281282
const offset = Math.abs(targetLeft - clientX) - thumbWidth / 2;
282-
view.scrollLeft = this.getScrollLeftForOffset(offset);
283+
this.view.scrollLeft = this.getScrollLeftForOffset(offset);
283284
}
284285

285286
handleVerticalTrackMouseDown(event) {
286287
event.preventDefault();
287-
const { view } = this.refs;
288288
const { target, clientY } = event;
289289
const { top: targetTop } = target.getBoundingClientRect();
290290
const thumbHeight = this.getThumbVerticalHeight();
291291
const offset = Math.abs(targetTop - clientY) - thumbHeight / 2;
292-
view.scrollTop = this.getScrollTopForOffset(offset);
292+
this.view.scrollTop = this.getScrollTopForOffset(offset);
293293
}
294294

295295
handleHorizontalThumbMouseDown(event) {
@@ -333,21 +333,19 @@ export default class Scrollbars extends Component {
333333
handleDrag(event) {
334334
if (this.prevPageX) {
335335
const { clientX } = event;
336-
const { view, trackHorizontal } = this.refs;
337-
const { left: trackLeft } = trackHorizontal.getBoundingClientRect();
336+
const { left: trackLeft } = this.trackHorizontal.getBoundingClientRect();
338337
const thumbWidth = this.getThumbHorizontalWidth();
339338
const clickPosition = thumbWidth - this.prevPageX;
340339
const offset = -trackLeft + clientX - clickPosition;
341-
view.scrollLeft = this.getScrollLeftForOffset(offset);
340+
this.view.scrollLeft = this.getScrollLeftForOffset(offset);
342341
}
343342
if (this.prevPageY) {
344343
const { clientY } = event;
345-
const { view, trackVertical } = this.refs;
346-
const { top: trackTop } = trackVertical.getBoundingClientRect();
344+
const { top: trackTop } = this.trackVertical.getBoundingClientRect();
347345
const thumbHeight = this.getThumbVerticalHeight();
348346
const clickPosition = thumbHeight - this.prevPageY;
349347
const offset = -trackTop + clientY - clickPosition;
350-
view.scrollTop = this.getScrollTopForOffset(offset);
348+
this.view.scrollTop = this.getScrollTopForOffset(offset);
351349
}
352350
return false;
353351
}
@@ -388,22 +386,20 @@ export default class Scrollbars extends Component {
388386
}
389387

390388
showTracks() {
391-
const { trackHorizontal, trackVertical } = this.refs;
392389
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 });
395392
}
396393

397394
hideTracks() {
398395
if (this.dragging) return;
399396
if (this.scrolling) return;
400397
if (this.trackMouseOver) return;
401398
const { autoHideTimeout } = this.props;
402-
const { trackHorizontal, trackVertical } = this.refs;
403399
clearTimeout(this.hideTracksTimeout);
404400
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 });
407403
}, autoHideTimeout);
408404
}
409405

@@ -439,17 +435,16 @@ export default class Scrollbars extends Component {
439435
const { onUpdate, hideTracksWhenNotNeeded } = this.props;
440436
const values = this.getValues();
441437
if (getScrollbarWidth()) {
442-
const { thumbHorizontal, thumbVertical, trackHorizontal, trackVertical } = this.refs;
443438
const { scrollLeft, clientWidth, scrollWidth } = values;
444-
const trackHorizontalWidth = getInnerWidth(trackHorizontal);
439+
const trackHorizontalWidth = getInnerWidth(this.trackHorizontal);
445440
const thumbHorizontalWidth = this.getThumbHorizontalWidth();
446441
const thumbHorizontalX = scrollLeft / (scrollWidth - clientWidth) * (trackHorizontalWidth - thumbHorizontalWidth);
447442
const thumbHorizontalStyle = {
448443
width: thumbHorizontalWidth,
449444
transform: `translateX(${thumbHorizontalX}px)`
450445
};
451446
const { scrollTop, clientHeight, scrollHeight } = values;
452-
const trackVerticalHeight = getInnerHeight(trackVertical);
447+
const trackVerticalHeight = getInnerHeight(this.trackVertical);
453448
const thumbVerticalHeight = this.getThumbVerticalHeight();
454449
const thumbVerticalY = scrollTop / (scrollHeight - clientHeight) * (trackVerticalHeight - thumbVerticalHeight);
455450
const thumbVerticalStyle = {
@@ -463,11 +458,11 @@ export default class Scrollbars extends Component {
463458
const trackVerticalStyle = {
464459
visibility: scrollHeight > clientHeight ? 'visible' : 'hidden'
465460
};
466-
css(trackHorizontal, trackHorizontalStyle);
467-
css(trackVertical, trackVerticalStyle);
461+
css(this.trackHorizontal, trackHorizontalStyle);
462+
css(this.trackVertical, trackVerticalStyle);
468463
}
469-
css(thumbHorizontal, thumbHorizontalStyle);
470-
css(thumbVertical, thumbVerticalStyle);
464+
css(this.thumbHorizontal, thumbHorizontalStyle);
465+
css(this.thumbVertical, thumbVerticalStyle);
471466
}
472467
if (onUpdate) onUpdate(values);
473468
if (typeof callback !== 'function') return;
@@ -562,26 +557,26 @@ export default class Scrollbars extends Component {
562557
})
563558
};
564559

565-
return createElement(tagName, { ...props, style: containerStyle, ref: 'container' }, [
560+
return createElement(tagName, { ...props, style: containerStyle, ref: (ref) => { this.container = ref; } }, [
566561
cloneElement(
567562
renderView({ style: viewStyle }),
568-
{ key: 'view', ref: 'view' },
563+
{ key: 'view', ref: (ref) => { this.view = ref; } },
569564
children
570565
),
571566
cloneElement(
572567
renderTrackHorizontal({ style: trackHorizontalStyle }),
573-
{ key: 'trackHorizontal', ref: 'trackHorizontal' },
568+
{ key: 'trackHorizontal', ref: (ref) => { this.trackHorizontal = ref; } },
574569
cloneElement(
575570
renderThumbHorizontal({ style: thumbHorizontalStyleDefault }),
576-
{ ref: 'thumbHorizontal' }
571+
{ ref: (ref) => { this.thumbHorizontal = ref; } }
577572
)
578573
),
579574
cloneElement(
580575
renderTrackVertical({ style: trackVerticalStyle }),
581-
{ key: 'trackVertical', ref: 'trackVertical' },
576+
{ key: 'trackVertical', ref: (ref) => { this.trackVertical = ref; } },
582577
cloneElement(
583578
renderThumbVertical({ style: thumbVerticalStyleDefault }),
584-
{ ref: 'thumbVertical' }
579+
{ ref: (ref) => { this.thumbVertical = ref; } }
585580
)
586581
)
587582
]);

0 commit comments

Comments
 (0)