# sortable-drag

> The gesture of reordering a row by dragging one of its items: the others slide aside, the item shows dimmed where it would land, what follows the pointer settles there, the view scrolls at its edges and Escape puts it back, with ui-sdk following the pointer. No render while it moves.

```bash
npx shadcn@latest add @audio-ui/sortable-drag  # React
npx @audio-ui/cli add sortable-drag  # Vue, Svelte, web
```

```ts
import { startSortableDrag } from "@/lib/sortable-drag";
```

- Files: `lib/sortable-drag.ts`
- Depends on: nothing; npm: `@ui-sdk/core`
- Used by: `device-chain`, `use-track-reorder`

| Export | Kind | What it is |
| --- | --- | --- |
| `SortableDragOptions` | interface |  |
| `startSortableDrag` | function | Follows a press on an item until it is released: call it from `pointerdown`. |

## Usage

The gesture of reordering a row by dragging one of its items: the others slide aside, the item shows dimmed where it would land, a copy follows the pointer, and the view scrolls at its edges, all without a React render while it moves. Call it from `pointerdown` with the press itself: a press that travels under 4 px stays a click, and `Escape` ends the drag.

```ts
import { startSortableDrag } from "@/lib/sortable-drag";

onPointerDown={(event) => {
  if (event.button !== 0) return;
  startSortableDrag({
    axis: "y",
    event: event.nativeEvent,
    from: index,
    ghost: () => ghostRef.current,
    items: rows,
    onEnd: (to, released) => {
      if (released && to !== null) move(index, to);
    },
    onLift: () => setDragging(true),
    scroller: listRef.current,
  });
}}
```

[`useTrackReorder`](/docs/hooks/use-track-reorder) and the [Device chain](/docs/components/base/device-chain) reorder with it.
