Sortable

Drag & drop reordering for lists and table rows, built on SortableJS.

  • Design the schema
  • Write the migration
  • Build the form
  • Ship it

Usage

<%= ui.sortable url: reorder_tasks_path do %>
  <% @tasks.each do |task| %>
    <%= ui.sortable_item item_id: task.id do %>
      <%= ui.sortable_handle %>
      <%= task.title %>
    <% end %>
  <% end %>
<% end %>
Props
PropTypeDefaultDescription
asSymbolnilRender the list as another component, e.g. `:list` or `:table_tbody`, passing it the remaining options. Without it the list is a `div`.
urlStringnilSave the order after every move: the list POSTs `id` (the item's `item_id`) and `position` (its new index, counting from 0). The list is locked until the answer arrives. If the server refuses, the item goes back to where it was; an error answered with a Turbo Stream is rendered.
groupStringnilLists with the same group exchange items. A move into another list is saved through that list's `url`.

Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

SubcomponentsUse subcomponents below or any HTML.
NameHelperDescription
sortable_itemui.sortable_itemAn item that can be dragged. Accepts any HTML attributes.
sortable_handleui.sortable_handleGrip to drag the item by. Once a list has handles, only the handles start a drag, so inputs and buttons in the items keep working. Shows a six-dot icon unless given content.

Examples

Whole Item Drags

Rails
Hotwire
Turbo
Stimulus
Tailwind
<%= ui.sortable class: "flex flex-wrap gap-2" do %>
  <% %w[Rails Hotwire Turbo Stimulus Tailwind].each do |name| %>
    <%= ui.sortable_item class: "rounded-btn-md border border-border bg-card px-3 py-1.5 text-sm" do %>
      <%= name %>
    <% end %>
  <% end %>
<% end %>

With a Handle

<%= ui.sortable class: "flex flex-col gap-2" do %>
  <% ["Welcome email", "Product tour", "First invoice"].each do |step| %>
    <%= ui.sortable_item class: "flex items-center gap-2" do %>
      <%= ui.sortable_handle %>
      <input type="text" value="<%= step %>" class="form__field" aria-label="Step name">
      <%= ui.btn ui.icon("trash"), variant: :ghost, size: :icon_md, aria: { label: "Remove" } %>
    <% end %>
  <% end %>
<% end %>

Saving the Order

  • Inbox
  • In progress
  • Review
  • Done
<%= ui.sortable as: :list, variant: :divided, url: ui_sortable_reorder_path, class: "rounded-card border border-border" do %>
  <% [[1, "Inbox"], [2, "In progress"], [3, "Review"], [4, "Done"]].each do |id, title| %>
    <%= ui.sortable_item as: :list_item, item_id: id do %>
      <%= ui.sortable_handle %>
      <%= ui.list_content { title } %>
    <% end %>
  <% end %>
<% end %>

When the Server Refuses

  • Locked first
  • Locked second
  • Locked third
<%= ui.sortable as: :list, variant: :divided, url: ui_sortable_reject_path, class: "rounded-card border border-border" do %>
  <% [[1, "Locked first"], [2, "Locked second"], [3, "Locked third"]].each do |id, title| %>
    <%= ui.sortable_item as: :list_item, item_id: id do %>
      <%= ui.sortable_handle %>
      <%= ui.list_content { title } %>
    <% end %>
  <% end %>
<% end %>

Table Rows

Name Role
Ada Lovelace Owner
Grace Hopper Admin
Alan Turing Member
<%= ui.table do %>
  <%= ui.table_thead do %>
    <%= ui.table_tr do %>
      <%= ui.table_th "" %>
      <%= ui.table_th "Name" %>
      <%= ui.table_th "Role" %>
    <% end %>
  <% end %>
  <%= ui.sortable as: :table_tbody do %>
    <% [["Ada Lovelace", "Owner"], ["Grace Hopper", "Admin"], ["Alan Turing", "Member"]].each do |name, role| %>
      <%= ui.sortable_item as: :table_tr do %>
        <%= ui.table_td(class: "w-8") { ui.sortable_handle } %>
        <%= ui.table_td name %>
        <%= ui.table_td role %>
      <% end %>
    <% end %>
  <% end %>
<% end %>

Between Lists

To do

Write the brief
Book the venue

Done

Pick a date
<div class="grid grid-cols-2 gap-4">
  <% { "To do" => ["Write the brief", "Book the venue"], "Done" => ["Pick a date"] }.each do |column, tasks| %>
    <div class="flex flex-col gap-2">
      <h4 class="text-sm font-medium"><%= column %></h4>
      <%= ui.sortable group: "board", class: "flex min-h-24 flex-col gap-2 rounded-card border border-dashed border-border p-2" do %>
        <% tasks.each do |task| %>
          <%= ui.sortable_item class: "rounded-btn-md border border-border bg-card px-3 py-2 text-sm" do %>
            <%= task %>
          <% end %>
        <% end %>
      <% end %>
    </div>
  <% end %>
</div>

Position Fields in a Form

Breakfast
Museum
Lunch
River cruise
<%= form_with url: root_path do |f| %>
  <%= ui.sortable class: "flex flex-col gap-2" do %>
    <% ["Breakfast", "Museum", "Lunch", "River cruise"].each_with_index do |stop, index| %>
      <%= ui.sortable_item class: "flex items-center gap-3" do %>
        <%= ui.sortable_handle %>
        <%= number_field_tag "stops[][position]", index + 1, readonly: true, class: "form__field w-16", data: { sortable_target: "position" }, aria: { label: "Position" } %>
        <span class="text-sm"><%= stop %></span>
      <% end %>
    <% end %>
  <% end %>
<% end %>

Are you absolutely sure?

This cannot be undone.