Skip to content

Commit e4812f9

Browse files
committed
Update VerticalScrollbar.tsx
1 parent bedf764 commit e4812f9

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

packages/main/src/components/AnalyticalTable/scrollbars/VerticalScrollbar.tsx

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isChrome as isChromeFn } from '@ui5/webcomponents-react-base/Device';
22
import { useSyncRef } from '@ui5/webcomponents-react-base/internal/hooks';
3+
import { debounce } from '@ui5/webcomponents-react-base/internal/utils/debounce';
34
import { clsx } from 'clsx';
45
import type { MutableRefObject } from 'react';
56
import { forwardRef, useEffect, useRef, useState } from 'react';
@@ -20,15 +21,28 @@ const isChrome = isChromeFn();
2021
export 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

Comments
 (0)