Pull to refresh

Pull a list down at its top to refresh it, on a touch screen. Everyone else gets a Refresh button that does the same.

Pull to refresh

Needs Alpine and the package's script. See getting started. Without them it is its Refresh button: a link, or a submit for its form.

Use it

<x-ds::pull-refresh :href="route('inbox')" label="Inbox" class="h-[70vh]">
    @foreach ($messages as $message)
        <a href="{{ route('messages.show', $message) }}" class="block px-4 py-3">{{ $message->subject }}</a>
    @endforeach
</x-ds::pull-refresh>

Nobody answering the refresh event, a pull (or the button) follows href: the page that comes back is the refreshed one. Give the root a height to make it the scrolling box; without one, the page scrolls it.

Props

PropTypeDefaultWhat it does
thresholdint (px)72How far the content has to come down before letting go refreshes.
timeoutint (ms)10000Ends a refresh nobody ends. 0 for none.
refreshingboolfalseRender it refreshing. A re-render without it ends a refresh.
hrefstring—Where the Refresh button goes. Without it the button submits its form.
buttonstring | falseRefreshThe button's words; false for none.
labelstring—The region's name.
updatedLabelstringUpdatedSaid when a refresh ends.

Events

EventOnWhen
refreshthe root, bubbling, cancelableA pull past the threshold, or the button. preventDefault() to answer it yourself.
refresheddispatch it on the rootYou are done.

While it refreshes the root is data-refreshing and aria-busy="true".

With Livewire

Answer refresh and call the component. The script marks the root data-refreshing; the re-render that brings the new rows is rendered without it, the morph drops it, and that ends the refresh. Nothing to dispatch.

<x-ds::pull-refresh label="Inbox" class="h-[70vh]" x-on:refresh.prevent="$wire.reload()">
    @foreach ($messages as $message) … @endforeach
</x-ds::pull-refresh>
public function reload(): void
{
    $this->messages = Message::latest()->limit(50)->get();
}

A component that renders nothing new still re-renders the root, so the refresh ends. Render :refreshing="true" to show one the server started.

With Alpine and fetch instead:

<x-ds::pull-refresh x-on:refresh.prevent="await load(); $el.dispatchEvent(new Event('refreshed'))">

Behaviour

  • Touch only. A mouse or a pen does not pull; the button is there.
  • The pull resists more the further it goes; the ring fills to the threshold.
  • Scrolled down, a pull scrolls the box back up, as usual.
  • overscroll-behavior-y: contain on the box keeps the browser's own pull to refresh out of it. The page's own is the browser's.
  • "Updated" is said, politely, when a refresh ends.

Don't

  • Don't hide the button (button="false") unless the page has its own Refresh. The gesture is touch only.
  • Don't leave refresh unanswered with no href and no form. The spinner then runs until the timeout.

More in specs/pull-refresh.md.


From the package repo: edit this page on GitHub.

Was this page helpful?