Installation
pnpm dlx shadcn@latest add @diceui/sortableLayout
Import the parts, and compose them together.
import {
Sortable,
SortableContent,
SortableItem,
SortableItemHandle,
SortableOverlay,
} from "@/components/ui/sortable";
return (
<Sortable>
<SortableContent>
<SortableItem>
<SortableItemHandle />
</SortableItem>
<SortableItem />
</SortableContent>
<SortableOverlay />
</Sortable>
)Every part accepts a render prop, so an item can be composed with your own element or component instead of the default div.
<SortableItem value={item.id} render={<Card />}>
{item.name}
</SortableItem>Usage
With Primitive Values
When using primitive arrays (strings, numbers), the getItemValue prop is optional. The component will automatically use the item itself as the unique identifier.
const [items, setItems] = React.useState(["Item 1", "Item 2", "Item 3"]);
<Sortable value={items} onValueChange={setItems}>
<SortableContent>
{items.map((item) => (
<SortableItem key={item} value={item}>
{item}
</SortableItem>
))}
</SortableContent>
</Sortable>With Object Arrays
When using object arrays, the getItemValue prop is required to extract a unique identifier from each item.
const [items, setItems] = React.useState([
{ id: 1, name: "Item 1" },
{ id: 2, name: "Item 2" },
]);
<Sortable
value={items}
onValueChange={setItems}
getItemValue={(item) => item.id}
>
<SortableContent>
{items.map((item) => (
<SortableItem key={item.id} value={item.id}>
{item.name}
</SortableItem>
))}
</SortableContent>
</Sortable>Examples
With Dynamic Overlay
Display a dynamic overlay when an item is being dragged.
With Handle
Use SortableItemHandle as a drag handle for sortable items.
With Primitive Values
Use an array of primitives (string or number) instead of objects for sorting.
API Reference
Sortable
The main container component for sortable functionality.
Prop
Type
SortableContent
Container for sortable items. Multiple SortableContent components can be used within a Sortable component.
Prop
Type
SortableItem
Individual sortable item component.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the item is disabled. |
[data-dragging] | Present when the item is being dragged. |
SortableItemHandle
A button component that acts as a drag handle for sortable items.
Prop
Type
| Data Attribute | Value |
|---|---|
[data-disabled] | Present when the item is disabled. |
[data-dragging] | Present when the parent sortable item is being dragged. |
The component renders a button and adds the following styles:
select-nonefor pointer eventscursor-grabwhen not dragging (unlessflatCursoris true)cursor-grabbingwhen dragging (unlessflatCursoris true)cursor-defaultwhenflatCursoris true
SortableOverlay
The overlay component that appears when an item is being dragged.
Prop
Type
Accessibility
Keyboard Interactions
| Key | Description |
|---|---|
| EnterSpace | Picks up the sortable item for reordering when released, and drops the item in its new position when pressed again. |
| ArrowUp | Moves the sortable item up in vertical orientation. |
| ArrowDown | Moves the sortable item down in vertical orientation. |
| ArrowLeft | Moves the sortable item left in horizontal orientation. |
| ArrowRight | Moves the sortable item right in horizontal orientation. |
| Esc | Cancels the sort operation and returns the item to its original position. |