Sortable list
A list the user reorders by dragging a handle or with the keyboard, with live announcements.
SourceStyles source@monoline/react 0.5.0Markdown for AI toolsstable
- 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
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label required | string | – | Required: names the list. |
value | SortableItem[] | – | The items in their current order when controlled. Pair with `onValueChange`. |
defaultValue | SortableItem[] | [] | 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.
| Attribute | Stylesheet |
|---|---|
[aria-pressed="true"] | sortable-list.css |
[data-dragging] | sortable-list.css |
CSS classes
| Class | Stylesheet |
|---|---|
.sortable-list | sortable-list.css |
.sortable-list-handle | sortable-list.css |
.sortable-list-horizontal | sortable-list.css |
.sortable-list-item | sortable-list.css |
.sortable-list-label | sortable-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
| Keys | Action |
|---|---|
| Space / Enter | Picks an item up |
| Arrow keys | (or Home and End) move it |
| Space / Enter | Drops it |
| Escape | Cancels and restores the order |
| Tab | Cancels |