v0.0.2
Demos (demo panel, opens in a new tab)

Drag and drop

Sorts items by drag and drop; with handler, the new order is sent to the server.

zb:sortable zubazar/ui-pro

PRO

This component is in Zubazar UI Pro; Zubazar UI Pro brings Zubazar UI with it. Installing Zubazar UI Pro →

Basic usage

Basic usage: view
BLADE

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.

blade
<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>
PropValues
keyItem identifier, e.g. the record id

With the keyboard

With the keyboard: view
BLADE

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.

blade
<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>
PropValues
labelThe name read on the move buttons and in the announcement; default: the row's own text

Dragging by the handle

Dragging by the handle: view
BLADE

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.

blade
<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>
PropValues
handletrue | false

Saving with Livewire

BLADE

handler is the name of a Livewire method; the dropped item's key and its new position, starting from 0, are sent to it.

blade
<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>
PropValues
handlerLivewire method name

Server method

PHP

The method receives the item's key and its new position; because the records in between shift too, renumber the whole list.

php
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?

PropTypeDefaultDescription
handler—nullLivewire method; called with (key, new position)
handlebooleanfalsetrue: dragged only by the handle icon; false: by the whole row
move-buttonsbooleantruefalse: 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—nullThe 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

1 example

Without the move buttons

Without the move buttons: view
BLADE

: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.

blade
<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>
PropValues
move-buttonstrue | false

Developer notes

Events, hooks and details
  • 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 handler the 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.
Zubazar UI · ZubazarDesigned for people.