virtual: Dynamic row height not working in Grid

Describe the bug

When cells within each row have varying size content, I would expect the height of the row (and in turn each cell) to be the height of the largest cell in the row. However, it seems that this is not working, unless I’m doing something incorrectly. I’ve read through several issues related to dynamically sizing rows (https://github.com/TanStack/virtual/issues/376, https://github.com/TanStack/virtual/issues/235, https://github.com/TanStack/virtual/issues/28) but they are all either out-dated, closed without resolution, or not quite the same use case. In the codesandbox example, i’m virtualizing the columns but forcing them all to render in order to illustrate this issue. You can see that each cell within the row is measured correctly according to the content, but I think the height of each cell should be adjusted to account for the maximum cell height, right?

Your minimal, reproducible example

https://codesandbox.io/s/window-scroll-virtualized-ltfdib?file=/src/main.tsx

Steps to reproduce

  1. See the codesandbox repro

Expected behavior

Height of each cell should be Math.max(...cellHeights)

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOs
  • Browser: Chrome 107

tanstack-virtual version

3.0.0-beta.23

TypeScript version

No response

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project’s Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15

Most upvoted comments

After thoroughly looking at the example provided here I realized that I was missing a data-index={virtualRow.index} attribute on <tr/> element which is required for virtualizer to provide accurate translateY pixels to the row element. Now it is dynamically adjusting row translate respective to it’s height as expected. Thanks for acknowledgement and Big thanks! to the TanStack team for such a powerfull package.

I used to use @tanstack/react-query for qureying backend and I started trying more packages from Tanstack after the new documentation release and now I can’t avoid using:-

  • @tanstack/react-table
  • @tanstack/react-router
  • @tanstack/react-virtual
  • @tanstack/react-query
  • @tanstack/react-form

All of these packages are great. Specially the @tanstack/react-table in combination with @tanstack/react-virtual is a game changer.

I read the whole thread patiently, including the existing threads related to this issue. I am facing a similar problem in useVirtualizer hook where I do not know why measureElement is never triggered.

const rowVirtualizer = useVirtualizer({
    count: rows.length,
    estimateSize: () => 60, //estimate row height for accurate scrollbar dragging
    getScrollElement: () => tableContainerRef.current,
    measureElement:
      (element) => {
        console.log("measuring element");
        return element?.getBoundingClientRect().height;
      },
    overscan: 5,
  });
<tr ref={(node) => rowVirtualizer.measureElement(node)} > {content} </tr>

I tested it many times but measureElement function is not called even once and virtualizer uses estimated size for all rows.

@jamesonhill maybe go with a bit different approach, first position rows, then render columns as one block, thanks to minHeight: row.size + ResizeObserver there is no need for custom measureElementMaxSize to store max size.

Checkout this https://codesandbox.io/s/window-scroll-virtualized-forked-pzebvz?file=/src/main.tsx