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
|
// @flow
import React, { Component } from 'react';
type ScrollbarUpdateContext = {
size: boolean,
position: boolean,
};
export default class CustomScrollbars extends Component {
props: {
thumbInset: { x: number, y: number },
children: ?React.Element<*>,
};
static defaultProps = {
thumbInset: { x: 2, y: 2 },
};
_scrollableElement: ?HTMLElement;
_thumbElement: ?HTMLElement;
componentDidMount() {
this._updateScrollbarsHelper({
position: true,
size: true
});
}
componentDidUpdate() {
this._updateScrollbarsHelper({
position: true,
size: true
});
}
render() {
return (
<div className="custom-scrollbars">
<div className="custom-scrollbars__thumb"
style={{ position: 'absolute', top: 0, right: 0 }}
ref={ this._onThumbRef }></div>
<div className="custom-scrollbars__scrollable"
style={{ overflow: 'auto' }}
onScroll={ this._onScroll }
ref={ this._onScrollableRef }>
{ this.props.children }
</div>
</div>
);
}
_onScrollableRef = (ref) => {
this._scrollableElement = ref;
}
_onThumbRef = (ref) => {
this._thumbElement = ref;
}
_onScroll = () => {
this._updateScrollbarsHelper({ position: true });
}
_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;
const thumbHeight = thumb.clientHeight;
// calculate the thumb boundary to make sure that the visual appearance of
// a thumb at lowest point matches the bottom of scrollable view
const thumbBoundary = visibleHeight - thumbHeight - (this.props.thumbInset.y * 2);
// 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.thumbInset.y;
return {
x: -this.props.thumbInset.x,
y: thumbPosition,
};
}
_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);
}
_updateScrollbarsHelper(updateFlags: $Shape<ScrollbarUpdateContext>) {
const scrollable = this._scrollableElement;
const thumb = this._thumbElement;
if(scrollable && thumb) {
this._updateScrollbars(scrollable, thumb, updateFlags);
}
}
_updateScrollbars(scrollable: HTMLElement, thumb: HTMLElement, context: $Shape<ScrollbarUpdateContext>) {
if(context.size) {
const thumbHeight = this._computeThumbHeight(scrollable);
thumb.style.setProperty('height', thumbHeight + 'px');
// hide thumb when there is nothing to scroll
if(thumbHeight < scrollable.offsetHeight) {
thumb.style.setProperty('opacity', '1');
} else {
thumb.style.setProperty('opacity', '0');
}
}
if(context.position) {
const { x, y } = this._computeThumbPosition(scrollable, thumb);
thumb.style.setProperty('transform', `translate(${x}px, ${y}px)`);
}
}
}
|