Skip to content

List

createList is the frame around any list of items. It owns the layout, the height, and the empty and loading states, and a render function owns each item's content, so a list of media cards, text, or any other component behaves the same way.

With key, setItems keeps every item whose key and content are unchanged and draws only the rest, so a list that refreshes on a timer keeps its scroll position and focus. A boxed row (stolon-item-row) whose text has a stolon-item-row-primary and a stolon-item-row-secondary part shares the widest primary width across the list, so every secondary text starts at the same place, and wraps under the primary on a narrow row. The List also owns the one selected look every list shares, the accent wash with a leading bar.

Decision

When Use
Any list of items a plugin builds, such as a queue, a set of results, or a history List
A list that needs a fixed height, a height that fills the window, or more items as the user scrolls List
Items the reader searches, filters, sorts, or selects Filtered List
Rows with columns that sort Data Table
Rows a server pages through Paginated Table
Items the reader reorders or edits A Sortable Card List or an Ordered List

Usage

import { createList, createMediaCard } from './configurationpage?name=myplugin_stolon.js';

var list = createList({
    items: rows,
    render: function (row) { return createMediaCard({ title: row.name, subtitle: row.state }); },
    height: 'fill',
    emptyText: 'Nothing in the queue.'
});
container.appendChild(list.element);

render returns text, a node, or a component handle such as a media card's. Text is shown as text, never as markup. A handle's destroy runs when its item is replaced.

Classes: stolon-list, stolon-list-item. The handle is { element, emptyElement, getItems, setItems, append, removeFirst, setHeight, setLoading, scrollToStart, scrollToEnd, cellAt, destroy }. removeFirst(count) drops the oldest items, such as the head of a capped log, and cellAt(index) returns the element that holds an item.

Heights

height What it does
'auto' (default) Grows with its items, and the page scrolls.
A number or a CSS length, such as 240 or '40vh' A fixed height that scrolls inside.
'fill' Reaches from the list's top to the bottom of the window, then scrolls inside. A list that starts below the first screen is as tall as the window. Inside a bounded flex column, it also takes the room left.
  • maxHeight lets a growing list grow up to a cap, then scroll inside, so a short list stays short.
  • minHeight is the least a filling list takes, 160px by default. fillGap is the room kept below it, 16px by default.
  • setHeight(height, maxHeight) switches modes later.

States and Modifiers

  • layout is rows (default), bordered rows with a divider between items, grid, tiles in as many columns as fit with minColumn as the least width, or plain, with no frame.
  • emptyText is "Nothing to show." by default. setLoading(true) shows loadingText, "Loading" by default, as a status line.
  • A ul or ol list, or one with emptyOutside: true, keeps its empty sentence outside itself as stolon-list-empty outside. The caller places handle.emptyElement after the list, and the list shows and hides it. A ul or ol list's loading line is an li.
  • A component that shows an empty sentence passes it as emptyText rather than building its own, so one rule styles every empty list.
  • onEndReached runs when the end comes into view, so a page can load more and call append. It watches the list when it scrolls inside, and the window when it grows, and waits while the list is loading.
  • element makes an element the page already has into the list, keeping its own classes.
  • key names an item by a property or a function. Without one, setItems draws every item again.
  • label sets the list's aria-label. role replaces the list role, and null sets none, for an element that already carries its meaning.
  • wrap: false makes each rendered node the item itself rather than wrapping it in a stolon-list-item cell, so a row keeps its place in the markup.

Gotchas

  • A filling list measures its place again when the window resizes or the page changes height. A list hidden when it measures takes its minHeight until it shows.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createList

function createList<T = any>(options: ListOptions<T>): ListHandle<T>

Builds a list. The list is the frame, its height, and its empty and loading states. Each item's content comes from render, so any Stolon component, such as a media card, can sit inside it. With key, setItems keeps every item whose key and content are unchanged and draws only the rest, so a list that refreshes on a timer keeps its scroll position and focus.

