Skip to content

Sortable Card List

Records as cards in priority order. Each card has a numbered head with a status dot, badges, up and down arrows, and a drag handle, plus a body that the open card shows. One editor element moves into whichever card is open.

Decision

When Use
A short ordered list where each record opens an editor, such as servers or channels Sortable Card List
Ranking with no editor Ordered List
A long list Paginated Table

Usage

<div id="serverList" class="stolon-card-list"></div>
<div class="stolon-card-list-actions">
    <button is="emby-button" type="button" id="btnAddServer" class="raised stolon-button stolon-primary">
        <span class="material-icons" aria-hidden="true">add</span><span>Add Server</span>
    </button>
</div>
<div id="serverEditorHolder" class="stolon-hidden"><div id="serverEditor"></div></div>
import { createSortableCardList } from './configurationpage?name=myplugin_stolon.js';

var cards = createSortableCardList(view.querySelector('#serverList'), {
    items: servers,
    editor: view.querySelector('#serverEditor'),
    collapsible: false,
    render: function (server, index) {
        return { title: server.Name, subtitle: server.Url, dot: server.Ok ? 'ok' : 'bad',
                 disabled: server.IsEnabled === false, badges: [{ label: server.Mode, cls: 'blue' }] };
    },
    onBeforeLeave: function (index) { readEditorInto(servers[index]); },
    onSelect: function (index) { loadEditor(servers[index]); },
    onMove: function (from, to) { saveOrder(); }
});
cards.setSelected(0);

The handle is { render, setItems, getSelected, setSelected, setHeight, destroy }.

Classes: stolon-card-list, stolon-card-item, stolon-card-item-head, stolon-card-item-head-static, stolon-card-item-grip, stolon-card-item-num, stolon-card-item-main, stolon-card-item-title, stolon-card-item-sub, stolon-card-item-sub-text, stolon-card-item-tags, stolon-card-item-arrows, stolon-card-item-chevron, stolon-card-item-body, stolon-card-list-actions. The dot is a Status Dot, stolon-dot, the badges are Badges, and the chevron is the Collapsible Section chevron, stolon-collapsible-icon, which turns by the head's aria-expanded.

States and Modifiers

  • render(item, index) returns { title, subtitle, badges: [{ label, cls }], dot, dotTitle, disabled }. dot is 'ok' (green), 'bad' (red), 'checking' (orange), 'unknown' (gray), a status color name or hex code, or 'none' to omit it, and any other word gives a gray dot. dotTitle names the dot as its title and aria-label, and defaults to Active, Error, Checking, Unknown, or the color's meaning. cls is a badge color or hex code, with medium, large, or other classes beside it, gray by default.
  • The editor parks in a hidden holder after the list while the list re-renders, so its fields and bindings survive.
  • setSelected(index, { collapsed }) opens a card and calls onSelect. A bad index clears the selection. setItems(next) re-renders and clamps the selection. getSelected() is the open index or -1.
  • height, maxHeight, and minHeight set the height of the list as a List takes them, 'auto' by default so the list grows with the page. setHeight(height, maxHeight) changes them later.
  • reorder (default true) turns on the arrows and drag. collapsible (default true) lets a click on the open head fold it. False keeps the open card open.
  • A click, Enter, or Space on a head opens the card. The keys are handled only when the head itself has focus.
  • A card can be selected, collapsed, disabled, or dragging. drop-before and drop-after draw a 3px accent line above or below the drop target.
  • The head is role="button" with aria-expanded. The arrow buttons are titled "Move up" and "Move down" and labelled "Move {value} up" and "Move {value} down", where {value} is the card's title or "Item 2". moveUpTitle, moveDownTitle, moveUpLabel, and moveDownLabel replace them, and after a move focus stays on the pressed arrow, or the other one when it is now disabled.
  • At 768px and below the mono subtitle line is hidden. stolon-card-item-sub-text stays, for words rather than an address.
  • The same card classes make a plain record list without the helper. A head with stolon-card-item-head-static only holds a control at its end.

Gotchas

  • Pass the editor as an element already in the page.
  • The badge cls is reduced to class names, and the badge label and dot name go through the built in escaper, not the escapeHtml option.
  • render runs for every card on each render, so keep it cheap. A move does not call onSelect.
  • createSortableCardList throws without a list element or a render function.

Reference

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

createSortableCardList

function createSortableCardList<T = any>(listEl: HTMLElement, options: SortableCardListOptions<T>): SortableCardListHandle<T>

Renders records as numbered cards with a status dot, badges, arrow and drag reordering, and a body the open card shows. One editor element moves into whichever card is open, so its fields and bindings survive a re-render.

