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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
import * as React from 'react';
const AUTOHIDE_TIMEOUT = 1000;
interface IProps {
autoHide: boolean;
trackPadding: { x: number; y: number };
onScroll?: (value: IScrollEvent) => void;
style?: React.CSSProperties;
children?: React.ReactNode;
}
interface IState {
canScroll: boolean;
showScrollIndicators: boolean;
showTrack: boolean;
isTrackHovered: boolean;
isDragging: boolean;
dragStart: {
x: number;
y: number;
};
isWide: boolean;
}
export interface IScrollEvent {
scrollLeft: number;
scrollTop: number;
}
export type ScrollPosition = 'top' | 'bottom' | 'middle';
interface IScrollbarUpdateContext {
size: boolean;
position: boolean;
}
export default class CustomScrollbars extends React.Component<IProps, IState> {
public static defaultProps: IProps = {
// auto-hide on macOS by default
autoHide: process.platform === 'darwin',
trackPadding: { x: 2, y: 2 },
};
public state = {
canScroll: false,
showScrollIndicators: true,
showTrack: false,
isTrackHovered: false,
isDragging: false,
dragStart: { x: 0, y: 0 },
isWide: false,
};
private scrollableRef = React.createRef<HTMLDivElement>();
private trackRef = React.createRef<HTMLDivElement>();
private thumbRef = React.createRef<HTMLDivElement>();
private autoHideTimer?: NodeJS.Timeout;
public scrollTo(x: number, y: number) {
const scrollable = this.scrollableRef.current;
if (scrollable) {
scrollable.scrollLeft = x;
scrollable.scrollTop = y;
}
}
public scrollToElement(child: HTMLElement, scrollPosition: ScrollPosition) {
const scrollable = this.scrollableRef.current;
if (scrollable) {
// throw if child is not a descendant of scroll view
if (!scrollable.contains(child)) {
throw new Error(
'Cannot scroll to an element which is not a descendant of CustomScrollbars.',
);
}
const scrollTop = this.computeScrollTop(scrollable, child, scrollPosition);
this.scrollTo(0, scrollTop);
}
}
public componentDidMount() {
this.updateScrollbarsHelper({
position: true,
size: true,
});
document.addEventListener('mousemove', this.handleMouseMove);
document.addEventListener('mouseup', this.handleMouseUp);
document.addEventListener('mousedown', this.handleMouseDown);
// show scroll indicators briefly when mounted
if (this.props.autoHide) {
this.startAutoHide();
}
}
public shouldComponentUpdate(nextProps: IProps, nextState: IState) {
const prevProps = this.props;
const prevState = this.state;
return (
prevProps.children !== nextProps.children ||
prevProps.autoHide !== nextProps.autoHide ||
prevProps.trackPadding.x !== nextProps.trackPadding.x ||
prevProps.trackPadding.y !== nextProps.trackPadding.y ||
prevState.canScroll !== nextState.canScroll ||
prevState.showScrollIndicators !== nextState.showScrollIndicators ||
prevState.showTrack !== nextState.showTrack ||
prevState.isTrackHovered !== nextState.isTrackHovered ||
prevState.isDragging !== nextState.isDragging ||
prevState.isWide !== nextState.isWide
);
}
public componentWillUnmount() {
this.stopAutoHide();
document.removeEventListener('mousemove', this.handleMouseMove);
document.removeEventListener('mouseup', this.handleMouseUp);
document.removeEventListener('mousedown', this.handleMouseDown);
}
public componentDidUpdate() {
this.updateScrollbarsHelper({
position: true,
size: true,
});
}
public render() {
const {
autoHide: _autoHide,
trackPadding: _trackPadding,
onScroll: _onScroll,
children,
...otherProps
} = this.props;
const showScrollbars = this.state.canScroll && this.state.showScrollIndicators;
const thumbAnimationClass = showScrollbars ? ' custom-scrollbars__thumb--visible' : '';
const thumbActiveClass =
this.state.isTrackHovered || this.state.isDragging ? ' custom-scrollbars__thumb--active' : '';
const thumbWideClass = this.state.isWide ? ' custom-scrollbars__thumb--wide' : '';
const trackClass =
showScrollbars && this.state.showTrack ? ' custom-scrollbars__track--visible' : '';
return (
<div {...otherProps} className="custom-scrollbars">
<div className={`custom-scrollbars__track ${trackClass}`} ref={this.trackRef} />
<div
className={`custom-scrollbars__thumb ${thumbWideClass} ${thumbActiveClass} ${thumbAnimationClass}`}
style={{ position: 'absolute', top: 0, right: 0 }}
ref={this.thumbRef}
/>
<div
className="custom-scrollbars__scrollable"
style={{ overflow: 'auto' }}
onScroll={this.onScroll}
ref={this.scrollableRef}>
{children}
</div>
</div>
);
}
private onScroll = () => {
this.updateScrollbarsHelper({ position: true });
if (this.props.autoHide) {
this.ensureScrollbarsVisible();
// only auto-hide when scrolling with mousewheel
if (!this.state.isDragging) {
this.startAutoHide();
}
} else {
// only auto-shrink when scrolling with mousewheel
if (!this.state.isDragging) {
this.startAutoShrink();
}
}
const scrollView = this.scrollableRef.current;
if (scrollView && this.props.onScroll) {
this.props.onScroll({
scrollLeft: scrollView.scrollLeft,
scrollTop: scrollView.scrollTop,
});
}
};
private handleEnterTrack = () => {
this.stopAutoHide();
this.setState({
isTrackHovered: true,
showScrollIndicators: true,
showTrack: true,
isWide: true,
});
};
private handleLeaveTrack = () => {
this.setState({
isTrackHovered: false,
});
// do not hide the scrollbar if user is dragging a thumb but left the track area.
if (!this.state.isDragging) {
if (this.props.autoHide) {
this.startAutoHide();
} else {
this.startAutoShrink();
}
}
};
private handleMouseDown = (event: MouseEvent) => {
const thumb = this.thumbRef.current;
const cursorPosition = {
x: event.clientX,
y: event.clientY,
};
// initiate dragging when user clicked inside of thumb
if (thumb && this.isPointInsideOfElement(thumb, cursorPosition)) {
this.setState({
isDragging: true,
dragStart: this.getPointRelativeToElement(thumb, cursorPosition),
});
}
};
private handleMouseUp = (event: MouseEvent) => {
if (!this.state.isDragging) {
return;
}
this.setState({
isDragging: false,
});
const track = this.trackRef.current;
if (track) {
// Make sure to auto-hide the scrollbar if cursor ended up outside of scroll track
const cursorPosition = {
x: event.clientX,
y: event.clientY,
};
if (!this.isPointInsideOfElement(track, cursorPosition)) {
if (this.props.autoHide) {
this.startAutoHide();
} else {
this.startAutoShrink();
}
}
}
};
private handleMouseMove = (event: MouseEvent) => {
const scrollable = this.scrollableRef.current;
const thumb = this.thumbRef.current;
const track = this.trackRef.current;
const cursorPosition = {
x: event.clientX,
y: event.clientY,
};
if (this.state.isDragging && scrollable && thumb) {
// the content height of the scroll view
const scrollHeight = scrollable.scrollHeight;
// the visible height of the scroll view
const visibleHeight = scrollable.offsetHeight;
// lowest point of scrollTop
const maxScrollTop = scrollHeight - visibleHeight;
// Map absolute cursor coordinate to point in scroll container
const pointInScrollContainer = this.getPointRelativeToElement(scrollable, cursorPosition);
// calculate the thumb boundary to make sure that the visual appearance of
// a thumb at the lowest point matches the bottom of scrollable view
const thumbBoundary = this.computeTrackLength(scrollable) - thumb.clientHeight;
const thumbTop =
pointInScrollContainer.y - this.state.dragStart.y - this.props.trackPadding.y;
const newScrollTop = (thumbTop / thumbBoundary) * maxScrollTop;
scrollable.scrollTop = newScrollTop;
}
if (scrollable && track) {
const intersectsTrack = this.isPointInsideOfElement(track, cursorPosition);
if (!this.state.isTrackHovered && intersectsTrack) {
this.handleEnterTrack();
} else if (this.state.isTrackHovered && !intersectsTrack) {
this.handleLeaveTrack();
}
}
};
private ensureScrollbarsVisible() {
if (!this.state.showScrollIndicators) {
this.setState({
showScrollIndicators: true,
});
}
}
private startAutoHide() {
if (this.autoHideTimer) {
clearTimeout(this.autoHideTimer);
}
this.autoHideTimer = global.setTimeout(() => {
this.setState({
showScrollIndicators: false,
showTrack: false,
isWide: false,
});
}, AUTOHIDE_TIMEOUT);
}
private startAutoShrink() {
if (this.autoHideTimer) {
clearTimeout(this.autoHideTimer);
}
this.autoHideTimer = global.setTimeout(() => {
this.setState({
showTrack: false,
isWide: false,
});
}, AUTOHIDE_TIMEOUT);
}
private stopAutoHide() {
if (this.autoHideTimer) {
clearTimeout(this.autoHideTimer);
this.autoHideTimer = undefined;
}
}
private isPointInsideOfElement(element: HTMLElement, point: { x: number; y: number }) {
const rect = element.getBoundingClientRect();
return (
point.x >= rect.left && point.x <= rect.right && point.y >= rect.top && point.y <= rect.bottom
);
}
private getPointRelativeToElement(element: HTMLElement, point: { x: number; y: number }) {
const rect = element.getBoundingClientRect();
return {
x: point.x - rect.left,
y: point.y - rect.top,
};
}
private computeTrackLength(scrollable: HTMLElement) {
return scrollable.offsetHeight - this.props.trackPadding.y * 2;
}
// Computes the position of child element within scrollable container
private computeOffsetTop(scrollable: HTMLElement, child: HTMLElement) {
let offsetTop = 0;
let node = child;
while (scrollable.contains(node)) {
offsetTop += node.offsetTop;
if (node.offsetParent) {
node = node.offsetParent as HTMLElement;
} else {
break;
}
}
return offsetTop;
}
private computeScrollTop(
scrollable: HTMLElement,
child: HTMLElement,
scrollPosition: ScrollPosition,
) {
const offsetTop = this.computeOffsetTop(scrollable, child);
switch (scrollPosition) {
case 'top':
return offsetTop;
case 'bottom':
return offsetTop - (scrollable.offsetHeight - child.clientHeight);
case 'middle':
return offsetTop - (scrollable.offsetHeight - child.clientHeight) * 0.5;
}
}
private computeThumbPosition(scrollable: HTMLElement, thumb: HTMLElement) {
// the content height of the scroll view
const scrollHeight = scrollable.scrollHeight;
// the visible height of the scroll view
const visibleHeight = scrollable.offsetHeight;
// scroll offset
const scrollTop = scrollable.scrollTop;
// lowest point of scrollTop
const maxScrollTop = scrollHeight - visibleHeight;
// calculate scroll position within 0..1 range
const scrollPosition = scrollHeight > 0 ? scrollTop / maxScrollTop : 0;
// calculate the thumb boundary to make sure that the visual appearance of
// a thumb at the lowest point matches the bottom of scrollable view
const thumbBoundary = this.computeTrackLength(scrollable) - thumb.clientHeight;
// calculate thumb position based on scroll progress and thumb boundary
// adding vertical inset to adjust the thumb's appearance
const thumbPosition = thumbBoundary * scrollPosition + this.props.trackPadding.y;
return {
x: -this.props.trackPadding.x,
y: thumbPosition,
};
}
private computeThumbHeight(scrollable: HTMLElement) {
const scrollHeight = scrollable.scrollHeight;
const visibleHeight = scrollable.offsetHeight;
const thumbHeight = (visibleHeight / scrollHeight) * visibleHeight;
// ensure that the scroll thumb doesn't shrink to nano size
return Math.max(thumbHeight, 8);
}
private updateScrollbarsHelper(updateFlags: Partial<IScrollbarUpdateContext>) {
const scrollable = this.scrollableRef.current;
const thumb = this.thumbRef.current;
if (scrollable && thumb) {
this.updateScrollbars(scrollable, thumb, updateFlags);
}
}
private updateScrollbars(
scrollable: HTMLElement,
thumb: HTMLElement,
context: Partial<IScrollbarUpdateContext>,
) {
if (context.size) {
const thumbHeight = this.computeThumbHeight(scrollable);
thumb.style.setProperty('height', thumbHeight + 'px');
// hide thumb when there is nothing to scroll
const canScroll = thumbHeight < scrollable.offsetHeight;
if (this.state.canScroll !== canScroll) {
this.setState({ canScroll });
// flash the scroll indicators when the view becomes scrollable
if (this.props.autoHide && canScroll) {
this.startAutoHide();
this.ensureScrollbarsVisible();
}
}
}
if (context.position) {
const { x, y } = this.computeThumbPosition(scrollable, thumb);
thumb.style.setProperty('transform', `translate(${x}px, ${y}px)`);
}
}
}
|