primereact icon indicating copy to clipboard operation
primereact copied to clipboard

VirtualScroller: Flickering in certain circumstances

Open sleepybjr opened this issue 2 years ago • 5 comments

Describe the bug

I'm using the DataTable with virtual scrolling and it is flickering for me. I have a big data table with many columns and it flickers when scrolling. It's hard to recreate in codesandbox, but it should be sufficient was you scroll.

I believe it's occurring due to this line: https://github.com/primefaces/primereact/blob/7d5a8d12953e3d70303c09134c0a02e3209b3605/components/lib/virtualscroller/VirtualScroller.js#L252

This line sets a new style (translate3d) for the next data chunk, which causes the table to display it. Then, it's corrected after.

Usually this is fast enough to not be seen, but in my instance, I think it's due to using body calls in the DataTable, it slows this process down and causes flickering.

This might be solvable by immediately setting the table back to the correct position after updating the translate3d style, but I am experimenting with it and haven't been able to find a fix.

Reproducer

https://codesandbox.io/s/primereact-test-forked-cwr1db

PrimeReact version

8.0.1

React version

18.x

Language

ES6

Build / Runtime

Next.js

Browser(s)

No response

Steps to reproduce the behavior

  1. Make a DataTable with a lot of dynamic body template columns.
  2. Scroll the table and notice flickering.

Expected behavior

Table should not flicker.

sleepybjr avatar May 07 '22 02:05 sleepybjr

I'm facing the same problem. The weird thing is it's only in production. I added transition-delay: 0.2s; to tbody and the problem seemed to go away, at least for the time being. So, this could be a temporary workaround.

I'm using PrimeReact 8.3.0 and Preact 10.10.1.

Edit: It's still flickering in some cases. Still, it's much better than before.

ttongsul avatar Sep 22 '22 01:09 ttongsul

I found a better workaround.

First, create three refs:

const dataTableRef = useRef(null); // DataTable ref
const startScrollIndex = useRef(0);
const endScrollIndex = useRef(0);

Put itemSize into a variable: const itemSize = 32;

Then, use the following virtualScrollerOptions:

virtualScrollerOptions={{
  itemSize,
  onScrollIndexChange: ({ first }) => {
    const tbodyEl = dataTableRef.current?.getTable()?.querySelector('tbody') ?? null;

    if (tbodyEl === null) {
      return;
    }

    tbodyEl.style.transform = `translate3d(0px, ${startScrollIndex.current * itemSize}px, 0px)`;

    if (endScrollIndex.current !== startScrollIndex.current) {
      endScrollIndex.current = first;
      return;
    }

    endScrollIndex.current = first;

    const observer = new MutationObserver(() => {
      observer.disconnect();
      startScrollIndex.current = endScrollIndex.current;
      tbodyEl.style.transform = `translate3d(0px, ${endScrollIndex.current * itemSize}px, 0px)`;
    });
    observer.observe(tbodyEl, { childList: true });
  },
}}

No flickering so far.

ttongsul avatar Sep 22 '22 09:09 ttongsul

I assigned this to @mertsincan to review much appreciated for your debugging and workaround!

melloware avatar Sep 22 '22 12:09 melloware

Hello, I can observe a similar behaviour of flickering scrollbar when I try use DataTable with VirtualScroller with itemSize set to fixed size which I later override in css styles. In my case I have rows which can have a huge content then the height of rows cannot be constant. Proposed workaround is not working for me

PrimeReact version 9.2.0

React version 17.x

Language ES6

Pabson avatar Mar 14 '23 12:03 Pabson