# Sortable list

> Drag to reorder and everything moves: rows slide out of the way, the dropped one settles, every rank rolls, and presets re-sort the lot.

Reordering is the most physical thing a list can do, and most sortable lists teleport rows or fade them. Here you press a row’s grip and it lifts, a darker hairline and a touch larger, and follows your finger exactly while the rows it passes slide out of its way one slot at a time. Let go and it settles into its slot with a small overshoot, every rank number rolls to its new place, and the row you moved says how far it went ("↑ 2") before fading. Sort presets above the list re-sort everything at once and every row glides to where it belongs; the highlight slides to the preset you picked and fades into "Your order" once you rearrange by hand. It all works from the keyboard too, and every move is announced.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/sortable-list.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `number-roll`
- Docs and live preview: https://hairlineui.com/ui/sortable-list
- Registry item (source): https://hairlineui.com/r/sortable-list.json

## Usage

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

const [items, setItems] = useState(features);

<SortableList
  items={items}
  onReorder={setItems}
  getLabel={(f) => f.name}
  renderItem={(f) => <span className="text-[13.5px] font-medium">{f.name}</span>}
/>
```

## Saving the order

Update the list right away so it moves, then save the ids; put the old order back if the save fails.

```tsx
"use client";
import { useState } from "react";
import { SortableList } from "@/components/sortable-list";

export function Priorities({ initial }: { initial: { id: string; name: string; votes: number }[] }) {
  const [items, setItems] = useState(initial);
  return (
    <SortableList
      items={items}
      getLabel={(f) => f.name}
      sorts={[{ label: "Votes", compare: (a, b) => b.votes - a.votes }]}
      onReorder={async (next) => {
        const before = items;
        setItems(next);
        const res = await fetch("/api/priorities", { method: "PUT", body: JSON.stringify(next.map((f) => f.id)) });
        if (!res.ok) setItems(before); // glides back
      }}
      renderItem={(f) => (
        <div className="flex justify-between text-[13.5px]">
          <span className="font-medium">{f.name}</span>
          <span className="text-muted-foreground tabular-nums">{f.votes}</span>
        </div>
      )}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `items` | `T[] (each with an id)` | – | The list in its current order. |
| `onReorder` | `(items: T[]) => void` | – | Called with the new order after a drop, a keyboard move or a preset. Update items with it; if you don’t, the rows glide back. |
| `renderItem` | `(item, { index, lifted }) => ReactNode` | – | A row’s content, between the rank and the edge; the grip, rank, border and motion are handled. |
| `getLabel` | `(item) => string` | – | The name used in the grip’s label and the announcements. |
| `sorts` | `{ label; compare }[]` | – | Presets shown as a segmented control; the one the order currently matches shows as pressed. |
| `label` | `string` | `"Reorderable list"` | The list’s accessible name. |

## States

- **lift**: Pressing the grip: the row’s border darkens, it scales to 1.015 (200ms) and rises above the others. The grip takes the pointer, so touch scrolling elsewhere is untouched.
- **drag**: The row follows the pointer without easing; rows it passes shift by one row height (220ms) the moment its centre crosses theirs.
- **drop**: onReorder gets the new order; every row glides from where it was on screen (FLIP, 420ms) and the dropped one settles with a slight overshoot (380ms). Ranks roll; a "↑ 2" chip blurs in and out over 1.8s.
- **sort**: A preset sorts the whole list with the same glide. The pressed preset is whichever the current order matches; with none, the highlight fades and "Your order" shows.
- **keyboard**: Space or Enter on a grip picks the row up (aria-pressed), ↑ and ↓ move it a place at a time with the same glide, Space drops it, Escape puts it back where it started.

## What it handles

Reordering is the most physical thing a list can do, and most sortable lists teleport rows or fade them. This one lifts the row, slides the others out of its way, and works from the keyboard too.

- **Lift and drag**: Press a row’s grip and it lifts and follows your finger exactly, while the rows it passes slide one slot out of the way.
- **It settles, and says how far**: The dropped row settles with a small overshoot and a “↑ 2” chip shows how far it moved before fading.
- **Ranks that roll**: Every rank number rolls to its new place instead of changing at once.
- **Sort presets**: Presets above the list re-sort everything at once and the rows glide. The highlight fades into “Your order” after a manual change.
- **Keyboard reordering**: Space or Enter picks a row up, the arrows move it, Space drops it and Escape puts it back. Every move is announced.
- **Saves safely**: onReorder gives you the new order. Save it, and put the old one back if the save fails.

## Replaces

a drag and drop library, row reorder animation, rank numbers, sort presets, keyboard reordering, screen reader announcements

## Where it fits

- **Priorities and roadmaps**: Rank features, tasks or ideas by dragging them into order.
- **Playlists and queues**: Reorder what plays, ships or gets processed next.
- **Admin ordering**: Order navigation links, form fields or pricing plans.
- **Leaderboards and rankings**: Sort by different measures and still let people arrange by hand.

## Notes

- Each grip is a button named "Reorder …" with instructions attached; picking up, every move, dropping and cancelling are announced politely.
- Rows move with transforms while you drag and only change order on drop, so heavy row content doesn’t re-render on every pointer move.
- For long lists inside a scrolling box, keep the box tall enough to drag within; it doesn’t scroll itself while you drag.
- Installs Number roll. With reduced motion rows move in place without gliding.

License: MIT (https://hairlineui.com/license#components)
