---
title: "Sortable"
description: "Sortable component - drag-and-drop reorderable lists."
slug: "sortable"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeCard, TableCard, ApiSection } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="sortable" title="Sortable" component="sortable">
  Drag-and-drop reorderable list with keyboard support, using the native HTML Drag and Drop API. Supports mouse drag, Alt+Arrow keyboard reordering, and live screen reader announcements.
</PageHeader>

<SkillPanel component="sortable">
  <SkillField label="Native basis">
<SkillText>HTML Drag and Drop API + keyboard reordering for accessible drag-and-drop lists.</SkillText>
  </SkillField>

  <SkillField label="Web Platform APIs">
<SkillRow>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/API/HTML_Drag_and_Drop_API" title="native drag/drop reordering"><code>Drag and Drop API</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/draggable" title="makes elements draggable"><code>draggable</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer" title="drag operation data"><code>DataTransfer</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent" title="sortable-change event on reorder"><code>CustomEvent</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-live" title="live region for screen reader announcements"><code>aria-live</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-visible" title="keyboard-only focus ring"><code>:focus-visible</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion" title="suppresses transitions"><code>prefers-reduced-motion</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors" title="Windows High Contrast Mode"><code>forced-colors</code></ApiPill>
    <ApiPill href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-contrast" title="enhanced contrast support"><code>prefers-contrast</code></ApiPill>
  </SkillRow>
  </SkillField>

  <SkillField label="Classes">
<SkillRow>
    <code>.sortable</code>
    <code>.sortable-item</code>
    <code>.sortable-handle</code>
    <code>.sortable-live</code>
  </SkillRow>
  </SkillField>

  <SkillField label="State attributes (managed by JS)">
<SkillText dense>• <code>data-dragging</code> - item being dragged</SkillText>
  <SkillText dense>• <code>data-over="before|after"</code> - drop indicator position</SkillText>
  <SkillText dense>• <code>data-active</code> - keyboard-focused item</SkillText>
  <SkillText>• <code>aria-disabled="true"</code> - non-interactive item</SkillText>
  </SkillField>

  <SkillField label="Keyboard">
<SkillText dense>• <code>↑</code> / <code>↓</code> - navigate items (vertical)</SkillText>
  <SkillText dense>• <code>←</code> / <code>→</code> - navigate items (horizontal)</SkillText>
  <SkillText dense>• <code>Alt + Arrow</code> - reorder focused item</SkillText>
  <SkillText>• <code>Home</code> / <code>End</code> - jump to first/last item</SkillText>
  </SkillField>
</SkillPanel>

<p class="text-sm font-medium mb-2 mt-6">Default</p>

<p class="text-muted-foreground text-sm mb-4">Drag items to reorder, or use keyboard: Tab to focus, Arrow keys to navigate, Alt+Arrow to move items.</p>

```html example height="18"
<div class="flex items-center justify-center py-10">
  <ul class="sortable" role="listbox" aria-label="Task priority" style="width: 20rem">
    <li class="sortable-item" draggable="true" role="option" tabindex="0">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Build components</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Write documentation</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Deploy to production</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Run tests</span>
    </li>
  </ul>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">Horizontal</p>

<p class="text-muted-foreground text-sm mb-4">Set <code>data-orientation="horizontal"</code> for a row layout. Arrow keys switch to Left/Right.</p>

```html example height="18"
<div class="flex items-center justify-center py-10">
  <ul class="sortable" role="listbox" aria-label="Reorder tags" data-orientation="horizontal" style="max-width: 28rem">
    <li class="sortable-item" draggable="true" role="option" tabindex="0">
      <span>React</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span>TypeScript</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span>CSS</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span>Vite</span>
    </li>
  </ul>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">With Icons</p>

<p class="text-muted-foreground text-sm mb-4">Rich content with icons alongside labels. The handle provides a clear drag affordance.</p>

