@@ -6,17 +6,17 @@ import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
6
6
import * as React from 'react' ;
7
7
import TableContext from './context/TableContext' ;
8
8
import { useLayoutState } from './hooks/useFrame' ;
9
+ import raf from 'rc-util/lib/raf' ;
9
10
10
11
interface StickyScrollBarProps {
11
12
scrollBodyRef : React . RefObject < HTMLDivElement > ;
12
13
onScroll : ( params : { scrollLeft ?: number } ) => void ;
13
14
offsetScroll : number ;
14
15
container : HTMLElement | Window ;
15
- data ?: readonly any [ ] ;
16
16
}
17
17
18
18
const StickyScrollBar : React . ForwardRefRenderFunction < unknown , StickyScrollBarProps > = (
19
- { scrollBodyRef, onScroll, offsetScroll, container, data } ,
19
+ { scrollBodyRef, onScroll, offsetScroll, container } ,
20
20
ref ,
21
21
) => {
22
22
const prefixCls = useContext ( TableContext , 'prefixCls' ) ;
@@ -40,6 +40,14 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
40
40
x : 0 ,
41
41
} ) ;
42
42
const [ isActive , setActive ] = React . useState ( false ) ;
43
+ const rafRef = React . useRef < number | null > ( null ) ;
44
+
45
+ React . useEffect (
46
+ ( ) => ( ) => {
47
+ raf . cancel ( rafRef . current ) ;
48
+ } ,
49
+ [ ] ,
50
+ ) ;
43
51
44
52
const onMouseUp : React . MouseEventHandler < HTMLDivElement > = ( ) => {
45
53
setActive ( false ) ;
@@ -82,30 +90,32 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
82
90
} ;
83
91
84
92
const checkScrollBarVisible = ( ) => {
85
- if ( ! scrollBodyRef . current ) {
86
- return ;
87
- }
88
- const tableOffsetTop = getOffset ( scrollBodyRef . current ) . top ;
89
- const tableBottomOffset = tableOffsetTop + scrollBodyRef . current . offsetHeight ;
90
- const currentClientOffset =
91
- container === window
92
- ? document . documentElement . scrollTop + window . innerHeight
93
- : getOffset ( container ) . top + ( container as HTMLElement ) . clientHeight ;
94
-
95
- if (
96
- tableBottomOffset - getScrollBarSize ( ) <= currentClientOffset ||
97
- tableOffsetTop >= currentClientOffset - offsetScroll
98
- ) {
99
- setScrollState ( state => ( {
100
- ...state ,
101
- isHiddenScrollBar : true ,
102
- } ) ) ;
103
- } else {
104
- setScrollState ( state => ( {
105
- ...state ,
106
- isHiddenScrollBar : false ,
107
- } ) ) ;
108
- }
93
+ rafRef . current = raf ( ( ) => {
94
+ if ( ! scrollBodyRef . current ) {
95
+ return ;
96
+ }
97
+ const tableOffsetTop = getOffset ( scrollBodyRef . current ) . top ;
98
+ const tableBottomOffset = tableOffsetTop + scrollBodyRef . current . offsetHeight ;
99
+ const currentClientOffset =
100
+ container === window
101
+ ? document . documentElement . scrollTop + window . innerHeight
102
+ : getOffset ( container ) . top + ( container as HTMLElement ) . clientHeight ;
103
+
104
+ if (
105
+ tableBottomOffset - getScrollBarSize ( ) <= currentClientOffset ||
106
+ tableOffsetTop >= currentClientOffset - offsetScroll
107
+ ) {
108
+ setScrollState ( state => ( {
109
+ ...state ,
110
+ isHiddenScrollBar : true ,
111
+ } ) ) ;
112
+ } else {
113
+ setScrollState ( state => ( {
114
+ ...state ,
115
+ isHiddenScrollBar : false ,
116
+ } ) ) ;
117
+ }
118
+ } ) ;
109
119
} ;
110
120
111
121
const setScrollLeft = ( left : number ) => {
@@ -119,6 +129,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
119
129
120
130
React . useImperativeHandle ( ref , ( ) => ( {
121
131
setScrollLeft,
132
+ checkScrollBarVisible,
122
133
} ) ) ;
123
134
124
135
React . useEffect ( ( ) => {
@@ -156,11 +167,6 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
156
167
}
157
168
} , [ scrollState . isHiddenScrollBar ] ) ;
158
169
159
- // The best way is to use ResizeObserver to detect the body height, but this way is enough
160
- React . useEffect ( ( ) => {
161
- checkScrollBarVisible ( ) ;
162
- } , [ data ] ) ;
163
-
164
170
if ( bodyScrollWidth <= bodyWidth || ! scrollBarWidth || scrollState . isHiddenScrollBar ) {
165
171
return null ;
166
172
}
0 commit comments