clauderic / react-tiny-virtual-list

A tiny but mighty 3kb list virtualization library, with zero dependencies 💪 Supports variable heights/widths, sticky items, scrolling to index, and more!
https://clauderic.github.io/react-tiny-virtual-list/
MIT License
2.47k stars 164 forks source link

get height from element style [feature request] #50

Closed damianobarbati closed 6 years ago

damianobarbati commented 6 years ago

What about adding the possibility to not give an "height" prop and derive it from element.style.height?

Use case:

I already deal with height in my classes for proper UI and it's redundant to add the logic twice into renderer.

@withWindowSize //HOC passing window height and width
@injectSheet(theme => ({
   tbody: ({ windowHeight }) => ({
      width: '100%',
      height: windowHeight - headerHeight - theadHeight,
   }),
}))
export default class Table extends React.Component {
....
       render = () => {
            const { windowHeight, classes } = this.props; //windowHeight from HOC not needed!
            const { items } = this;

             const tbodyHeight = windowHeight - headerHeight - theadHeight; //AGAIN!

             return (
                 <VirtualList
                    className={classes.tbody} //here element has already proper style.height applied
                    height={tbodyHeight}
                    .... 
                  />
              );
         }
}
clauderic commented 6 years ago

The height needs to be known in advance, as this is how the height of the scrollable list can be calculated.

Reading from computed style would be slow and expensive, as you would need to wait for the DOM node to be mounted to know it's computed height.

Venryx commented 5 years ago

Personally I prefer the approach described here: https://github.com/coderiety/react-list#itemsizeestimatorindex-cache

What it does is that:

I know react-tiny-virtual-list has the estimatedItemSize prop, which is similar. However, it's not as good because it's a "one number for every item" property, instead of an estimator-function which can adjust based on, for example, the number of characters in a chat message, to get a more accurate height estimate (thus causing less scroll janking).