Swipe actions
Swipe a row to show its buttons: archive, delete, mark as read, the way mail and messaging apps do. A long swipe does the main action straight away. Scrolling up and down still works, and every action is a real button, so nothing depends on the gesture.
A mail list
Drag a row sideways with the mouse, or swipe it on a touch screen: left for Archive and Delete, right for Read. Drag far to the left to delete in one go.
<div class="swipe-list w-96 max-w-full" id="swipe-mail" aria-label="Messages">
<div class="swipe-item" data-momo-swipe data-id="m1">
<div class="swipe-content">
<a class="list-group-item" href="#m1"><span class="avatar avatar-sm bg-pink">AK</span><span><b>Ana Kim</b><small>Order #10421: can it ship to my office?</small></span><small class="list-group-end">9:41</small></a>
</div>
<div class="swipe-start"><button class="swipe-action bg-blue" data-act="read"><i class="mi mi-mail-open"></i>Read</button></div>
<div class="swipe-end">
<button class="swipe-action bg-yellow" data-act="archive"><i class="mi mi-archive"></i>Archive</button>
<button class="swipe-action bg-red" data-act="delete" data-momo-swipe-full><i class="mi mi-trash-2"></i>Delete</button>
</div>
</div>
<div class="swipe-item" data-momo-swipe data-id="m2">
<div class="swipe-content">
<a class="list-group-item" href="#m2"><span class="avatar avatar-sm bg-cyan">MR</span><span><b>Marco Rossi</b><small>Loved the linen tote, thank you!</small></span><small class="list-group-end">8:15</small></a>
</div>
<div class="swipe-start"><button class="swipe-action bg-blue" data-act="read"><i class="mi mi-mail-open"></i>Read</button></div>
<div class="swipe-end">
<button class="swipe-action bg-yellow" data-act="archive"><i class="mi mi-archive"></i>Archive</button>
<button class="swipe-action bg-red" data-act="delete" data-momo-swipe-full><i class="mi mi-trash-2"></i>Delete</button>
</div>
</div>
<div class="swipe-item" data-momo-swipe data-id="m3">
<div class="swipe-content">
<a class="list-group-item" href="#m3"><span class="avatar avatar-sm bg-yellow">LF</span><span><b>Lena Fischer</b><small>When will my refund for #10419 arrive?</small></span><small class="list-group-end">Yesterday</small></a>
</div>
<div class="swipe-start"><button class="swipe-action bg-blue" data-act="read"><i class="mi mi-mail-open"></i>Read</button></div>
<div class="swipe-end">
<button class="swipe-action bg-yellow" data-act="archive"><i class="mi mi-archive"></i>Archive</button>
<button class="swipe-action bg-red" data-act="delete" data-momo-swipe-full><i class="mi mi-trash-2"></i>Delete</button>
</div>
</div>
<div class="swipe-item" data-momo-swipe data-id="m4">
<div class="swipe-content">
<a class="list-group-item" href="#m4"><span class="avatar avatar-sm bg-lime">SO</span><span><b>Sam Okafor</b><small>Do you offer wholesale prices?</small></span><small class="list-group-end">Mon</small></a>
</div>
<div class="swipe-start"><button class="swipe-action bg-blue" data-act="read"><i class="mi mi-mail-open"></i>Read</button></div>
<div class="swipe-end">
<button class="swipe-action bg-yellow" data-act="archive"><i class="mi mi-archive"></i>Archive</button>
<button class="swipe-action bg-red" data-act="delete" data-momo-swipe-full><i class="mi mi-trash-2"></i>Delete</button>
</div>
</div>
<p class="swipe-empty"><i class="mi mi-inbox"></i> No messages left. Nicely done.</p>
</div>
<script>
// the buttons are normal buttons: one click listener handles them all
document.getElementById('swipe-mail').addEventListener('click', function (e) {
var btn = e.target.closest('.swipe-action');
if (!btn) return;
var row = btn.closest('.swipe-item'), name = row.querySelector('b').textContent;
if (btn.dataset.act === 'read') {
var unread = row.classList.toggle('is-read');
btn.lastChild.textContent = unread ? 'Unread' : 'Read';
} else {
Momo.swipe.remove(row);
Momo.toast({ title: btn.dataset.act === 'delete' ? 'Deleted' : 'Archived', text: name, color: btn.dataset.act === 'delete' ? 'red' : 'yellow', icon: btn.dataset.act === 'delete' ? 'trash-2' : 'archive' });
}
});
</script>
<style>#swipe-mail .is-read b { font-weight: 500; } #swipe-mail .is-read { color: var(--momo-muted); }</style>Add it, step by step
- Wrap the row in
<div class="swipe-item" data-momo-swipe>and put the row itself in.swipe-content. The row can be anything: a list group item, a card, a table-like line..swipe-listaround the items gives the box and the lines between them; a.swipe-emptymessage inside it shows when the last row is gone. - Add the buttons.
.swipe-endholds the ones shown by swiping toward the start (to the left in left-to-right languages: the usual place for Archive and Delete);.swipe-startthe ones shown by swiping the other way (Read, Pin). One side is fine. - Color them with a
.bg-*class (bg-redfor Delete,bg-yellowfor Archive,bg-bluefor Read). - Pick the long-swipe action with
data-momo-swipe-full, usually the outermost button. A swipe past 60% of the row presses it. - Handle the buttons like any button, with a click listener. Momo closes the row afterwards;
Momo.swipe.remove(row)folds it away when you delete or archive it.
list.addEventListener('click', (e) => {
const btn = e.target.closest('.swipe-action');
if (!btn) return;
const row = btn.closest('.swipe-item');
if (btn.dataset.act === 'delete') {
deleteMessage(row.dataset.id);
Momo.swipe.remove(row); // folds the row away, then removes it
}
});
Momo.swipe.open(row, 'end'); // show a side from code
Momo.swipe.close(row);
row.addEventListener('momo:swipe', (e) => console.log(e.detail.open, e.detail.side));One side only
A to-do list where a swipe marks a task done.
Pack 12 orders
Call the printer
Update the shop banner
All done for today.
<div class="swipe-list w-80 max-w-full" id="swipe-todo" aria-label="Tasks">
<div class="swipe-item" data-momo-swipe><div class="swipe-content"><div class="list-group-item"><i class="mi mi-circle"></i> Pack 12 orders</div></div><div class="swipe-end"><button class="swipe-action bg-green" data-momo-swipe-full><i class="mi mi-check"></i>Done</button></div></div>
<div class="swipe-item" data-momo-swipe><div class="swipe-content"><div class="list-group-item"><i class="mi mi-circle"></i> Call the printer</div></div><div class="swipe-end"><button class="swipe-action bg-green" data-momo-swipe-full><i class="mi mi-check"></i>Done</button></div></div>
<div class="swipe-item" data-momo-swipe><div class="swipe-content"><div class="list-group-item"><i class="mi mi-circle"></i> Update the shop banner</div></div><div class="swipe-end"><button class="swipe-action bg-green" data-momo-swipe-full><i class="mi mi-check"></i>Done</button></div></div>
<p class="swipe-empty"><i class="mi mi-circle-check"></i> All done for today.</p>
</div>
<script>
document.getElementById('swipe-todo').addEventListener('click', function (e) {
var btn = e.target.closest('.swipe-action');
if (btn) Momo.swipe.remove(btn.closest('.swipe-item'));
});
</script>Good to know
- Only one row is open at a time; touching anywhere else closes it. A tap on an open row closes it instead of following its link.
- Keyboard: Tab reaches the buttons and the row opens to show the one in focus; Esc closes it.
- Going up or down first is treated as a scroll, so lists scroll normally. Right-to-left pages flip the sides.
- A dragged row stays straight: it only slides.
Reference
| Class / attribute | What it does |
|---|---|
.swipe-item[data-momo-swipe] | A row that can be swiped |
.swipe-content | The row itself (it slides) |
.swipe-start, .swipe-end | The buttons on each side |
.swipe-action + .bg-* | A button: an icon over a word |
data-momo-swipe-full | The action a long swipe presses |
.swipe-list | A boxed list of swipe items with lines between them |
.swipe-empty | A message in the list, shown when no rows are left (without one, the empty list hides) |
Momo.swipe.open / close / remove | From code; remove returns a promise |
Event momo:swipe | { open, side } when a row opens or closes |