```html example height="18"
<div class="flex items-center justify-center py-10">
  <ul class="sortable" role="listbox" aria-label="Prioritize features" style="width: 22rem">
    <li class="sortable-item" draggable="true" role="option" tabindex="0">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <i
        data-lucide="layout-dashboard"
        style="width: 1rem; height: 1rem; flex-shrink: 0; color: var(--muted-foreground)"
      ></i>
      <span>Dashboard</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <i data-lucide="settings" style="width: 1rem; height: 1rem; flex-shrink: 0; color: var(--muted-foreground)"></i>
      <span>Settings</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <i data-lucide="users" style="width: 1rem; height: 1rem; flex-shrink: 0; color: var(--muted-foreground)"></i>
      <span>Team</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <i
        data-lucide="bar-chart-3"
        style="width: 1rem; height: 1rem; flex-shrink: 0; color: var(--muted-foreground)"
      ></i>
      <span>Analytics</span>
    </li>
  </ul>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">Disabled Items</p>

<p class="text-muted-foreground text-sm mb-4">Set <code>aria-disabled="true"</code> to lock an item in place: it cannot be dragged, keyboard focus skips it, and it <strong>keeps its position</strong> while the other items move around it. A locked row in the middle is a fixed divider - move an item across it (drag it, or <code>Alt+↑</code>/<code>Alt+↓</code>) and the item steps over the locked slot while its neighbour on the other side shifts across, so the locked row stays put and both groups keep their size.</p>

```html example height="22"
<div class="flex items-center justify-center py-10">
  <ul class="sortable" role="listbox" aria-label="Release steps" style="width: 20rem">
    <li class="sortable-item" draggable="true" role="option" tabindex="0">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Configure database</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Set up CI/CD</span>
    </li>
    <li class="sortable-item" aria-disabled="true" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <i
        data-lucide="lock"
        style="width: 0.875rem; height: 0.875rem; flex-shrink: 0; color: var(--muted-foreground)"
      ></i>
      <span>Security review (locked)</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Deploy application</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Monitor rollout</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Write postmortem</span>
    </li>
  </ul>
</div>
```

<p class="text-sm font-medium mb-2 mt-8">Keyboard Reordering</p>

<p class="text-muted-foreground text-sm mb-4">Focus the list with <kbd style="padding:0.125rem 0.375rem;border:1px solid var(--border);border-radius:var(--radius-sm);font-size:0.75rem;font-family:var(--font-mono);background:var(--muted);">Tab</kbd>, navigate with <kbd style="padding:0.125rem 0.375rem;border:1px solid var(--border);border-radius:var(--radius-sm);font-size:0.75rem;font-family:var(--font-mono);background:var(--muted);">↑</kbd>/<kbd style="padding:0.125rem 0.375rem;border:1px solid var(--border);border-radius:var(--radius-sm);font-size:0.75rem;font-family:var(--font-mono);background:var(--muted);">↓</kbd>, reorder with <kbd style="padding:0.125rem 0.375rem;border:1px solid var(--border);border-radius:var(--radius-sm);font-size:0.75rem;font-family:var(--font-mono);background:var(--muted);">Alt+↑</kbd>/<kbd style="padding:0.125rem 0.375rem;border:1px solid var(--border);border-radius:var(--radius-sm);font-size:0.75rem;font-family:var(--font-mono);background:var(--muted);">Alt+↓</kbd>. A screen reader live region announces each move.</p>

```html example height="18"
<div class="flex items-center justify-center py-10">
  <ul class="sortable" role="listbox" aria-label="Rank languages" style="width: 20rem">
    <li class="sortable-item" draggable="true" role="option" tabindex="0">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>JavaScript</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Python</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Rust</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Go</span>
    </li>
    <li class="sortable-item" draggable="true" role="option" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>TypeScript</span>
    </li>
  </ul>
