Skip to the code

Visible rows in a virtual list

The tea shop's order history has 10,000 rows, and about ten fit in its scroll box at once. A virtual list renders only those, plus a few on each side, inside a spacer as tall as the whole list. Write visibleRange({ scrollTop, viewportHeight, rowHeight, rowCount, overscan }), the math that picks those rows. Every row is rowHeight pixels tall, the box shows viewportHeight pixels of the list from scrollTop down, and overscan is 3 when it isn't given. The React problem Virtualized list (virtualized-list) builds the whole component; this is the counting at its heart.

Return { start, end, offsetTop, totalHeight }. Row i covers the pixels from i * rowHeight up to, but not including, (i + 1) * rowHeight, and the box shows the pixels from scrollTop up to, but not including, scrollTop + viewportHeight. A row is in view when it shares at least one pixel with the box, so a row that starts exactly at the box's bottom edge isn't. start is the first row in view minus overscan, never below 0, and end is the last row in view plus overscan, never past rowCount - 1. offsetTop is where row start begins, start * rowHeight, and totalHeight is the spacer's height, rowCount * rowHeight.

First clamp scrollTop to the positions the box can really scroll to: from 0 to totalHeight - viewportHeight, or just 0 when the list is shorter than the box. Elastic scrolling, as on iOS, can report a position below 0 or past the end for a moment while a list bounces, and the rows you return must still exist. scrollTop can have a fraction, on a zoomed page or a high-density screen. An empty list renders nothing: return { start: 0, end: -1, offsetTop: 0, totalHeight: 0 }, so a loop from start to end runs no times.

visibleRange({ scrollTop: 4500, viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 2 });
// Rows 150 to 159 are in view, so with 2 more on each side:
// { start: 148, end: 161, offsetTop: 4440, totalHeight: 300000 }

What the tests check

  1. renders the rows in view at the top of the list
    expect(visibleRange({ scrollTop: 0, viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 })).toEqual({ start: 0, end: 9, offsetTop: 0, totalHeight: 300000 });
    
  2. a row partly in view counts, and a row that starts at the bottom edge doesn't
    const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 };
    expect(visibleRange({ ...orders, scrollTop: 45 })).toEqual({ start: 1, end: 11, offsetTop: 30, totalHeight: 300000 });
    expect(visibleRange({ ...orders, scrollTop: 60 })).toEqual({ start: 2, end: 11, offsetTop: 60, totalHeight: 300000 });
    
  3. adds overscan rows on each side
    expect(visibleRange({ scrollTop: 4500, viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 2 })).toEqual({ start: 148, end: 161, offsetTop: 4440, totalHeight: 300000 });
    
  4. overscan is 3 unless given, and stops at both ends of the list
    const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000 };
    expect(visibleRange({ ...orders, scrollTop: 0 })).toEqual({ start: 0, end: 12, offsetTop: 0, totalHeight: 300000 });
    expect(visibleRange({ ...orders, scrollTop: 299700 })).toEqual({ start: 9987, end: 9999, offsetTop: 299610, totalHeight: 300000 });
    
  5. a scroll position the box can't have is clamped first
    const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 };
    expect(visibleRange({ ...orders, scrollTop: -40 })).toEqual({ start: 0, end: 9, offsetTop: 0, totalHeight: 300000 });
    expect(visibleRange({ ...orders, scrollTop: 400000 })).toEqual({ start: 9990, end: 9999, offsetTop: 299700, totalHeight: 300000 });
    
  6. a list shorter than its box renders every row
    expect(visibleRange({ scrollTop: 20, viewportHeight: 300, rowHeight: 30, rowCount: 5, overscan: 2 })).toEqual({ start: 0, end: 4, offsetTop: 0, totalHeight: 150 });
    
  7. an empty list renders nothing
    expect(visibleRange({ scrollTop: 0, viewportHeight: 300, rowHeight: 30, rowCount: 0 })).toEqual({ start: 0, end: -1, offsetTop: 0, totalHeight: 0 });
    
  8. a scroll position with a fraction still finds every row it touches
    const orders = { viewportHeight: 300, rowHeight: 30, rowCount: 10000, overscan: 0 };
    expect(visibleRange({ ...orders, scrollTop: 59.5 })).toEqual({ start: 1, end: 11, offsetTop: 30, totalHeight: 300000 });
    expect(visibleRange({ ...orders, scrollTop: 60.5 })).toEqual({ start: 2, end: 12, offsetTop: 60, totalHeight: 300000 });
    

Learn more

solution.jsSaved in this browser

Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.

Ctrl + Enter runs the tests (⌘ + Enter on a Mac).