Skip to content

Sortable list

Rows put in order by dragging a handle or the whole row, or from the keyboard: Space picks up, the arrows move, Space drops, each step read aloud. The lifted row follows the pointer exactly, the others make room on a spring, and a list in a scroll box scrolls at its edges.

Friday supper

  • Oysters with mignonetteTwo each, shucked at the tableTheo
  • Burrata with charred peachesBasil oil, flaky saltPriya
  • Saffron risottoNeeds the last 20 minutes on the stoveMaya
  • Braised short ribs, polenta, gremolataBraised the day before; reheats in the low oven while the risotto restsTheo
  • Olive oil cakeCrème fraîche, roasted plumsMaya
Press Space to pick up. Use the arrow keys to move, Space to drop, Escape to cancel.

Drag a handle, or focus one and press Space.

The whole row, in a scroll box

  • Brine the short ribs
  • Make the stock
  • Bake the olive oil cake
  • Roast the plums
  • Shuck the oysters
  • Chop the gremolata
  • Toast the saffron
  • Whip the crème fraîche
  • Polish the glasses
  • Set the table for eight
  • Chill the white wine
  • Light the candles
Press Space to pick up. Use the arrow keys to move, Space to drop, Escape to cancel.

A row that can’t be picked up

  • Maya welcomes everyoneThe host opens; this one stays put
  • Priya, on the peachesTwo minutes, she promised
  • Theo, on ten years of FridaysBring tissues
Press Space to pick up. Use the arrow keys to move, Space to drop, Escape to cancel.

Installation

With UI Polish connected, ask your agent:

Add the UI Polish sortable list to this project.

Or with the shadcn CLI:

npx shadcn@latest add https://uipolish.com/r/sortable-list.json
Brings inIcons, List, Text

Rows someone puts in order: the courses of a dinner, the steps of a recipe, the speakers at a toast. Each row is a List row with a grip at its start; drag the grip, or focus it and press Space, and the row lifts and follows the pointer exactly while the others open a gap for it. Let go and it settles into the gap. A list inside a scroll box scrolls while a row is held near its edge.

TSX
<SortableList  aria-label="Courses"  items={courses}  onReorder={(from, to) => setCourses((c) => move(c, from, to))}  renderItem={(c) => <SortableItem title={c.title} description={c.note} trailing={c.cook} />}/>

onReorder hands you the move; nothing changes until your state does. A move that takes an item out at from and puts it back at to is all it needs:

TypeScript
function move<T>(list: T[], from: number, to: number) {  const next = list.slice();  next.splice(to, 0, ...next.splice(from, 1));  return next;}

Base UI has no drag and drop, so the dragging is in drag.ts, beside this file, written on pointer events with no package. Kanban board uses the same engine. The look and motion are the library's.

When to use it

  • An order that means something and is short enough to see: courses, agenda items, a playlist, the fields of a form, priorities.
  • An order people set by hand, rather than one a sort could work out.

When not to

  • An order a value decides (date, price, name): sort by it instead.
  • Moving things between groups: use a kanban board.
  • Hundreds of rows: dragging past a screenful is slow work. Offer "Move to top" and "Move to position…" actions instead, or as well.
  • A list that only shows things: use a list.

Props

SortableList takes every <ul> prop (name it with aria-label or aria-labelledby), ref and className, besides these:

PropWhat it does
items, renderItemYour items, and a function that returns each one's SortableItem
getKeyA stable key for an item. Default: its id
childrenInstead of items: the SortableItems themselves, each with a key
onReorder(from, to): the row at from should now be at to. Required. Not called when a row lands where it was
handletrue (default): rows lift by the grip at their start, and the rest of the row stays selectable text. false: by the whole row, which becomes one tab stop; on a touch screen a press must hold for 250ms first, so a swipe still scrolls
variantbox (default): rows in List's rounded surface. plain: no box
messagesThe words read to screen readers and the grip's name, to translate them (see Accessibility)

SortableItem takes <li> props besides these:

PropWhat it does
titleThe row's name; what a move is announced with when it's text
labelWhat a move is announced with, when title isn't plain text
descriptionA line under the title
leadingAn avatar, icon or thumbnail. Decorative
trailingA value at the end: who, when, how many. Keep it the item's own, not its position: the lifted copy shows what the row showed when it was picked up
actionsSecondary actions, usually List's ListAction. A press on one never lifts the row
disabledThe row can't be picked up; the others still move around it

Keyboard