</div>
```

```html example label="Move buttons" hint="Up / Down buttons per item (.sortable-moves > .sortable-move[data-move=up|down]) reorder by tap or click - the way to sort on a touch screen, where drag-and-drop mostly never starts, and a visible alternative to Alt+Arrow for anyone not using a mouse. Up disables on the first item, Down on the last; focus stays on the button you pressed, so repeated taps keep moving the same item. Each button is labelled for screen readers (&quot;Move Run tests up&quot;) and every move is announced. Dragging still works." height="22"
<div class="flex items-center justify-center py-10">
  <ul class="sortable" aria-label="Task priority" style="width: 22rem">
    <li class="sortable-item" draggable="true" tabindex="0">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Build components</span>
      <span class="sortable-moves">
        <button type="button" class="sortable-move" data-move="up"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m18 15-6-6-6 6" /></svg
        ></button>
        <button type="button" class="sortable-move" data-move="down"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m6 9 6 6 6-6" /></svg
        ></button>
      </span>
    </li>
    <li class="sortable-item" draggable="true" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Write documentation</span>
      <span class="sortable-moves">
        <button type="button" class="sortable-move" data-move="up"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m18 15-6-6-6 6" /></svg
        ></button>
        <button type="button" class="sortable-move" data-move="down"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m6 9 6 6 6-6" /></svg
        ></button>
      </span>
    </li>
    <li class="sortable-item" draggable="true" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Deploy to production</span>
      <span class="sortable-moves">
        <button type="button" class="sortable-move" data-move="up"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m18 15-6-6-6 6" /></svg
        ></button>
        <button type="button" class="sortable-move" data-move="down"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m6 9 6 6 6-6" /></svg
        ></button>
      </span>
    </li>
    <li class="sortable-item" draggable="true" tabindex="-1">
      <span class="sortable-handle">
        <i data-lucide="grip-vertical"></i>
      </span>
      <span>Run tests</span>
      <span class="sortable-moves">
        <button type="button" class="sortable-move" data-move="up"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m18 15-6-6-6 6" /></svg
        ></button>
        <button type="button" class="sortable-move" data-move="down"
          ><svg
            aria-hidden="true"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
          >
            <path d="m6 9 6 6 6-6" /></svg
        ></button>
      </span>
    </li>
  </ul>
</div>
```

```html example label="Between two lists" hint="Lists that share a data-group exchange items: drag from Backlog to Done and back, and still reorder inside each one. Dropping on a row inserts before / after it; dropping on the list's free space adds to the end, and an emptied list stays a drop zone (its data-empty text says so). Keyboard: Alt+→ / Alt+← moves the focused item to the next / previous list. Both lists fire sortable-change - the receiving one with detail.from, the one it left with detail.to." height="22"
<div class="flex flex-wrap gap-4">
  <div class="flex-1 min-w-48">
    <p class="demo-label">Backlog</p>
    <ul class="sortable" id="bin-backlog" aria-label="Backlog" data-group="tasks" data-empty="Drop tasks here"
      ><li class="sortable-item" draggable="true" tabindex="0"
        ><span class="sortable-handle">⋮⋮</span><span>Design review</span></li
      ><li class="sortable-item" draggable="true" tabindex="-1"
        ><span class="sortable-handle">⋮⋮</span><span>Write tests</span></li
      ><li class="sortable-item" draggable="true" tabindex="-1"
        ><span class="sortable-handle">⋮⋮</span><span>Update changelog</span></li
      ></ul
    >
  </div>
  <div class="flex-1 min-w-48">
    <p class="demo-label">Done</p>
    <ul class="sortable" id="bin-done" aria-label="Done" data-group="tasks" data-empty="Drop tasks here"
      ><li class="sortable-item" draggable="true" tabindex="0"
        ><span class="sortable-handle">⋮⋮</span><span>Set up CI</span></li
      ><li class="sortable-item" draggable="true" tabindex="-1"
        ><span class="sortable-handle">⋮⋮</span><span>Pick a name</span></li
      ></ul
    >
  </div>
