Pull to refresh
Pull down at the top of a list to load what is new: the gesture every phone app has. The content follows your finger, an arrow turns when you have pulled far enough, and a spinner stays until your data has arrived.
A list that refreshes
Touch screens only: on a phone pull with your finger; on a computer use the Refresh button (a mouse cannot pull, and it is good for keyboard users too). Each refresh brings in a new order.
Today's orders
- AKOrder #10430Ana Kim, 1 item2 min
- MROrder #10429Marco Rossi, 2 items9 min
- LFOrder #10428Lena Fischer, 3 items16 min
- SOOrder #10427Sam Okafor, 1 item23 min
- PNOrder #10426Priya Nair, 2 items30 min
- DWOrder #10425Devin Williams, 1 item37 min
<div class="w-96 max-w-full">
<div class="cluster justify-between items-center mb-2">
<b>Today's orders</b>
<button class="btn btn-sm" type="button" onclick="Momo.refresh('#refresh-orders')"><i class="mi mi-refresh-cw"></i> Refresh</button>
</div>
<div class="border rounded bg-surface overflow-auto" style="height: 19rem" data-momo-refresh id="refresh-orders">
<ul class="list-group list-group-flush" id="refresh-list">
<li class="list-group-item"><span class="avatar avatar-sm bg-pink">AK</span><span><b>Order #10430</b><small>Ana Kim, 1 item</small></span><small class="list-group-end">2 min</small></li>
<li class="list-group-item"><span class="avatar avatar-sm bg-cyan">MR</span><span><b>Order #10429</b><small>Marco Rossi, 2 items</small></span><small class="list-group-end">9 min</small></li>
<li class="list-group-item"><span class="avatar avatar-sm bg-yellow">LF</span><span><b>Order #10428</b><small>Lena Fischer, 3 items</small></span><small class="list-group-end">16 min</small></li>
<li class="list-group-item"><span class="avatar avatar-sm bg-lime">SO</span><span><b>Order #10427</b><small>Sam Okafor, 1 item</small></span><small class="list-group-end">23 min</small></li>
<li class="list-group-item"><span class="avatar avatar-sm bg-violet">PN</span><span><b>Order #10426</b><small>Priya Nair, 2 items</small></span><small class="list-group-end">30 min</small></li>
<li class="list-group-item"><span class="avatar avatar-sm bg-orange">DW</span><span><b>Order #10425</b><small>Devin Williams, 1 item</small></span><small class="list-group-end">37 min</small></li>
</ul>
</div>
</div>
<script>
(function () {
var n = 10430, people = [['Helena Chavez', 'HC', 'pink'], ['Blake Howard', 'BH', 'cyan'], ['Iris Tan', 'IT', 'green'], ['Omar Haddad', 'OH', 'orange']];
document.getElementById('refresh-orders').addEventListener('momo:refresh', function (e) {
// your fetch() goes here; this one waits a second and adds an order
e.detail.waitUntil(new Promise(function (done) {
setTimeout(function () {
var p = people[n % people.length];
n++;
var li = document.createElement('li');
li.className = 'list-group-item';
li.innerHTML = '<span class="avatar avatar-sm bg-' + p[2] + '">' + p[1] + '</span><span><b>Order #' + n + '</b><small>' + p[0] + ', 1 item</small></span><small class="list-group-end">now</small>';
document.getElementById('refresh-list').prepend(li);
done();
}, 1000);
}));
});
})();
</script><main data-momo-refresh>
<ul class="list-group">…</ul>
</main>
<script>
const list = document.querySelector('main');
list.addEventListener('momo:refresh', (e) => {
e.detail.waitUntil(loadNewOrders()); // a promise: the spinner stays until it settles
});
</script>Add it, step by step
- Put
data-momo-refreshon what scrolls. If the page itself scrolls, put it on your main content. If a panel scrolls on its own (a height andoverflow-auto), put it on that panel. - Load in
momo:refreshand hand the promise toe.detail.waitUntil(). The spinner stays while it runs, then everything slides back. WithoutwaitUntilit finishes after a short moment. - Update the list when your data arrives: add the new items, replace the old ones, whatever fits.
- Give computers a button that calls
Momo.refresh(el): the same spinner and the same event.
list.addEventListener('momo:refresh', (e) => {
e.detail.waitUntil(
fetch('/api/orders?since=' + newest)
.then((r) => r.json())
.then((orders) => orders.forEach(addOrderOnTop))
.catch(() => Momo.toast({ title: "Couldn't refresh", text: 'Check your connection', color: 'red' }))
);
});
list.addEventListener('momo:refreshed', () => { /* the spinner is gone */ });Good to know
- It only starts at the very top, and only when pulling down: normal scrolling never changes.
- Rows you can swipe sideways (Swipe actions), kanban boards, sliders and carousels do not start a pull.
- Screen readers hear "Refreshing" and "Updated"; the list is
aria-busywhile it loads. - The browser's own pull-to-refresh is turned off on that element, so the two never fight.
Reference
| Attribute / API | What it does |
|---|---|
data-momo-refresh | Pull to refresh on this element (the page's content, or a panel that scrolls itself) |
Event momo:refresh | Pulled far enough: e.detail.waitUntil(promise) keeps the spinner until it settles |
Event momo:refreshed | Done; everything slid back |
Momo.refresh(el) | Refresh from code or a button (returns a promise) |
.is-pulling, .is-ready, .is-refreshing | The states, for your own styling |
--refresh-pull | How far it is pulled right now (px) |