Option Type What it does
items T[] | null
render (required) (item: T, index: number): ListContent
element HTMLElement | null
layout ListLayout
height ListHeight
maxHeight number | string | null
minHeight number | string | null
fillGap number
minColumn number | string
label string
role string | null
wrap boolean
emptyText string | null
emptyOutside boolean
loadingText string
Handle member Type
getItems (): T[]
setItems (items: T[] | null | undefined): void
append (items: T[] | null | undefined): void
removeFirst (count: number): void
setHeight (height: ListHeight, maxHeight?: number | string | null): void
setLoading (loading: boolean): void
scrollToStart (): void
scrollToEnd (): void
cellAt (index: number): HTMLElement | null
emptyElement HTMLElement
destroy (): void
Type What it is
ListHeight How tall a list is. auto grows with its items so the page scrolls. A number of pixels or a CSS length is a fixed height that scrolls inside. fill takes the room from the list's top to the bottom of the window, then scrolls inside.
ListLayout How a list lays out its items. rows are bordered rows with dividers, grid is tiles, plain has no frame.
ListItemHandle A rendered item with its own teardown, such as the handle a Stolon component returns.
ListContent What a list's render function returns for one item. Text is shown as text, never as markup.
ItemRowParts The two parts of a boxed row's text: the primary text, and a dimmed secondary text that starts at the same place on every row of a list.
Class What it looks like
stolon-list A column of items, bordered rows by default, that may scroll inside a fixed or filling height
stolon-list.stolon-rows Rows drawn as one bordered block with a divider between items
stolon-list.stolon-grid Tiles in as many columns as fit, each at least --stolon-list-column wide
stolon-list.stolon-scrolls A list that scrolls inside itself rather than growing the page
stolon-list.stolon-fill A list that reaches from its top to the bottom of the window, and flexes inside a bounded column
stolon-list-item.stolon-openable An item that opens or is picked by a click, with the shared row hover, and an accent ring for a keyboard user
stolon-list-item.stolon-selected A selected item or row, in the accent wash with a leading accent bar, the one selection look every list shares. An open record card keeps the accent on its outline and head only, so its body stays on the plain surface
stolon-list-item.stolon-text A text item, padded the way a component in a row pads itself
stolon-list-empty The sentence a list shows while it holds no items
stolon-list-empty.stolon-outside The empty sentence of a ul or ol list, which sits after the list rather than in it
stolon-item-row A boxed row that looks like a field and is at least a field tall, with its text and row buttons centered, as in a string list
stolon-item-row-parts A row's text in two parts, a primary and a dimmed secondary that starts at the same place on every row of the list, and wraps under the primary when the row is too narrow for both

Example

Example Code

<!-- title: List -->
<div id="mount"></div>
<script type="module">
const base = await window.__baseReady;
const mount = document.getElementById('mount');
const D = window.__galleryData;
const names = Array.from({ length: 30 }, (_, i) => 'Item ' + (i + 1));

const fixed = base.createList({ items: names.slice(0, 15), render: (t) => t, height: 200, onEndReached: () => {
    const have = fixed.getItems().length;
    if (have >= 30) return;
    fixed.setLoading(true);
    setTimeout(() => { fixed.append(names.slice(have, have + 5)); fixed.setLoading(false); }, 400);
} });
mount.appendChild(fixed.element);

const cards = base.createList({
    layout: 'grid',
    minColumn: 120,
    items: D.MEDIA,
    render: (m) => base.createMediaCard({ layout: 'card', shape: base.mediaShape(m.Type), itemId: m.ImageTags.Primary ? m.Id : null, title: m.Name, subtitle: String(m.ProductionYear) })
});
cards.element.style.marginTop = '16px';
mount.appendChild(cards.element);

const empty = base.createList({ items: [], render: (t) => t, emptyText: 'Nothing in the queue.' });
empty.element.style.marginTop = '16px';
mount.appendChild(empty.element);
</script>