stickyScrollBar.js
6.13 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
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import * as React from 'react';
import addEventListener from "rc-util/es/Dom/addEventListener";
import getScrollBarSize from "rc-util/es/getScrollBarSize";
import classNames from 'classnames';
import { getOffset } from "rc-util/es/Dom/css";
import TableContext from './context/TableContext';
import { useLayoutState } from './hooks/useFrame';
var StickyScrollBar = function StickyScrollBar(_ref, ref) {
var _scrollBodyRef$curren, _scrollBodyRef$curren2;
var scrollBodyRef = _ref.scrollBodyRef,
onScroll = _ref.onScroll,
offsetScroll = _ref.offsetScroll,
container = _ref.container;
var _React$useContext = React.useContext(TableContext),
prefixCls = _React$useContext.prefixCls;
var bodyScrollWidth = ((_scrollBodyRef$curren = scrollBodyRef.current) === null || _scrollBodyRef$curren === void 0 ? void 0 : _scrollBodyRef$curren.scrollWidth) || 0;
var bodyWidth = ((_scrollBodyRef$curren2 = scrollBodyRef.current) === null || _scrollBodyRef$curren2 === void 0 ? void 0 : _scrollBodyRef$curren2.clientWidth) || 0;
var scrollBarWidth = bodyScrollWidth && bodyWidth * (bodyWidth / bodyScrollWidth);
var scrollBarRef = React.useRef();
var _useLayoutState = useLayoutState({
scrollLeft: 0,
isHiddenScrollBar: false
}),
_useLayoutState2 = _slicedToArray(_useLayoutState, 2),
scrollState = _useLayoutState2[0],
setScrollState = _useLayoutState2[1];
var refState = React.useRef({
delta: 0,
x: 0
});
var _React$useState = React.useState(false),
_React$useState2 = _slicedToArray(_React$useState, 2),
isActive = _React$useState2[0],
setActive = _React$useState2[1];
var onMouseUp = function onMouseUp() {
setActive(false);
};
var onMouseDown = function onMouseDown(event) {
event.persist();
refState.current.delta = event.pageX - scrollState.scrollLeft;
refState.current.x = 0;
setActive(true);
event.preventDefault();
};
var onMouseMove = function onMouseMove(event) {
var _window;
// https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons
var _ref2 = event || ((_window = window) === null || _window === void 0 ? void 0 : _window.event),
buttons = _ref2.buttons;
if (!isActive || buttons === 0) {
// If out body mouse up, we can set isActive false when mouse move
if (isActive) {
setActive(false);
}
return;
}
var left = refState.current.x + event.pageX - refState.current.x - refState.current.delta;
if (left <= 0) {
left = 0;
}
if (left + scrollBarWidth >= bodyWidth) {
left = bodyWidth - scrollBarWidth;
}
onScroll({
scrollLeft: left / bodyWidth * (bodyScrollWidth + 2)
});
refState.current.x = event.pageX;
};
var onContainerScroll = function onContainerScroll() {
var tableOffsetTop = getOffset(scrollBodyRef.current).top;
var tableBottomOffset = tableOffsetTop + scrollBodyRef.current.offsetHeight;
var currentClientOffset = container === window ? document.documentElement.scrollTop + window.innerHeight : getOffset(container).top + container.clientHeight;
if (tableBottomOffset - getScrollBarSize() <= currentClientOffset || tableOffsetTop >= currentClientOffset - offsetScroll) {
setScrollState(function (state) {
return _objectSpread(_objectSpread({}, state), {}, {
isHiddenScrollBar: true
});
});
} else {
setScrollState(function (state) {
return _objectSpread(_objectSpread({}, state), {}, {
isHiddenScrollBar: false
});
});
}
};
var setScrollLeft = function setScrollLeft(left) {
setScrollState(function (state) {
return _objectSpread(_objectSpread({}, state), {}, {
scrollLeft: left / bodyScrollWidth * bodyWidth || 0
});
});
};
React.useImperativeHandle(ref, function () {
return {
setScrollLeft: setScrollLeft
};
});
React.useEffect(function () {
var onMouseUpListener = addEventListener(document.body, 'mouseup', onMouseUp, false);
var onMouseMoveListener = addEventListener(document.body, 'mousemove', onMouseMove, false);
onContainerScroll();
return function () {
onMouseUpListener.remove();
onMouseMoveListener.remove();
};
}, [scrollBarWidth, isActive]);
React.useEffect(function () {
var onScrollListener = addEventListener(container, 'scroll', onContainerScroll, false);
var onResizeListener = addEventListener(window, 'resize', onContainerScroll, false);
return function () {
onScrollListener.remove();
onResizeListener.remove();
};
}, [container]);
React.useEffect(function () {
if (!scrollState.isHiddenScrollBar) {
setScrollState(function (state) {
var _scrollBodyRef$curren3, _scrollBodyRef$curren4;
return _objectSpread(_objectSpread({}, state), {}, {
scrollLeft: scrollBodyRef.current.scrollLeft / ((_scrollBodyRef$curren3 = scrollBodyRef.current) === null || _scrollBodyRef$curren3 === void 0 ? void 0 : _scrollBodyRef$curren3.scrollWidth) * ((_scrollBodyRef$curren4 = scrollBodyRef.current) === null || _scrollBodyRef$curren4 === void 0 ? void 0 : _scrollBodyRef$curren4.clientWidth)
});
});
}
}, [scrollState.isHiddenScrollBar]);
if (bodyScrollWidth <= bodyWidth || !scrollBarWidth || scrollState.isHiddenScrollBar) {
return null;
}
return React.createElement("div", {
style: {
height: getScrollBarSize(),
width: bodyWidth,
bottom: offsetScroll
},
className: "".concat(prefixCls, "-sticky-scroll")
}, React.createElement("div", {
onMouseDown: onMouseDown,
ref: scrollBarRef,
className: classNames("".concat(prefixCls, "-sticky-scroll-bar"), _defineProperty({}, "".concat(prefixCls, "-sticky-scroll-bar-active"), isActive)),
style: {
width: "".concat(scrollBarWidth, "px"),
transform: "translate3d(".concat(scrollState.scrollLeft, "px, 0, 0)")
}
}));
};
export default React.forwardRef(StickyScrollBar);