# Sortable List

> Drag to reorder a list in React with Motion Reorder, plus Arrow key reordering from the handle and polite announcements of every move for screen readers.

- Kind: Component (Workflow)
- Docs: https://ui.minidev.pro/docs/sortable-list
- Source (registry JSON with the full file): https://ui.minidev.pro/r/sortable-list.json
- License: MIT. One file, yours to edit after install.

## When to use

Use it for short lists people order by hand: priorities, playlist tracks, checklist steps. Every row is rendered, so keep it to a few dozen items. Built on Motion's Reorder. [`KanbanBoard`](https://ui.minidev.pro/docs/kanban-board) shows cards in columns but cannot drag them; for a fixed order use a plain list.

Keyboard: Arrow Up/Down move one place, Home/End move to the ends; focus stays on the moved handle. Pass a new `items` array (different ids or order) to reset the list from outside.

## Installation

### shadcn CLI (copies the source into your project)

```bash
npx shadcn@latest add @minidev/sortable-list
```

Writes `components/ui/sortable-list.tsx` and installs its npm dependencies. Older CLI versions: `npx shadcn@latest add https://ui.minidev.pro/r/sortable-list.json`.

### npm package

```bash
npm i minidev-ui-kit lucide-react motion
```

Add the tokens to your global stylesheet once:

```css
@import "tailwindcss";
@import "minidev-ui-kit/styles.css";
@source "../node_modules/minidev-ui-kit";
```

Next.js also needs `transpilePackages: ["minidev-ui-kit"]` in next.config.

## Import

After the shadcn install:

```tsx
import { SortableList } from "@/components/ui/sortable-list"
```

From the npm package:

```tsx
import { SortableList } from "minidev-ui-kit/ui/sortable-list"
```

## Usage

```tsx
import { SortableList } from "@/components/ui/sortable-list"

export function Example() {
  return (
    <SortableList />
  )
}
```

## Props

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `items` | `Item[]` | `DEMO` | no | Rows in their starting order. Defaults to a short sample task list. |
| `onReorder` | `(items: Item[]) => void` | - | no | Called with the full list after every drag or keyboard move. |
| `className` | `string` | - | no |  |

## Examples

### Custom items

Each item needs a stable id and a label; detail is an optional right aligned note.

```tsx
const STAGES = [
  { id: "lead", label: "Lead", detail: "12" },
  { id: "qualified", label: "Qualified", detail: "8" },
  { id: "proposal", label: "Proposal sent", detail: "5" },
  { id: "negotiation", label: "Negotiation", detail: "3" },
  { id: "won", label: "Won", detail: "2" },
]

<SortableList items={STAGES} />
```

### Read the new order

onReorder gets the full list after every drag or arrow key move, ready to persist.

```tsx
const COLUMNS = [
  { id: "invoice", label: "Invoice" },
  { id: "customer", label: "Customer" },
  { id: "amount", label: "Amount" },
  { id: "due", label: "Due date" },
  { id: "status", label: "Status" },
]

function ColumnOrderDemo() {
  const [order, setOrder] = React.useState(COLUMNS.map((c) => c.label))
  return (
    <div className="flex w-full max-w-sm flex-col gap-3">
      <SortableList items={COLUMNS} onReorder={(items) => setOrder(items.map((i) => i.label))} />
      <p className="text-xs text-fg-muted">Column order: {order.join(", ")}</p>
    </div>
  )
}
```

## Keyboard interactions

| Key | Action |
| --- | --- |
| `Tab` | Moves to the next row's drag handle. |
| `ArrowUp` or `ArrowDown` | On a handle, moves that row up or down one place. Focus stays on the handle. |
| `Home` or `End` | On a handle, moves that row to the top or bottom. |

## Accessibility

- Each drag handle is a `<button>` labelled with the row and its place, such as "Reorder Design review, position 2 of 5".
- A polite live region announces each keyboard move, such as "Design review moved to position 1 of 5".
- Under reduced motion, the `useReducedMotion` branch turns off the layout animation, uses a zero-length transition and drops the 1.02 scale while dragging.

## FAQ

### How do I save the new order?

`onReorder` receives the full list after every keyboard move and every time rows swap during a drag, so it can fire several times per drag. Debounce the save, or save `next.map((t) => t.id)` when the order stops changing.

### Is SortableList controlled?

It keeps its own order. A new `items` array resets it only when the ids or their order differ, so passing the `onReorder` result back in does not cause a loop.

### Can rows show custom content?

Rows are `{ id, label, detail? }`, with `detail` shown on the right in mono. For avatars, badges or buttons, copy the file and edit `Row`.

## Rules for generating code

- Use semantic tokens only (bg, surface, sunken, border, fg, fg-muted, fg-subtle, ink, accent, success, warning, danger). Never raw hex or palette steps like blue-500.
- Shadows only from the set: shadow-xs, shadow-sm, shadow-raised, shadow-md, shadow-lg, shadow-overlay.
- Numbers use tabular-nums. Dark mode is the `dark` class on <html>; components never branch on theme.

## Dependencies

- npm: `lucide-react`, `motion`

## Related

- [Kanban Board](https://ui.minidev.pro/docs/kanban-board/md): A horizontally scrolling Kanban board built from column data: each column shows its title, a card count and stacked cards with a title and optional meta line.
- [Approval Card](https://ui.minidev.pro/docs/approval-card/md): React request card for approval workflows showing the requester avatar, title and summary, with Reject and Approve buttons wired to your callbacks.
- [Assign Picker](https://ui.minidev.pro/docs/assign-picker/md): Assignee picker for tasks and tickets: a listbox of people with initials avatars, where the selected person is highlighted and choices report their id.
- [Comment Thread](https://ui.minidev.pro/docs/comment-thread/md): Threaded React comment list with initials avatars, author name, timestamp and body in bordered bubbles. Pairs with the comment composer.
- [Due Date Chip](https://ui.minidev.pro/docs/due-date-chip/md): Small Tailwind due date chip with a calendar icon that switches to a red border and icon when overdue. Use it on task cards, tickets and boards.
- [Gantt Row](https://ui.minidev.pro/docs/gantt-row/md): One row of a Gantt timeline: a task label beside a track with a bar placed by start and end values on a shared scale. Stack rows to build a React roadmap.

---

All 557 components: https://ui.minidev.pro/llms.txt (full text: https://ui.minidev.pro/llms-full.txt).
