Sortable
Drag things into the order you want: tasks, menu items, table rows, dashboard cards. One attribute on the list. It works with a mouse, a finger and the keyboard, and tells your app the new order so you can save it.
A list with handles
Drag a row by its handle, or Tab to a handle, press Space, move with the arrows, and Space again.
- Write the product briefToday
- Review the new checkout
- Order packaging
- Plan the holiday sale
- Reply to wholesale leads
Order: t1, t2, t3, t4, t5
<ul class="list-group w-96" id="sort-tasks" data-momo-sortable aria-label="Tasks">
<li class="list-group-item" data-id="t1"><span class="sort-handle"></span><span class="sort-label">Write the product brief</span><span class="badge bg-violet ms-auto">Today</span></li>
<li class="list-group-item" data-id="t2"><span class="sort-handle"></span><span class="sort-label">Review the new checkout</span></li>
<li class="list-group-item" data-id="t3"><span class="sort-handle"></span><span class="sort-label">Order packaging</span></li>
<li class="list-group-item" data-id="t4"><span class="sort-handle"></span><span class="sort-label">Plan the holiday sale</span></li>
<li class="list-group-item" data-id="t5"><span class="sort-handle"></span><span class="sort-label">Reply to wholesale leads</span></li>
</ul>
<p class="text-muted text-sm mt-2" id="sort-order">Order: t1, t2, t3, t4, t5</p>
<script>
document.getElementById('sort-tasks').addEventListener('momo:sort', function () {
document.getElementById('sort-order').textContent = 'Order: ' + Momo.sortable(this).value().join(', ');
});
</script>Add it, step by step
- Add
data-momo-sortableto the list: aul, anol, a.list-group, a grid of cards, or a table'stbody. Its children become draggable. - Give each item a
data-idso you know which is which when you save. - Decide how it is grabbed. An empty
<span class="sort-handle"></span>(six dots) in each item means only the handle drags: best when the item has links or buttons, and on touch it starts at once. Without handles the whole item drags; on touch you press and hold first, so the page still scrolls. - Save the order in
momo:sort. Say no withe.preventDefault()and the item goes back.
list.addEventListener('momo:sort', (e) => {
const { id, fromIndex, index } = e.detail; // 't3', 2, 0
saveOrder(Momo.sortable(list).value()); // ['t3', 't1', 't2', 't4', 't5']
});A grid of cards
Grids work the same. Here without handles, so the whole card drags (press and hold on touch). With the keyboard, left and right move one place, up and down a whole row.
<div class="grid grid-cols-2 sm:grid-cols-3 gap-3" data-momo-sortable aria-label="Dashboard cards">
<div class="stat" data-id="revenue"><span class="stat-label">Revenue</span><span class="stat-value">$48,210</span></div>
<div class="stat stat-violet" data-id="orders"><span class="stat-label">Orders</span><span class="stat-value">1,284</span></div>
<div class="stat" data-id="customers"><span class="stat-label">Customers</span><span class="stat-value">932</span></div>
<div class="stat stat-lime" data-id="refunds"><span class="stat-label">Refunds</span><span class="stat-value">23</span></div>
<div class="stat" data-id="visits"><span class="stat-label">Visits</span><span class="stat-value">9,980</span></div>
<div class="stat stat-yellow" data-id="reviews"><span class="stat-label">Reviews</span><span class="stat-value">4.8</span></div>
</div>Table rows
Put data-momo-sortable on the tbody and a handle in the first cell.
| Move | Product | Price | Stock |
|---|---|---|---|
| Linen shirt | $49.00 | 124 | |
| Canvas tote | $29.00 | 312 | |
| Wool beanie | $24.00 | 88 | |
| Leather wallet | $59.00 | 41 |
<div class="table-wrap">
<table class="table">
<thead><tr><th class="w-12"><span class="visually-hidden">Move</span></th><th>Product</th><th>Price</th><th>Stock</th></tr></thead>
<tbody data-momo-sortable aria-label="Products">
<tr data-id="p1"><td><span class="sort-handle"></span></td><td class="sort-label">Linen shirt</td><td>$49.00</td><td>124</td></tr>
<tr data-id="p2"><td><span class="sort-handle"></span></td><td class="sort-label">Canvas tote</td><td>$29.00</td><td>312</td></tr>
<tr data-id="p3"><td><span class="sort-handle"></span></td><td class="sort-label">Wool beanie</td><td>$24.00</td><td>88</td></tr>
<tr data-id="p4"><td><span class="sort-handle"></span></td><td class="sort-label">Leather wallet</td><td>$59.00</td><td>41</td></tr>
</tbody>
</table>
</div>Between lists
Give several lists the same name, data-momo-sortable="chores", and items move between them. With the keyboard, left and right move a picked-up item to the next list. e.detail.from and e.detail.to are the lists. For a full board with columns, counts and limits, see Kanban.
Today
- Pack 12 orders
- Call the printer
- Update the shop banner
Later
- Photograph the new mugs
- Write the newsletter
<div class="grid sm:grid-cols-2 gap-4">
<div>
<p class="fw-bold mb-2" id="sort-today">Today</p>
<ul class="list-group" data-momo-sortable="chores" aria-labelledby="sort-today">
<li class="list-group-item" data-id="c1"><span class="sort-handle"></span><span class="sort-label">Pack 12 orders</span></li>
<li class="list-group-item" data-id="c2"><span class="sort-handle"></span><span class="sort-label">Call the printer</span></li>
<li class="list-group-item" data-id="c3"><span class="sort-handle"></span><span class="sort-label">Update the shop banner</span></li>
</ul>
</div>
<div>
<p class="fw-bold mb-2" id="sort-later">Later</p>
<ul class="list-group" data-momo-sortable="chores" aria-labelledby="sort-later">
<li class="list-group-item" data-id="c4"><span class="sort-handle"></span><span class="sort-label">Photograph the new mugs</span></li>
<li class="list-group-item" data-id="c5"><span class="sort-handle"></span><span class="sort-label">Write the newsletter</span></li>
</ul>
</div>
</div>Notes
data-sort-fixedon an item keeps it in place (a header row, say). Items added later can be dragged too.- Screen readers hear "sortable item", how to move it, and the new position after each move.
.sort-labelmarks the text read out for an item; without it the whole item's text is used.