The ecosystem · packages/virtual
Lists of any length
A thousand rows or a million cost the page the same: only the rows inside the box are rendered, plus four more on each side. Scrolling makes the rows that come into view and drops those that leave; the rest stay as they are.
Use <VirtualList> for a list too long to render whole: a log, every order, a grid of a million checkboxes. Every row must have the same height, and the list scrolls inside a box of its own.
Installing it#
Name the package in the app's header:
app [program] { pf: platform "…/server/platform/main.roc", virtual: "…/packages/virtual/main.roc" }Then import virtual.VirtualList in a .rocx file. It isn't a plugin and adds nothing to the server. rocx generate (which rocx dev and rocx build run first) copies the component into the app (Generated/virtual/), as for the charts.
It is built from two platform features any component can use: <Repeat> renders a range of rows by index (if, match and .map), and on:viewport tells a handler which part of an element shows (handlers).
A million rows#
Each row is a number and its square. Pick a count and scroll; the line above the list says which rows are on the page.
The row is a function child: given an index, it returns the row's markup. The demo's list, in part:
import virtual.Rows
import virtual.VirtualList
<VirtualList count={count!()} row_height={36} class="rows-demo-list" label="Numbers and their squares" on_view!={saw!}>
{|row| <div class="rows-demo-row"><span>{RowsDemo.thousands(row + 1)}</span><span>{RowsDemo.thousands((row + 1) * (row + 1))}</span></div>}
</VirtualList>Its props#
| Prop | What it is |
|---|---|
count | How many rows there are. Like every value prop of an effectful component, it is a getter: when count={rows!()} changes, the list follows. |
row_height | Each row's height, in pixels. Every row is this high. |
overscan | How many rows to render beyond those that show, on each side, so a quick scroll shows rows instead of a gap. 4 unless you set it. |
first_height | The box height, in pixels, to render rows for before the box is measured: on the server, and until the page hydrates. 800 unless you set it. |
class | Classes for the box, added to virtual-list. Give the box its height here. |
label | The box's aria-label. Left out when empty. |
on_view! | Called with each measure of the box, an Html.Viewport: top, left, width and height, in pixels. |
| the child | {|index| …}: a row, given its index from 0. It runs once, when the row is made, as an effectful component does. |
The scrolling box#
The list is a <div> that scrolls (overflow-y: auto), so give it a height through its class. Inside, a second <div> as tall as all the rows (count × row_height pixels) gives the scrollbar its full length; the rendered rows sit in it at their places.
A box with no height grows to fit all its rows and never scrolls, so the list renders only the rows in the first 4,096 pixels and leaves the rest blank.
On the server#
The server can't measure the box, so it renders the rows the first first_height pixels would show, and the browser hydrates the same rows. Then on:viewport measures the box, and the rows that really show replace them. So the first rows are in the page's HTML before any wasm runs.
Writing signals from on_view!#
on_view! runs on every scroll, at most once per animation frame. A signal's setter doesn't compare values (a memo does): every write re-runs whatever reads the signal, changed or not. So write a signal from on_view! only when the part of the view you use has changed. The checkboxes example fits as many boxes in a row as the list's width allows, and writes the column count only when it changes:
on_view!={|view| if (Grid.columns(view.width) != columns!()) set_columns!(Grid.columns(view.width)) else {}}The list does the same: it writes its view only when the range of rows changes, so scrolling within a row writes nothing.
Lists taller than a browser allows#
Browsers cap an element's height: Firefox at about 17.8 million pixels, Chromium at about 33.5 million. The demo's million 36-pixel rows are 36 million. So a list taller than 10 million pixels is scaled: its inner <div> is 10 million pixels tall, and each pixel scrolled moves through more than a pixel of rows, so scrolling to the end shows the last row. The row at the box's top is always placed where it should be.
Limits#
- Not yet: rows of different heights, measured as they show.
- One column. For a grid, lay out each row yourself: the checkboxes example puts a
<Repeat>of boxes in each row, as many as fit the widthon_view!reports. - In a scaled list, the scrollbar reaches to within a few rows of any row, not to one exact pixel.
The package's README has the full reference, including Rows, the plain-Roc arithmetic behind the list.