SortableList unstable
A headless list reordered by dragging rows or activating handles.
The consumer owns the order: SortableList emits Reordered { from, to }
and renders the order supplied in items.
Details
The component tracks the move and provides announcement data. A drop by handle focuses the moved row's handle. A pointer drop leaves focus unchanged.
Anatomy:
| Part | Element |
|---|---|
list |
any element, usually a <ul> or <ol> |
item |
any element, one per entry in items |
handle |
<button>, one in each item |
status |
any element, a polite live region |
list and status take the consumer's builder. item and handle
take { element, id }, with the id of one entry in items; an id
that isn't there fails the render. item keys its element with id, so
rows keep their DOM nodes when the order changes. No part adds markup.
Data attributes:
Present with an empty value or absent, except data-orientation and
data-drop-position:
list:data-orientation,data-movinganddata-disableditem:data-movingon the row being moved, anddata-drop-position,"before"or"after", on the row the moved one would land next tohandle:data-movinganddata-disabled
Keyboard:
The handle is a native <button type="button">, so Enter, Space, a tap and
a screen reader's activation all arrive as its click:
| Input | Action |
|---|---|
| Activate a handle | pick its row up |
| Down, Up (Right, Left) | move the landing place by one, no wrap |
| Home, End | move the landing place to either end |
| Activate the moved handle | drop at the landing place |
| Activate another handle | drop at that row's place |
| Escape, or focus leaving | cancel |
A horizontal list moves with Right and Left, swapped in a right-to-left layout. The keys are cancelled only during a move, so an enclosing dialog keeps Escape otherwise. A touch screen reader user double-taps one handle, swipes to another row, and double-taps its handle.
Announcements:
The parts carry announcement: what happened last, the row's id, its
1-based position and the count of rows. Write the sentence in the
status part, in the user's language, and include how to move the row in
the PickedUp one. Dropped reports the row's current position in items,
even if the consumer declines the reorder. The announcement clears when
the row leaves items, the row count changes, or its position matches
neither the source nor the destination.
Example A reorderable todo list
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { SortableList } from "effect/ui/kit"import { Effect, Schema } from "effect"
const Todo = Schema.Struct({ id: Schema.String, title: Schema.String })const Message = UiSchema.TaggedEnum({ ReorderedTodos: { from: Schema.Number, to: Schema.Number }})
const Todos = Html.component({ name: "Todos", model: Schema.Struct({ todos: Schema.Array(Todo) }), message: Message}).make({ init: () => ({ todos: [{ id: "a", title: "Buy milk" }, { id: "b", title: "Walk dog" }] }), update: { ReorderedTodos: ($, { from, to }) => { const todos = [...$.model.todos] todos.splice(to, 0, ...todos.splice(from, 1)) return { todos } } }, view: (h, model) => SortableList(h, { props: { items: model.todos.map((todo) => todo.id) }, onEmit: ({ from, to }) => Message.ReorderedTodos({ from, to }), view: (sortableList) => { const { announcement } = sortableList const title = (id: string) => model.todos.find((todo) => todo.id === id)?.title return h.div( sortableList.list(h.ul).children( model.todos.map((todo) => sortableList.item({ element: h.li, id: todo.id }).children( sortableList.handle({ element: h.button, id: todo.id }) .attr("aria-label", `Reorder ${todo.title}`), todo.title ) ) ), sortableList.status(h.div.class("sr-only")).children( announcement === undefined ? "" : `${title(announcement.id)}, ${announcement._tag}, ${announcement.position} of ${announcement.count}` ) ) } })})
const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Todos, {}))if (!/<button[^>]* aria-pressed="false"[^>]*aria-label="Reorder Buy milk"/.test(html)) { throw new Error("the handle isn't a toggle button")}if (!/class="sr-only"[^>]* role="status"/.test(html)) throw new Error("the status isn't a live region")Components
SortableList
unstableA headless sortable list. Call it with the consumer's h, the props,
onEmit for Reordered, and a view closure that draws
sortableList.list around one sortableList.item per id, each with a
sortableList.handle inside, and sortableList.status anywhere.
Details
The parts also return moving, the SortableListMove in progress or
undefined, announcement, a SortableListAnnouncement or undefined,
disabled and orientation.
Part attributes override those already set on the builder, including attributes the part removes:
list:data-orientation,data-movinganddata-disableditem:draggable,data-movinganddata-drop-positionhandle:type="button",disabled,aria-disabled(removed),aria-pressed("true"only on the row being moved by handle),data-movinganddata-disabledstatus:role="status",aria-live="polite"andaria-atomic="true"
list handles focusout, dragenter, dragleave and drop; item
handles dragstart, dragover, dragleave, drop and dragend;
handle handles click, mousedown and keydown. Each is on a layer of
its own, so the consumer's handlers still run. focusout, mousedown
and keydown are handled only during a move by handle, so otherwise
they reach the consumer's handlers on enclosing elements. Drops accept only
rows from the same list. A drop between rows uses the last hovered position.
The list follows the nearest DirectionProvider
or, without one, the list's CSS direction.
Signature
declare const SortableList: Headless<{ readonly disabled?: boolean; readonly items: readonly Array<string>; readonly orientation?: "horizontal" | "vertical";}, { readonly _tag: "Reordered"; readonly from: number; readonly to: number;}, { announcement: SortableListAnnouncement | undefined; disabled: boolean; handle: <B extends PartTarget<"button">>(__namedParameters: { readonly element: B; readonly id: string; }) => ReturnType<B["attr"]> & Pick<Part<"button", [id: string], never>, "~R">; item: <B extends Keyed<string>>(__namedParameters: { readonly element: B; readonly id: string; }) => ReturnType<B["attr"]> & Pick<Part<string, [id: string], never>, "~R">; list: Part<string, [], never>; moving: SortableListMove | undefined; orientation: "horizontal" | "vertical"; status: Part<string, [], never>;}, never, never, never, never, never, never, { readonly ended: { readonly count: number; readonly dropped: boolean; readonly from: number; readonly id: string; readonly to: number; } | null; readonly moving: { readonly dragging: boolean; readonly id: string; readonly over: boolean; readonly stepped: boolean; readonly to: number; } | null; readonly pressed: boolean; readonly rtl: boolean;}, { readonly _tag: "ReleasedPointer";} | { readonly _tag: "FoundDirection"; readonly rtl: boolean;} | { readonly _tag: "PressedKey"; readonly key: string;} | { readonly _tag: "ClickedHandle"; readonly id: string;} | { readonly _tag: "PressedHandle";} | { readonly _tag: "LeftList";} | { readonly _tag: "StartedDrag"; readonly id: string;} | { readonly _tag: "DraggedOver"; readonly after: boolean; readonly id: string;} | { readonly _tag: "LeftDropTarget";} | { readonly _tag: "EnteredList";} | { readonly _tag: "DroppedOnItem"; readonly after: boolean; readonly id: string;} | { readonly _tag: "DroppedOnList";} | { readonly _tag: "EndedDrag";} | { readonly _tag: "ChangedProps";}, QueryEntries<{}, {}>>Models
What a SortableList's status should say: PickedUp when a handle
picks a row up, Moved as the landing place changes, Dropped and
Cancelled once the move ends. position is 1-based: where the row would
land while it moves, and where it is after.
Signature
interface SortableListAnnouncement { readonly _tag: "Cancelled" | "Dropped" | "Moved" | "PickedUp"; readonly count: number; readonly id: string; readonly position: number;}What a SortableList emits.
Signature
type SortableListEvents = typeof SortableListEvents.TypeThe row being moved: its id, its index from, and the index to it
would end up at.
Signature
interface SortableListMove { readonly from: number; readonly id: string; readonly to: number;}The props of a SortableList.
Signature
type SortableListProps = typeof SortableListProps.TypeSchemas
SortableListEvents
unstableReordered asks to move the row at from so that it ends up at to:
splice it out at from and back in at to. Nothing is emitted when the
row lands where it started.
Signature
declare const SortableListEvents: TaggedEnum<{ Reordered: TaggedStruct<"Reordered", { readonly from: Number; readonly to: Number; }>;}>SortableListProps
unstableConfigure row order, disabled state and orientation.
Details
items: unique row IDs in render order. Required.disabled: disables every handle and stops drags and drops.orientation:"vertical"by default, which moves with Down and Up and splits rows into top and bottom halves for a drag, or"horizontal".
Signature
declare const SortableListProps: Struct<{ readonly disabled: optional<Boolean>; readonly items: $Array<String>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>;}>