@@ -6,17 +6,17 @@ import getScrollBarSize from 'rc-util/lib/getScrollBarSize';
66import * as React from 'react' ;
77import TableContext from './context/TableContext' ;
88import { useLayoutState } from './hooks/useFrame' ;
9+ import raf from 'rc-util/lib/raf' ;
910
1011interface StickyScrollBarProps {
1112 scrollBodyRef : React . RefObject < HTMLDivElement > ;
1213 onScroll : ( params : { scrollLeft ?: number } ) => void ;
1314 offsetScroll : number ;
1415 container : HTMLElement | Window ;
15- data ?: readonly any [ ] ;
1616}
1717
1818const StickyScrollBar : React . ForwardRefRenderFunction < unknown , StickyScrollBarProps > = (
19- { scrollBodyRef, onScroll, offsetScroll, container, data } ,
19+ { scrollBodyRef, onScroll, offsetScroll, container } ,
2020 ref ,
2121) => {
2222 const prefixCls = useContext ( TableContext , 'prefixCls' ) ;
@@ -40,6 +40,14 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
4040 x : 0 ,
4141 } ) ;
4242 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+ ) ;
4351
4452 const onMouseUp : React . MouseEventHandler < HTMLDivElement > = ( ) => {
4553 setActive ( false ) ;
@@ -82,30 +90,32 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
8290 } ;
8391
8492 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+ } ) ;
109119 } ;
110120
111121 const setScrollLeft = ( left : number ) => {
@@ -119,6 +129,7 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
119129
120130 React . useImperativeHandle ( ref , ( ) => ( {
121131 setScrollLeft,
132+ checkScrollBarVisible,
122133 } ) ) ;
123134
124135 React . useEffect ( ( ) => {
@@ -156,11 +167,6 @@ const StickyScrollBar: React.ForwardRefRenderFunction<unknown, StickyScrollBarPr
156167 }
157168 } , [ scrollState . isHiddenScrollBar ] ) ;
158169
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-
164170 if ( bodyScrollWidth <= bodyWidth || ! scrollBarWidth || scrollState . isHiddenScrollBar ) {
165171 return null ;
166172 }
0 commit comments