</div>
<p class="text-sm text-muted-foreground mt-4 mb-0" id="bin-summary"></p>
<script>
  const lists = ['bin-backlog', 'bin-done'].map((id) => document.getElementById(id));
  const summary = () => {
    document.getElementById('bin-summary').textContent = lists
      .map((l) => l.getAttribute('aria-label') + ': ' + (l.api?.getState().config.order.join(', ') || '(empty)'))
      .join(' · ');
  };
  lists.forEach((l) => l.addEventListener('sortable-change', summary));
  setTimeout(summary);
</script>
```

```html example label="Density" hint="Set data-density on the component root to scale its internal whitespace. A whitespace policy, not a zoom: only gaps and padding scale (ratio 0.75 / 1 / 1.25), typography and fixed dimensions stay identical. comfortable matches the unsized default." height="18"
<div class="flex flex-wrap gap-4">
  <div class="flex-1 min-w-48">
    <p class="demo-label">Compact</p>
    <ul class="sortable" aria-label="Tasks compact" data-density="compact"
      ><li class="sortable-item" draggable="true"
        ><span class="sortable-handle">⋮⋮</span><span>Build components</span></li
      ><li class="sortable-item" draggable="true"
        ><span class="sortable-handle">⋮⋮</span><span>Write docs</span></li
      ></ul
    >
  </div>
  <div class="flex-1 min-w-48">
    <p class="demo-label">Comfortable</p>
    <ul class="sortable" aria-label="Tasks comfortable" data-density="comfortable"
      ><li class="sortable-item" draggable="true"
        ><span class="sortable-handle">⋮⋮</span><span>Build components</span></li
      ><li class="sortable-item" draggable="true"
        ><span class="sortable-handle">⋮⋮</span><span>Write docs</span></li
      ></ul
    >
  </div>
  <div class="flex-1 min-w-48">
    <p class="demo-label">Spacious</p>
    <ul class="sortable" aria-label="Tasks spacious" data-density="spacious"
      ><li class="sortable-item" draggable="true"
        ><span class="sortable-handle">⋮⋮</span><span>Build components</span></li
      ><li class="sortable-item" draggable="true"
        ><span class="sortable-handle">⋮⋮</span><span>Write docs</span></li
      ></ul
    >
  </div>
</div>
```

<StatesSection>
  <p class="text-sm text-muted-foreground m-0 mb-3">Named states via the shared State API, driven per instance through the bound <code>api</code>:</p>
  <ul class="text-sm text-muted-foreground m-0 mb-3 ps-5">
    <li><code>default</code> - authored item order; <code>setState('default')</code> restores that order after any drag/keyboard moves, <code>\{ index \}</code> activates one item, and <code>getState().config</code> reports the live <code>order</code> + <code>activeIndex</code></li>
  </ul>
  <p class="text-sm text-muted-foreground">The demo element carries <code>data-state-demo</code>; <code>bun run screenshots</code> drives it via <code>el.api.setState(name)</code> and captures <code>screenshots/\{mode\}/sortable-\{state\}.png</code>.</p>

Machine contract - verified against <a class="link" href="../schemas/sortable.schema.json"><code>sortable.schema.json</code></a> by <code>bun run verify</code>:


```states
| State | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `activeIndex` | `number` | `—` | `-1` | Position of the active item (`-1` = none); `setState('default', { index })` activates one, drag/keyboard follow, mirrored as `data-active-index`. |
```

</StatesSection>

<ApiSection component="sortable" />

<SourceFiles component="sortable" section="css">
  <SourceNote for="css">Styles for the sortable component. Includes drag states, drop indicators, keyboard focus, disabled state, horizontal orientation, and accessibility media queries.</SourceNote>
</SourceFiles>

<SourceFiles component="sortable" section="js">
  <SourceNote for="js">Drag-and-drop + keyboard reordering. Arrow keys navigate, Alt+Arrow reorders, live region announces moves to screen readers.</SourceNote>
</SourceFiles>

</DocPage>
