-
-
Notifications
You must be signed in to change notification settings - Fork 620
Expand file tree
/
Copy pathstickyScrollBar.tsx
More file actions
227 lines (204 loc) · 6.9 KB
/
stickyScrollBar.tsx
File metadata and controls
227 lines (204 loc) · 6.9 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
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
import { useContext } from '@rc-component/context';
import classNames from 'classnames';
import addEventListener from 'rc-util/lib/Dom/addEventListener';
import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
import * as React from 'react';
import TableContext from './context/TableContext';
import { useLayoutState } from './hooks/useFrame';
import raf from 'rc-util/lib/raf';
import { getOffset } from './utils/offsetUtil';
import { getDOM } from 'rc-util/lib/Dom/findDOMNode';
interface StickyScrollBarProps {
scrollBodyRef: React.RefObject<HTMLDivElement>;
onScroll: (params: { scrollLeft?: number }) => void;
offsetScroll: number;
container: HTMLElement | Window;
direction: string;
}
const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarProps> = (
{ scrollBodyRef, onScroll, offsetScroll, container, direction },
ref,
) => {
const prefixCls = useContext(TableContext, 'prefixCls');
const bodyScrollWidth = scrollBodyRef.current?.scrollWidth || 0;
const bodyWidth = scrollBodyRef.current?.clientWidth || 0;
const scrollBarWidth = bodyScrollWidth && bodyWidth * (bodyWidth / bodyScrollWidth);
const scrollBarRef = React.useRef<HTMLDivElement>();
const [scrollState, setScrollState] = useLayoutState<{
scrollLeft: number;
isHiddenScrollBar: boolean;
}>({
scrollLeft: 0,
isHiddenScrollBar: true,
});
const refState = React.useRef<{
delta: number;
x: number;
}>({
delta: 0,
x: 0,
});
const [isActive, setActive] = React.useState(false);
const rafRef = React.useRef<number | null>(null);
// 记录上一次的 scrollParents
const lastScrollParentsRef = React.useRef<(HTMLElement | SVGElement)[]>([]);
React.useEffect(
() => () => {
raf.cancel(rafRef.current);
},
[],
);
const onMouseUp: React.MouseEventHandler<HTMLDivElement> = () => {
setActive(false);
};
const onMouseDown: React.MouseEventHandler<HTMLDivElement> = event => {
event.persist();
refState.current.delta = event.pageX - scrollState.scrollLeft;
refState.current.x = 0;
setActive(true);
event.preventDefault();
};
const onMouseMove: React.MouseEventHandler<HTMLDivElement> = event => {
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
const { buttons } = event || (window?.event as any);
if (!isActive || buttons === 0) {
// If out body mouse up, we can set isActive false when mouse move
if (isActive) {
setActive(false);
}
return;
}
let left: number =
refState.current.x + event.pageX - refState.current.x - refState.current.delta;
const isRTL = direction === 'rtl';
// Limit scroll range
left = Math.max(
isRTL ? scrollBarWidth - bodyWidth : 0,
Math.min(isRTL ? 0 : bodyWidth - scrollBarWidth, left),
);
// Calculate the scroll position and update
const shouldScroll = !isRTL || Math.abs(left) + Math.abs(scrollBarWidth) < bodyWidth;
if (shouldScroll) {
onScroll({
scrollLeft: (left / bodyWidth) * (bodyScrollWidth + 2),
});
refState.current.x = event.pageX;
}
};
const checkScrollBarVisible = () => {
raf.cancel(rafRef.current);
rafRef.current = raf(() => {
if (!scrollBodyRef.current) {
return;
}
const tableOffsetTop = getOffset(scrollBodyRef.current).top;
const tableBottomOffset = tableOffsetTop + scrollBodyRef.current.offsetHeight;
const currentClientOffset =
container === window
? document.documentElement.scrollTop + window.innerHeight
: getOffset(container).top + (container as HTMLElement).clientHeight;
if (
tableBottomOffset - getScrollBarSize() <= currentClientOffset ||
tableOffsetTop >= currentClientOffset - offsetScroll
) {
setScrollState(state => ({
...state,
isHiddenScrollBar: true,
}));
} else {
setScrollState(state => ({
...state,
isHiddenScrollBar: false,
}));
}
});
};
const setScrollLeft = (left: number) => {
setScrollState(state => {
return {
...state,
scrollLeft: (left / bodyScrollWidth) * bodyWidth || 0,
};
});
};
React.useImperativeHandle(ref, () => ({
setScrollLeft,
checkScrollBarVisible,
}));
React.useEffect(() => {
const onMouseUpListener = addEventListener(document.body, 'mouseup', onMouseUp, false);
const onMouseMoveListener = addEventListener(document.body, 'mousemove', onMouseMove, false);
checkScrollBarVisible();
return () => {
onMouseUpListener.remove();
onMouseMoveListener.remove();
};
}, [scrollBarWidth, isActive]);
// Loop for scroll event check
React.useEffect(() => {
if (!scrollBodyRef.current) {
return;
}
// 清理上一次 scrollParents 的事件监听
lastScrollParentsRef.current.forEach(p =>
p.removeEventListener('scroll', checkScrollBarVisible),
);
const scrollParents: (HTMLElement | SVGElement)[] = [];
let parent = getDOM(scrollBodyRef.current);
while (parent) {
scrollParents.push(parent);
parent = parent.parentElement;
}
lastScrollParentsRef.current = scrollParents;
scrollParents.forEach(p => p.addEventListener('scroll', checkScrollBarVisible, false));
window.addEventListener('resize', checkScrollBarVisible, false);
window.addEventListener('scroll', checkScrollBarVisible, false);
container.addEventListener('scroll', checkScrollBarVisible, false);
return () => {
scrollParents.forEach(p => p.removeEventListener('scroll', checkScrollBarVisible));
window.removeEventListener('resize', checkScrollBarVisible);
window.removeEventListener('scroll', checkScrollBarVisible);
container.removeEventListener('scroll', checkScrollBarVisible);
};
}, [container]);
React.useEffect(() => {
if (!scrollState.isHiddenScrollBar) {
setScrollState(state => {
const bodyNode = scrollBodyRef.current;
if (!bodyNode) {
return state;
}
return {
...state,
scrollLeft: (bodyNode.scrollLeft / bodyNode.scrollWidth) * bodyNode.clientWidth,
};
});
}
}, [scrollState.isHiddenScrollBar]);
if (bodyScrollWidth <= bodyWidth || !scrollBarWidth || scrollState.isHiddenScrollBar) {
return null;
}
return (
<div
style={{
height: getScrollBarSize(),
width: bodyWidth,
bottom: offsetScroll,
}}
className={`${prefixCls}-sticky-scroll`}
>
<div
onMouseDown={onMouseDown}
ref={scrollBarRef}
className={classNames(`${prefixCls}-sticky-scroll-bar`, {
[`${prefixCls}-sticky-scroll-bar-active`]: isActive,
})}
style={{
width: `${scrollBarWidth}px`,
transform: `translate3d(${scrollState.scrollLeft}px, 0, 0)`,
}}
/>
</div>
);
};
export default React.forwardRef(StickyScrollBar);