Skip to content

Ordered List

A light ordered list of rows with an up and a down button on each. render may return { primary, secondary }, so every row's secondary text starts at the same place.

Decision

When Use
A short list the user ranks, with no editor and no drag Ordered List
Records that each open an editor Sortable Card List
Plain text values the reader also adds, edits, and removes String List

Usage

import { createOrderedList } from './configurationpage?name=myplugin_stolon.js';

var order = createOrderedList({
    items: config.Steps,
    render: function (step) { return '<strong>' + shared.escapeHtml(step.Name) + '</strong>'; },
    label: function (step) { return step.Name; },
    onMove: function (from, to, items) { config.Steps = items; tracker.check(); }
});
container.appendChild(order.element);

Classes: stolon-ordered-list, stolon-ordered-item, stolon-ordered-main, stolon-ordered-arrows, stolon-ordered-up, stolon-ordered-down. The handle is { element, setItems, getItems, setHeight, destroy }.

States and Modifiers

  • render(item, index) is required and returns a node, an HTML string that is already escaped, or { primary, secondary } as text or nodes for a boxed row whose secondary text lines up across the rows.
  • label(item, index) names the row for the button labels, "Item 1", "Item 2" by default.
  • onMove(from, to, items) receives the two indexes and a copy of the new order.
  • moveUpTitle and moveDownTitle replace the tooltips "Move up" and "Move down". moveUpLabel and moveDownLabel replace the labels "Move {value} up" and "Move {value} down", as a template where {value} is the row's name or as a function(name).
  • The first up button and the last down button are disabled. A move keeps focus on the button that was pressed, or on the other button of the row when that one is now disabled.
  • The buttons are native, so Enter and Space work.
  • 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.

Gotchas

  • createOrderedList throws without a render function.
  • stolon-ordered-up and stolon-ordered-down are hooks with no CSS rules.
  • destroy() empties the list and ignores later clicks.

Reference

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

createOrderedList

function createOrderedList<T = any>(options: OrderedListOptions<T>): OrderedListHandle<T>

A light ordered list of rows, each with an up and a down button. The caller draws the rows, and a move reorders the items, redraws, and keeps focus on the button that was pressed.

Option Type What it does
items T[] array of records in order, replaced with setItems [optional]
render (required) (item: T, index: number): Node | string | ItemRowParts function(item, index) returning a node or an HTML string for the row, where a string must already be escaped [required]
label (item: T, index: number): string function(item, index) returning the row's name for the button labels (default "Item 1", "Item 2")
onMove (fromIndex: number, toIndex: number, items: T[]): void function(fromIndex, toIndex, items) called after a move, so the caller can save [optional]
moveUpTitle string tooltip of the up button (default 'Move up')
moveDownTitle string tooltip of the down button (default 'Move down')
moveUpLabel RowLabel the up button's aria-label, where {value} is the row's name, or a function(name) returning it (default 'Move {value} up')
moveDownLabel RowLabel the down button's aria-label, where {value} is the row's name, 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
element HTMLOListElement
setItems (next: T[] | null): void
getItems (): T[]
setHeight (height: ListHeight, maxHeight?: number | string | null): void
destroy (): void
Class What it looks like
stolon-ordered-list Light ordered list of rows with move buttons, built by createOrderedList
stolon-ordered-main Content of a row that takes the free width and clips

Example

Example Code

<!-- title: Ordered List -->
<div id="mount"></div>
<span id="orderedStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
const order = base.createOrderedList({
    items: ['Intro', 'Verse', 'Chorus', 'Outro'],
    render: (item, index) => ({ primary: Object.assign(document.createElement('strong'), { textContent: item }), secondary: 'Position ' + (index + 1) }),
    label: (item) => item,
    onMove: (from, to, items) => shared.setStatus('orderedStatus', 'Order: ' + items.join(', ') + '.', false, { timeout: 0 })
});
document.getElementById('mount').appendChild(order.element);
</script>