Lists
A screen that renders every item of a long collection pays for all of them: PHP builds each element, the host lays each one out and the platform creates a view for each. VirtualizedList avoids that. PHP builds only the rows near the viewport, and the host asks for more as the user scrolls, so a list of thousands of items costs what is on screen. Use it for any collection that can grow beyond a screenful.
A virtualized list
Section titled “A virtualized list”use Pam\Desktop\Align;use Pam\Desktop\Style;use Pam\Desktop\UI\Row;use Pam\Desktop\UI\Text;use Pam\Desktop\UI\VirtualizedList;
VirtualizedList::of( $this->orders, static fn (Order $order): Row => Row::make( Text::make($order->customer)->style(new Style(flexGrow: 1)), Text::make($order->total), )->style(new Style(height: 56, paddingHorizontal: 14, alignItems: Align::Center)), static fn (Order $order): string => "order-{$order->id}",) ->rowHeight(56) ->onEndReached(fn () => $this->loadMore()) ->style(new Style(flexGrow: 1));of($items, $row, $key) takes the data, a closure that builds the row for one item (it also receives the index) and a closure that returns a stable key. Without a key closure the key is the item’s key or id, then its index. Stable keys let rows keep their native view when the data changes.
In a template, repeat the rows inside the list. p-for children of a VirtualizedList are built lazily, and inverted="true" works as in PHP:
<VirtualizedList estimatedRowHeight="44" class="orders"> <Row p-for="$order in $orders" p-key="$order->id" class="order"> <Text class="customer">{{ $order->customer }}</Text> <Text class="total">{{ $order->total }}</Text> </Row></VirtualizedList>Sizing
Section titled “Sizing”A vertical list without a height is sized by its rows, like a CSS block with overflow: auto: its height is the rows already measured plus rowHeight() for each row not built yet, up to max-height (or the window height). The list shrinks by default, so in a column with a definite height it takes the space its siblings leave, and flex-grow: 1 makes it fill that space. An empty list has no height.
// A card that follows its rows: three rows make it short; many rows stop at the window height and scroll.Column::make( Text::make('Orders'), VirtualizedList::of($orders, $row, $key)->rowHeight(44),);Give rowHeight() the real height of a row whenever rows have a fixed height. It is also the estimate for rows not measured yet, so a good value keeps the scrollbar steady.
Options
Section titled “Options”| Method | Meaning |
|---|---|
rowHeight(float) |
Height of a row, or the estimate for rows without an authored height. |
columns(int) |
A grid with that many columns. |
horizontal() |
A horizontal list. |
inverted() |
Row 0 rests at the bottom and older rows go up, as in a conversation. |
initialScrollIndex(int) |
The row the list opens at. |
scrollRequest($serial, targetOffset: $y) |
Scrolls from code each time $serial changes. |
onScroll, onEndReached |
The offset changed; the end is near. |
refreshing(bool), onRefresh |
Refresh with F5 or Ctrl+R (Cmd+R). |
Pagination
Section titled “Pagination”onEndReached fires when the user scrolls near the end. Load the next page there, and guard against overlapping requests:
#[State]public array $orders = [];
private bool $loading = false;private int $page = 0;
public function loadMore(): void{ if ($this->loading) { return; } $this->loading = true;
SQLite::query( 'shop.db', 'SELECT id, customer, total FROM orders ORDER BY id DESC LIMIT 100 OFFSET ?', [$this->page * 100], function (array $rows): void { $this->orders = [...$this->orders, ...$rows]; $this->page++; $this->loading = false; }, );}SQLite returns at most 1000 rows per query, so paginating is required for large tables anyway.
columns() turns the list into a virtualized grid, and VirtualGrid::of($columns, $items, $cell) is a shortcut:
use Pam\Desktop\UI\VirtualGrid;
VirtualGrid::of(4, $this->photos, fn (Photo $photo): Image => Image::make($photo->thumbnailUrl) ->fit(ImageFit::Cover) ->style(new Style(height: 160, borderRadius: 10))) ->rowHeight(170) ->style(new Style(flexGrow: 1));Request thumbnails, not originals: an image is decoded no larger than its box, but the download is the whole file.
Chats and inverted lists
Section titled “Chats and inverted lists”inverted() keeps row 0 at the bottom, so a conversation opens on the newest message and older messages are loaded as the user scrolls up through onEndReached:
VirtualizedList::of($this->messages, $this->messageRow(...), static fn (Message $m): string => $m->id) ->inverted() ->onEndReached(fn () => $this->loadOlder()) ->style(new Style(flexGrow: 1));Keep $this->messages newest first.
Refresh
Section titled “Refresh”There is no pull gesture on desktop. F5 or Ctrl+R (Cmd+R) asks the visible list with onRefresh to refresh, and the host shows a spinner while refreshing is true:
VirtualizedList::of($this->orders, $row, $key) ->refreshing($this->refreshing) ->onRefresh(function (): void { $this->refreshing = true; $this->reload(fn () => $this->refreshing = false); });refreshEnabled(false), refreshTintColor(), refreshIndicatorSize() and progressViewOffset() adjust it.
Simple lists
Section titled “Simple lists”FlatList::make([...]) shows plain strings, and SectionList::make(['Title' => [...]]) groups them under headers. VirtualizedList::make(...) takes ready-made elements instead of data. Prefer of() for anything driven by data.
What the host does for you
Section titled “What the host does for you”- Rows outside the viewport are never built in PHP or mounted in the host.
- The worker sends rows in windows as the user scrolls; scrolling itself runs in the native scroll view without PHP.
- In software rendering, scrolling rows are drawn from cached images and released a second after the scroll ends.
- A row group of a CSS table can be a
VirtualizedList; only mounted rows are measured.
Next steps
Section titled “Next steps”- Performance for the rest of the cost model.
- Media & Web for image caching in lists.