11import { isChrome as isChromeFn } from '@ui5/webcomponents-react-base/Device' ;
22import { useSyncRef } from '@ui5/webcomponents-react-base/internal/hooks' ;
3+ import { debounce } from '@ui5/webcomponents-react-base/internal/utils/debounce' ;
34import { clsx } from 'clsx' ;
45import type { MutableRefObject } from 'react' ;
56import { forwardRef , useEffect , useRef , useState } from 'react' ;
@@ -20,15 +21,28 @@ const isChrome = isChromeFn();
2021export const VerticalScrollbar = forwardRef < HTMLDivElement , VerticalScrollbarProps > ( ( props , ref ) => {
2122 const { internalRowHeight, tableRef, tableBodyHeight, scrollContainerRef, classNames } = props ;
2223 const [ hasHorizontalScrollbar , setHasHorizontalScrollbar ] = useState ( false ) ;
23- const horizontalScrollbarSectionStyles = clsx ( hasHorizontalScrollbar && classNames . bottomSection ) ;
2424 const [ componentRef , scrollbarRef ] = useSyncRef < HTMLDivElement > ( ref ) ;
2525 const contentRef = useRef < HTMLDivElement > ( null ) ;
2626
2727 useEffect ( ( ) => {
28- if ( tableRef ?. current ) {
29- const hasScrollbar = tableRef . current . offsetWidth !== tableRef . current . scrollWidth ;
30- setHasHorizontalScrollbar ( hasScrollbar ) ;
31- }
28+ const tableElement = tableRef ?. current ;
29+ if ( ! tableElement ) return ;
30+
31+ const debouncedCheckScrollbar = debounce ( ( ) => {
32+ requestAnimationFrame ( ( ) => {
33+ const hasScrollbar = tableElement . offsetWidth !== tableElement . scrollWidth ;
34+ setHasHorizontalScrollbar ( hasScrollbar ) ;
35+ } ) ;
36+ } , 100 ) ;
37+
38+ const resizeObserver = new ResizeObserver ( debouncedCheckScrollbar ) ;
39+ resizeObserver . observe ( tableElement ) ;
40+ debouncedCheckScrollbar ( ) ;
41+
42+ return ( ) => {
43+ debouncedCheckScrollbar . cancel ( ) ;
44+ resizeObserver . disconnect ( ) ;
45+ } ;
3246 } , [ tableRef ] ) ;
3347
3448 // Force style recalculation to fix Chrome scrollbar-color bug (track height not updating correctly)
@@ -83,7 +97,7 @@ export const VerticalScrollbar = forwardRef<HTMLDivElement, VerticalScrollbarPro
8397 className = { classNames . verticalScroller }
8498 />
8599 </ div >
86- < div className = { horizontalScrollbarSectionStyles } />
100+ < div className = { clsx ( hasHorizontalScrollbar && classNames . bottomSection ) } />
87101 </ FlexBox >
88102 ) ;
89103} ) ;
0 commit comments