Option Type What it does
items T[] array of records in priority order, replaced with setItems [optional]
render (required) (item: T, index: number): CardSpec | null | undefined function(item, index) returning { title, subtitle, badges: [{ label, cls }], dot: 'ok'|'bad'|'checking'|'unknown'|'none', dotTitle, disabled } [required]
editor HTMLElement | null element placed inside the open card's body [optional]
onSelect (index: number): void function(index) called when a card opens, before the editor is placed [optional]
onBeforeLeave (index: number): void function(index) called when the open card is left, to read the editor back [optional]
onMove (fromIndex: number, toIndex: number): void function(fromIndex, toIndex) called after a reorder, so the caller can save [optional]
reorder boolean allow arrows and drag (default true)
collapsible boolean clicking the open head folds it and a chevron shows the state, where false keeps the open card open (default true)
escapeHtml (str: unknown): string function(str) returning a safe string (default the built in escaper)
moveUpTitle string tooltip of an up arrow (default 'Move up')
moveDownTitle string tooltip of a down arrow (default 'Move down')
moveUpLabel RowLabel aria-label of an up arrow, where {value} is the card's title or "Item 2", or a function(name) returning it (default 'Move {value} up')
moveDownLabel RowLabel aria-label of a down arrow, where {value} is the card's title or "Item 2", or a function(name) returning it (default 'Move {value} down')
height ListHeight height of the list, as createList takes it (default 'auto')
maxHeight number | string | null height past which the list scrolls [optional]
minHeight number | string | null least height of a filling list [optional]
Handle member Type
render (): void
setItems (next: T[] | null): void
getSelected (): number
setSelected (index: number | null, opts?: { collapsed?: boolean; }): void
setHeight (height: ListHeight, maxHeight?: number | string | null): void
destroy (): void
Type What it is
CardSpec What render returns for one card. dot picks the status dot, and a disabled card is dimmed.
Class What it looks like
stolon-card-list Vertical list of record cards in priority order, each with a head row and a body the open card shows
stolon-card-item One record card. selected marks the open one, collapsed folds it, disabled dims its text, dragging fades it
stolon-card-item-head Clickable head row: grip, number, main text, arrows, and the collapsible chevron, which turns by its aria-expanded. Tinted when the card is open
stolon-card-item-grip Drag handle icon at the left of the head
stolon-card-item-num Round priority number, filled with the accent on the open card
stolon-card-item-main Title line and sub line that take the remaining width and clip
stolon-card-item-title Bold title line that clips with an ellipsis
stolon-card-item-sub Small dimmed mono sub line that clips with an ellipsis
stolon-card-item-sub.stolon-card-item-sub-text The sub line in the page's own font, for words rather than an address or path
stolon-card-item-head.stolon-card-item-head-static A head row that only holds a control at its end (such as Unblock) opens nothing itself
stolon-card-item-body Body shown only inside the open, unfolded card
stolon-card-list-actions Action row under the list, for an Add button

Example

Example Code

<!-- title: Sortable Card List -->
<div id="serverList" class="stolon-card-list"></div>
<div class="stolon-card-list-actions">
    <button is="emby-button" type="button" class="raised stolon-button stolon-primary">
        <span class="material-icons" aria-hidden="true">add</span><span>Add Server</span>
    </button>
</div>
<span id="orderStatus" class="stolon-status-text"></span>
<div id="editorHolder" class="stolon-hidden">
    <div id="serverEditor">
        <div class="inputContainer"><label for="txtEditName">Name</label><input is="emby-input" type="text" id="txtEditName" value="Edited in place" /></div>
    </div>
</div>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
const servers = [
    { name: 'Living room', url: 'http://living:8096', mode: 'Both', dot: 'ok' },
    { name: 'Cabin', url: 'http://cabin:8096', mode: 'Pull', dot: 'bad' },
    { name: 'Backup', url: 'http://backup:8096', mode: 'Push', dot: 'checking', disabled: true }
];
const colors = { Both: 'purple', Pull: 'green', Push: 'blue' };
const cards = base.createSortableCardList(document.getElementById('serverList'), {
    items: servers,
    editor: document.getElementById('serverEditor'),
    render: (server) => ({
        title: server.name,
        subtitle: server.url,
        dot: server.dot,
        disabled: server.disabled,
        badges: [{ label: server.mode, cls: colors[server.mode] }]
    }),
    onMove: (from, to) => shared.setStatus('orderStatus', 'Moved ' + (from + 1) + ' to ' + (to + 1) + '.', false, { timeout: 0 })
});
cards.setSelected(0);
</script>