diff --git a/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx b/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx index 82ca9192066..f8bf1c16491 100644 --- a/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx +++ b/packages/grafana-ui/src/components/Table/TableNG/TableNG.tsx @@ -171,7 +171,7 @@ export function TableNG(props: TableNGProps) { [theme, visibleFields, cellHeight] ); const gridRef = useRef(null); - const scrollbarWidth = useScrollbarWidth(gridRef, height, sortedRows); + const scrollbarWidth = useScrollbarWidth(gridRef, height); const availableWidth = useMemo( () => (hasNestedFrames ? width - COLUMN.EXPANDER_WIDTH : width) - scrollbarWidth, [width, hasNestedFrames, scrollbarWidth] diff --git a/packages/grafana-ui/src/components/Table/TableNG/hooks.ts b/packages/grafana-ui/src/components/Table/TableNG/hooks.ts index 47efa394409..84b9a277dad 100644 --- a/packages/grafana-ui/src/components/Table/TableNG/hooks.ts +++ b/packages/grafana-ui/src/components/Table/TableNG/hooks.ts @@ -534,16 +534,28 @@ export function useColumnResize( return dataGridResizeHandler; } -export function useScrollbarWidth(ref: RefObject, height: number, renderedRows: TableRow[]) { +export function useScrollbarWidth(ref: RefObject, height: number) { const [scrollbarWidth, setScrollbarWidth] = useState(0); useLayoutEffect(() => { const el = ref.current?.element; - if (el) { - setScrollbarWidth(el.offsetWidth - el.clientWidth); + if (!el) { + return; } - }, [ref, height, renderedRows]); + + const updateScrollbarDimensions = () => { + setScrollbarWidth(el.offsetWidth - el.clientWidth); + }; + + updateScrollbarDimensions(); + + const resizeObserver = new ResizeObserver(updateScrollbarDimensions); + resizeObserver.observe(el); + return () => { + resizeObserver.disconnect(); + }; + }, [ref, height]); return scrollbarWidth; }