Drag and drop
Sorts items by drag and drop; with handler, the new order is sent to the server.
zb:sortable zubazar/ui-pro
This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →
Basic usage
Each row is an zb:sortable.item with a unique key; drag a row with the mouse, or from the grip on the left on touch screens.
<zb:sortable>
<zb:sortable.item key="1">Electronics</zb:sortable.item>
<zb:sortable.item key="2">Home and living</zb:sortable.item>
<zb:sortable.item key="3">Clothing</zb:sortable.item>
<zb:sortable.item key="4">Beauty</zb:sortable.item>
</zb:sortable>
| Prop | Values |
|---|---|
key | Item identifier, e.g. the record id |
With the keyboard
On the keyboard, the Move up and Move down buttons on the right of each row move it by one place; give rows with badges a short name with label.
<zb:sortable>
<zb:sortable.item key="1" label="Electronics"><span class="flex items-center justify-between gap-3">Electronics <zb:badge>12 products</zb:badge></span></zb:sortable.item>
<zb:sortable.item key="2" label="Home and living"><span class="flex items-center justify-between gap-3">Home and living <zb:badge>8 products</zb:badge></span></zb:sortable.item>
<zb:sortable.item key="3" label="Clothing"><span class="flex items-center justify-between gap-3">Clothing <zb:badge>5 products</zb:badge></span></zb:sortable.item>
</zb:sortable>
| Prop | Values |
|---|---|
label | The name read on the move buttons and in the announcement; default: the row's own text |
Dragging by the handle
With handle, dragging starts only from the handle icon on the left, so the row does not shift while you use its buttons and links.
<zb:sortable handle>
<zb:sortable.item key="1"><span class="flex items-center justify-between gap-3">Electronics <zb:button size="sm" variant="ghost">Edit</zb:button></span></zb:sortable.item>
<zb:sortable.item key="2"><span class="flex items-center justify-between gap-3">Home and living <zb:button size="sm" variant="ghost">Edit</zb:button></span></zb:sortable.item>
<zb:sortable.item key="3"><span class="flex items-center justify-between gap-3">Clothing <zb:button size="sm" variant="ghost">Edit</zb:button></span></zb:sortable.item>
</zb:sortable>
| Prop | Values |
|---|---|
handle | true | false |
Saving with Livewire
handler is the name of a Livewire method; the dropped item's key and its new position, starting from 0, are sent to it.
<zb:sortable handler="updateOrder">
@foreach ($categories as $category)
<zb:sortable.item :key="$category->id" wire:key="category-{{ $category->id }}">{{ $category->name }}</zb:sortable.item>
@endforeach
</zb:sortable>
| Prop | Values |
|---|---|
handler | Livewire method name |
Server method
The method receives the item's key and its new position; because the records in between shift too, renumber the whole list.
public function updateOrder(int $categoryId, int $newPosition): void
{
$order = Category::orderBy('position')->pluck('id')
->reject(fn (int $id) => $id === $categoryId)
->values();
$order->splice($newPosition, 0, [$categoryId]);
foreach ($order as $position => $id) {
Category::whereKey($id)->update(['position' => $position]);
}
}
Props
The values the component takes. What are props and slots?
| Prop | Type | Default | Description |
|---|---|---|---|
handler | — | null | Livewire method; called with (key, new position) |
handle | boolean | false | true: dragged only by the handle icon; false: by the whole row |
move-buttons | boolean | true | false: the kit's Move up / Move down buttons are not drawn, dragging stays; add your own buttons and announce the move yourself |
sortable.item: key | — | — | Required; item identifier |
sortable.item: label | — | null | The name read on the Move up / Move down buttons and in the announcement of the new place; otherwise the row's own text (without nested lists, buttons, form controls, icons and hidden elements). Give it on rows that show an order number or a badge |
Advanced
Without the move buttons
:move-buttons="false" removes the kit's Move up and Move down buttons, and dragging keeps working. Add your own buttons for keyboard users, and announce the move to screen readers yourself.
<zb:sortable :move-buttons="false">
<zb:sortable.item key="1">Electronics</zb:sortable.item>
<zb:sortable.item key="2">Home and living</zb:sortable.item>
<zb:sortable.item key="3">Clothing</zb:sortable.item>
</zb:sortable>
| Prop | Values |
|---|---|
move-buttons | true | false |
Developer notes
- A row is dragged with the mouse from anywhere on it (not from a link, button or field in it); on touch screens a swipe over the rows scrolls the page. Escape puts a dragged row back.
- Without
handler, the new order stays in the browser only; when the page is reloaded, the old order returns. - Dragging needs neither Alpine nor a drag-and-drop library.
- The move buttons do what a drop does and call
handlerthe same way; the first row has no move up button and the last no move down button. - The new place of a moved row is announced to screen readers ("Clothing moved to position 2") and the focus stays on the same button; in nested lists each row moves within its own list.
- To spread the content across the row, give the slot a flex container.
- In loops, give every row a
wire:key.