Sortable

Animated

A list reordered by drag, keyboard or agent.

Source
$npx @dinachi/cli@latest add sortable
  • Overview

    /docs

  • Installation

    /docs/installation

  • Components

    /docs/components

  • CLI

    /docs/cli

aria-label
disabled

Usage

import { useState } from "react"
import { Sortable, SortableHandle, SortableItem } from "@/components/ui/sortable"

const pages: Record<string, string> = {
  overview: "Overview",
  install: "Installation",
  components: "Components",
}

export function PageOrder() {
  const [order, setOrder] = useState(["overview", "install", "components"])

  return (
    <Sortable aria-label="Pages" value={order} onValueChange={setOrder}>
      {order.map((id) => (
        <SortableItem key={id} id={id} label={pages[id]}>
          <SortableHandle />
          <span>{pages[id]}</span>
        </SortableItem>
      ))}
    </Sortable>
  )
}

Examples

  • Overview

    /docs

  • Installation

    /docs/installation

  • Components

    /docs/components

  • CLI

    /docs/cli

Drag a row by its grip.
import { Sortable, SortableItem, SortableHandle } from '@/components/ui/sortable';

export function Example() {
  const [order, setOrder] = useState(['overview', 'install', 'components']);

  return (
    <Sortable value={order} onValueChange={setOrder}>
      {order.map((id) => (
        <SortableItem key={id} id={id} label={pages[id].title}>
          <SortableHandle />
          <span className="min-w-0 flex-1 truncate text-sm">{pages[id].title}</span>
        </SortableItem>
      ))}
    </Sortable>
  );
}

Notes

Motion tier

Needs motion. add --all skips it; install with add --motion or by name.

Ids as order

value is a string[] of ids, not your objects. Keep the id-to-row lookup yourself.

Refusing a change

To refuse, do not apply next. details.cancel() undoes nothing itself; it marks the change refused, which tells an agent its move failed and suppresses the keyboard announcement.

When to use it

  • Lists whose order is content: navigation, a playlist, a pinned set, form fields.
  • Short lists visible whole; it does not handle dragging across a long scroll.
  • Skip it for lists nobody reorders.

Behaviour

  • Drags start only from the handle. Place it anywhere in the row.
  • The lifted row scales slightly and takes a shadow.
  • Displaced rows animate to their new positions.

API

value
agent
string[]
The current order, as stable ids. Required.
onValueChange
agent
(next, details) => void
Runs for drags, keys and agents. details.reason says which. Required.
disabled
agent
boolean = false
Turns off every handle. Agents are refused too.
aria-label
agent
string
Names the list. The agent reads the same name.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as list.

Reads

label

The list's aria-label. Without one, the bridge names it list.

value

Every row's label in order, joined with commas.

items

Every row's label, in order.

disabled

True when the list is disabled.

Actions

move("Components", 1)

Moves a row, by label then by id, to a position counted from 1. A miss lists the rows.

An agent's move runs through onValueChange like a drag. isAgentChange(details) is true for it; call details.cancel() and skip applying next to refuse, and the agent is told. A position outside the list fails; a disabled list refuses on its own.

await agent.act({
  steps: [{ ref: "list", action: "move", args: ["CLI", 1] }],
})

Accessibility

  • Keeps ul and li semantics.
  • Handles are named after their row, as in Reorder Installation; aria-pressed while lifted.
  • Grab, move, drop and cancel are announced assertively, with position.
  • Escape while lifted cancels the reorder without closing an enclosing dialog.
  • Reduced motion drops the lifted row's scale and shadow fade.