Playground

Sortable list

A list the user reorders by dragging a handle or with the keyboard, with live announcements.

  • Built on native elements
  • Each row has a handle button named 'Reorder <item>' with aria-pressed while picked up and a description of the keys.
  • Space or Enter picks an item up; the arrow keys (or Home and End) move it; Space or Enter drops it; Escape cancels and restores the order; Tab cancels.
  • A polite live region announces: picked up, moved to position N of M, dropped, cancelled.
  • Styles in one file: sortable-list.css

Import

import '@monoline/css';
import {SortableList} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add sortable-list

Usage

import {SortableList, type SortableItem} from '@monoline/react';

const items: SortableItem[] = ['Write the brief', 'Draft the spec', 'Review with design', 'Build', 'Ship'].map((t) => ({id: t, label: t}));

<div style={{maxWidth: '360px'}}>
  <SortableList aria-label="Tasks" defaultValue={items} />
</div>

Examples

Vertical

Tab to a handle, press Space, move with the arrow keys, press Space to drop. Drag with a pointer works too.

Horizontal

Props

SortableListProps

PropTypeDefaultDescription
aria-label requiredstring–Required: names the list.
valueSortableItem[]–The items in their current order when controlled. Pair with `onValueChange`.
defaultValueSortableItem[][]The items in their starting order when uncontrolled.
onValueChange((items: SortableItem[]) => void)–Called with the new order when the user drops an item in a new place. Not called for a cancelled move or a drop where it started.
orientation"vertical" | "horizontal"'vertical'`vertical` (default) moves with ArrowUp and ArrowDown; `horizontal` lays the items in a row and moves with ArrowLeft and ArrowRight.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[aria-pressed="true"]sortable-list.css
[data-dragging]sortable-list.css

CSS classes

ClassStylesheet
.sortable-listsortable-list.css
.sortable-list-handlesortable-list.css
.sortable-list-horizontalsortable-list.css
.sortable-list-itemsortable-list.css
.sortable-list-labelsortable-list.css

Customization

Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.

app.css
.sortable-list {
  border-color: var(--accent);
}
.sortable-list[aria-pressed="true"] {
  background: var(--wash);
}

Accessibility

  • Each row has a handle button named 'Reorder <item>' with aria-pressed while picked up and a description of the keys.
  • Space or Enter picks an item up; the arrow keys (or Home and End) move it; Space or Enter drops it; Escape cancels and restores the order; Tab cancels.
  • A polite live region announces: picked up, moved to position N of M, dropped, cancelled.
  • Pointer drag uses the same handle; reordering never depends on dragging alone.

Keyboard

KeysAction
Space / EnterPicks an item up
Arrow keys(or Home and End) move it
Space / EnterDrops it
EscapeCancels and restores the order
TabCancels