Moey Icons
No results. Try “dialog”, “dark” or “grid”.
Moey extras

Pull to refresh

Pull down at the top of a list to load what's new — the gesture every phone app has. The content follows your finger, an arrow turns when you've pulled far enough, and a spinner stays until your data has arrived.

The whole page — pull down from the top
A panel that scrolls on its own

Touch screens only: on a phone pull with your finger; on a computer use your browser's device mode (touch) — or the button below.

<main data-moey-refresh>
  <ul class="list-group">…</ul>
</main>

<script>
  const list = document.querySelector('main');
  list.addEventListener('moey:refresh', (e) => {
    e.detail.waitUntil(loadNewOrders());   // a promise: the spinner stays until it settles
  });
</script>

Add it, step by step

  1. Put data-moey-refresh on what scrolls. If the page itself scrolls, put it on your main content (or <body>). If a panel scrolls on its own (overflow-y-auto with a height), put it on that panel.
  2. Load in moey:refresh and hand the promise to e.detail.waitUntil(). The spinner stays while it runs, then everything slides back. Without waitUntil it finishes after a short moment.
  3. Update the list when your data arrives — add the new items, replace the old ones, whatever fits.
  4. Give computers a button. A mouse can't pull, so add a refresh button (it is good for keyboard users too) that calls Moey.refresh(el) — the same spinner and event.

Loading data

list.addEventListener('moey:refresh', (e) => {
  e.detail.waitUntil(
    fetch('/api/orders?since=' + newest)
      .then((r) => r.json())
      .then((orders) => orders.forEach(addOrderOnTop))
      .catch(() => Moey.toast({ message: "Couldn't refresh — check your connection", type: 'danger' }))
  );
});
list.addEventListener('moey:refreshed', () => { /* the spinner is gone */ });

With a refresh button

On a computer, the button runs the same refresh — try it:

Orders
  • AK
    Order #10430
    1 items
    2m
  • BK
    Order #10429
    2 items
    9m
  • CK
    Order #10428
    3 items
    16m
  • DK
    Order #10427
    1 items
    23m
  • EK
    Order #10426
    2 items
    30m
  • FK
    Order #10425
    3 items
    37m
<button class="btn btn-sm" onclick="Moey.refresh('#orders')">
  <i class="mi mi-refresh-cw"></i>Refresh
</button>

Good to know

  • It only starts at the very top, and only when pulling down — normal scrolling never changes.
  • The phone browser's own pull-to-reload is switched off where Moey's is used, so the two never fight (and an installed web app has none anyway).
  • Rows you can swipe sideways (Swipe actions), Kanban boards and sliders don't start a pull.
  • Screen readers hear "Refreshing…" and "Updated"; the list is aria-busy while it loads.

Reference

Attribute / APIWhat it does
data-moey-refreshPull to refresh on this element (the page's content, or a panel that scrolls itself)
Event moey:refreshPulled far enough: e.detail.waitUntil(promise) keeps the spinner until it settles
Event moey:refreshedDone; everything slid back
Moey.refresh(el)Refresh from code or a button (returns a promise)
.is-pulling · .is-ready · .is-refreshingThe states, for your own styling
--mo-refresh-pullHow far it is pulled right now (px)