KeyDoes
Space or Enter, on a gripPicks the row up. With handle={false}, Space on the row
Up, DownMoves it one place
Home, EndMoves it to the top or the bottom
Space or EnterDrops it where it is
EscapePuts it back where it was picked up
TabDrops it, then moves on

Clicking elsewhere while a row is held puts it back.

States

StateWhat you seeHow
RestList rows, the grip a quiet 16px mark at the start
Grip hoverThe grip goes to the text color; the cursor is a hand ready to grab@media (hover: hover)
Grip focusA ring just inside the grip:focus-visible
Holding (touch, whole row)The row's fill presses down while the 250ms hold runsdata-drag-pending
LiftedA copy of the row rides above the page: the surface (a step lighter in dark), the edge and the raised shadow, grown by about 12px across (at most 2%) about the point you hold. Its place stays emptydata-drag-source on the row, data-drag-clone on the copy
Lifted from the keyboardThe copy also carries the focus ringdata-keyboard on its layer
Making roomThe rows between its old place and the new one slide by its height; the separators step aside so the gap reads as one spacedata-dragging on the list
Outside the listDragged more than 24px past the list, the gap goes back to where the row came from: a drop there puts it back
SettlingThe copy lands on the row's new place and comes down to the row's own size and fill as it does; in its last frame it is the rowdata-settling on the copy
DisabledGrip at 50%, no grab cursor, out of the tab orderdisabled

Layout

Rows are List's: 12px in from their edges, 60px with a description, 44px without, 48px or more on touch screens. The grip is a 24 by 32px target whose glyph sits on the row's padding line, so the text starts 44px in; on a touch screen it's 40px square and reaches the row's edge.

The lifted copy is drawn in a layer at the end of the page (or of the nearest element with data-theme), outside any scroll box, so no edge or column ever cuts it off, its shadow included. It copies the row as it was when picked up.

Motion

WhatDurationEasing
The copy following the pointernone: 1:1, every pointer event
The copy growing as it lifts, and coming down as it lands--ui-dur-snap (240ms)--ui-ease-snap
Its shadow and fill coming in--ui-dur-fast (160ms)--ui-ease-color
…and going as it lands--ui-dur-snap (240ms)--ui-ease-color
The others making room--ui-dur-move (380ms)--ui-ease-move
The copy settling into its place on the drop--ui-dur-snap (240ms)--ui-ease-snap
The copy moving a place from the keyboard--ui-dur-move (380ms)--ui-ease-move
Rows gliding back after a move is refused or undone--ui-dur-move (380ms)--ui-ease-move
Grip color, separators--ui-dur-instant (120ms)--ui-ease-color
Press under a holding finger--ui-dur-press (80ms)
Edge scrollingup to about 20px a frame, faster the nearer the edge

Every move starts from where things are seen: a row caught again while it settles is picked up from there, a gap that changes its mind retargets from where it has got to, and the drop measures every row before the page changes and glides each from there to its new place. Under prefers-reduced-motion: reduce rows and the copy move at once and nothing grows; the shadow still fades in and out.

Accessibility

  • The list is a <ul role="list">, so screen readers count the rows.
  • Each grip is a button named "Move" and the row's name ("Move Olive oil cake"), described by the instructions: "Press Space to pick up. Use the arrow keys to move, Space to drop, Escape to cancel." With handle={false} the title is the one tab stop, a button with the role description "sortable item".
  • Every step is read from a polite live region: "Picked up Olive oil cake, position 5 of 5." "Moved to position 4 of 5." "Dropped Olive oil cake at position 2 of 5." "Cancelled. Olive oil cake is back at position 5 of 5." A pointer drag announces its pick-up and its drop.
  • Focus stays on the grip through the whole move and is on it afterwards, in the row's new place.
  • The copy is aria-hidden and inert; the row it stands for stays in the page, so nothing is read twice.
  • messages takes instructions, handle(label) and the four announcements, each a function of { label, position, total }.
  • Grips are 40px on touch screens.

Before / after

Before: native HTML drag and drop. Nothing works on a phone or from the keyboard, the browser draws a ghost image at half opacity, the other rows don't move until the drop, and a screen reader hears nothing.

TSX
<li draggable onDragStart={() => setDragging(i)} onDrop={() => move(dragging, i)}>  {course.title}</li>

After: a grip that works by mouse, finger and keyboard, a row that follows the pointer, a gap that opens where it will land, and every step read aloud.

TSX
<SortableList aria-label="Courses" items={courses} onReorder={(from, to) => setCourses((c) => move(c, from, to))}  renderItem={(c) => <SortableItem title={c.title} />} />