Getting started

Actions

Forms

Layout

Navigation

Overlays

Feedback

Data display

Controllers

Helpers

Recipes

Virtual range

The offsets, visible range and scroll-to position of a virtualized list, from item sizes.

Reach for it to build a virtualizer of your own, or to work out a range on the server; VirtualizerController is built on it. The helpers are pure: no DOM at import time or in the call path, so they run on the server and in tests. Each result below is computed when the page is built, by running the call shown.

Import

import { alignVirtualOffset, findVirtualRange, virtualOffsets } from '@ecopages/radiant-ui/helpers';

Offsets

virtualOffsets(sizes) lays the items end to end and returns count + 1 offsets: item i starts at offsets[i], and the last offset is the total.

CallResult
virtualOffsets([10, 20, 30])[0,10,30,60]
virtualOffsets([])[0]

Range

findVirtualRange(offsets, view, overscan) returns the first index to render and one past the last: the items that overlap the view, plus overscan on each side, clamped to the list. view is { start, size }, the scroll offset and the viewport size. It searches the offsets by halves, so a long list costs a few steps. With 100 items of 20px:

CallResult
findVirtualRange(offsets, { start: 200, size: 100 }){"start":10,"end":15}
findVirtualRange(offsets, { start: 210, size: 100 }, 2){"start":8,"end":18}
findVirtualRange(offsets, { start: 1900, size: 100 }, 2){"start":93,"end":100}

Align

alignVirtualOffset(item, align, view, total) returns the scroll offset that puts an item at the start, center or end of the view, clamped to the scrollable range. auto leaves an item that is fully in view where it is, and otherwise takes the nearer edge. For a 20px item at 1000px in a 100px view over 2000px:

CallResult
alignVirtualOffset({ start: 1000, size: 20 }, 'center', { start: 0, size: 100 }, 2000)960
alignVirtualOffset({ start: 1000, size: 20 }, 'end', { start: 0, size: 100 }, 2000)920
alignVirtualOffset({ start: 1000, size: 20 }, 'auto', { start: 950, size: 100 }, 2000)950
alignVirtualOffset({ start: 1990, size: 20 }, 'start', { start: 0, size: 100 }, 2000)1900