-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomScrollbar.js
More file actions
167 lines (143 loc) · 6 KB
/
CustomScrollbar.js
File metadata and controls
167 lines (143 loc) · 6 KB
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
import React, { useLayoutEffect, useRef, useState, useEffect, useCallback } from 'react';
import useEventListener from './assets/useEventListener';
import useFakeAwait from './assets/useFakeAwait';
import ConditionalWrapper from './assets/ConditionalWrapper';
import useDebounceFunc from './assets/useDebounceFunc';
// Styling
import './assets/scrollbar.scss';
/**
* We use a negative right on the content to hide original OS scrollbars
*/
const OS_SCROLLBAR_WIDTH = (() => {
const outer = document.createElement('div');
const inner = document.createElement('div');
outer.style.overflow = 'scroll';
outer.style.width = '100%';
inner.style.width = '100%';
document.body.appendChild(outer);
outer.appendChild(inner);
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
outer.removeChild(inner);
document.body.removeChild(outer);
return scrollbarWidth;
})();
/**
* We need this for OSs that automatically hide the scrollbar (so the offset
* doesn't change in such case). Eg: macOS with "Automatically based on mouse".
*/
const SCROLLBAR_WIDTH = OS_SCROLLBAR_WIDTH || 20;
const defaultOpts = {
className: "scrollbar",
disabled: false,
height: "content",
shouldRender: true,
autohide: false
}
function CustomScrollbar({ children, ...options }) {
const { className, disabled, height, autohide: authideMS } = { ...defaultOpts, ...options };
const [scrollRatio, setScrollRatio] = useState(1);
const [autohide, setAutohide] = useState(false);
const [isDraggingTrack, setIsDraggingTrack] = useState(false);
const [disabledScrollbar, setDisabledScrollbar] = useState(disabled);
useEffect(() => setDisabledScrollbar(disabled), [disabled]);
const compare = useCallback(ref => {
if(!ref) return false;
if(ref.clientHeight) return ref;
return false;
}, []);
const [scrollerNode, setScrollerNode] = useFakeAwait(compare, []);
const [trackNode, setTrackNode] = useFakeAwait(compare, []);
const trackAnimationRef = useRef();
const memoizedProps = useRef({
authideMS
});
const moveTrack = useCallback(e => {
let moveAnimation;
let lastPageY = e.pageY;
let lastScrollTop = scrollerNode.scrollTop;
setIsDraggingTrack(true);
const drag = ({ pageY }) => {
cancelAnimationFrame(moveAnimation);
moveAnimation = requestAnimationFrame(() => {
const delta = pageY - lastPageY;
lastScrollTop += delta / scrollRatio;
lastPageY = pageY;
scrollerNode.scrollTop = lastScrollTop;
});
};
const stop = () => {
setIsDraggingTrack(false);
window.removeEventListener('mousemove', drag);
};
window.addEventListener('mousemove', drag);
window.addEventListener('mouseup', stop, { once: true });
}, [scrollerNode, scrollRatio],
);
const autohideFunc = useCallback(() => setAutohide(true), []);
const autohideTrigger = useDebounceFunc(autohideFunc, authideMS)
const onScroll = useCallback(() => {
const { clientHeight, scrollHeight, trackHeight } = memoizedProps.current;
if (scrollRatio === 1 || !trackNode) return;
cancelAnimationFrame(trackAnimationRef.current);
if(memoizedProps.current.authideMS){
setAutohide(false);
autohideTrigger();
}
trackAnimationRef.current = requestAnimationFrame(() => {
const ratio = (scrollerNode.scrollTop) / (scrollHeight - clientHeight);
const y = ratio * (clientHeight - trackHeight);
trackNode.style.transform = `translateY(${y}px)`;
});
}, [scrollerNode, scrollRatio, trackNode, autohideTrigger]);
const updateScrollbar = useCallback(() => {
if(!scrollerNode || !trackNode) return;
let scrollbarAnimation;
cancelAnimationFrame(scrollbarAnimation);
scrollbarAnimation = requestAnimationFrame(() => {
const { clientHeight, scrollHeight } = scrollerNode;
setScrollRatio(clientHeight / scrollHeight);
memoizedProps.current = {
...memoizedProps.current,
clientHeight,
scrollHeight: scrollHeight,
trackHeight: trackNode.clientHeight + 12 // Plus 12 to get margin on top and bottom
};
});
}, [scrollerNode, trackNode]);
useLayoutEffect(() => updateScrollbar(), [updateScrollbar]);
useEventListener(window, 'resize', updateScrollbar);
useLayoutEffect(() => {
if (disabledScrollbar || !scrollerNode) return;
scrollerNode.addEventListener('scroll', onScroll);
return () => {
scrollerNode.removeEventListener('scroll', onScroll);
};
}, [disabledScrollbar, onScroll, scrollerNode]);
const scrollerStyle = {
right: `-${SCROLLBAR_WIDTH}px`,
padding: `0 ${SCROLLBAR_WIDTH}px 0 0`,
width: `calc(100% + ${OS_SCROLLBAR_WIDTH}px)`
}
const trackStyle = {
right: isDraggingTrack ? 1 : undefined,
width: isDraggingTrack ? 10 : undefined,
height: `${scrollRatio * 100}%`,
opacity: !autohide && (!disabledScrollbar || scrollRatio === 1) ? undefined : 0,
display: disabledScrollbar ? 'none' : undefined
}
return (
<div className={ className }>
<div className="wrapper" style={{ marginLeft: `-${SCROLLBAR_WIDTH}px` }}>
<div className="inner" ref={ setScrollerNode } style={ scrollerStyle }>
<ConditionalWrapper
condition={ !(height === "content") }
wrapper={ <div className="test" style={{ height }}></div> }>
{ children }
</ConditionalWrapper>
</div>
</div>
<div className="tracker" ref={ setTrackNode } onMouseDown={ disabledScrollbar ? undefined : moveTrack } style={ trackStyle }/>
</div>
);
}
export default CustomScrollbar;