How to use Alpine to make your LiveView feel more responsive!
To start: when creating LiveViews in Phoenix, I'd highly recommend you turn on the latency simulator. This will help to give you a better idea of how it feels to use your app under different conditions. You can do this in your browser's console:
window.liveSocket.enableLatencySim(500)
When interacting with your LiveViews, your console will continue to remind you that this setting is enabled.
You might be surprised how different your app feels when a little extra latency comes into play, and it can help show you where you might be leaning on LiveView for the wrong things (i.e. "general UI events that do not need the server in the first place").
Okay, so back to the matter at hand. When using LiveView, one way you can indicate to the client that you are waiting on the server is to add phx-page-loading to your tag. For example, this button will emit the
phx:page-loading-start event:
<button phx-click="some_event" phx-page-loading class="...">Show More</button>
Once the server responds to our event, the phx:page-loading-stop event will be emitted. By default in Phoenix we get a loading indicator using the topbar library, which will work great in a lot of cases. You can also provide your own loading state changes with or instead of topbar by hooking in to those events.
Let's say we wanted to have an interactive search bar, that pulls in results as someone is typing in their search. However we don't want to hit our server on every single character typed. The obvious solution in LiveView is to use the
phx-debounce binding:
<input phx-debounce="1500" phx-page-loading value="<%= @search_query %>" name="search_field" type="search" class="...">
Awesome, now we'll only update the server with our query at most every 1.5 seconds. Job done.
🤚🛑 But hold up!... Now as our client starts typing in their query, they won't receive any feedback that we're working to resolve their query. Our trusty phx-page-loading event won't fire until after 1.5 seconds.
This is where alpinejs can give us a hand. Alpine can listen for regular events on our client as well as listen for custom events like the ones Phoenix emits on the window. This allows us to provide feedback to the client that we're working on their request immediately, even though we've delayed contacting the server.
To start, we'll have alpine track some loading state using the form (or some other container that will have our input in it's scope):
<form x-data="{ loading: false }" phx-change="search_club" phx-target="<%= @myself %>" class="...">
Now when we start typing in our search input, we'll have alpine change our loading state to true:
<form x-data="{ loading: false }" phx-change="search_club" phx-target="<%= @myself %>" class="...">
<!-- Loading Indicators + Stuff -->
<input x-on:input="loading = true" phx-page-loading value="<%= @search_query %>" phx-debounce="1500" name="search_field" type="search" class="...">
</form>
Great, so now we can use this loading state to decide whether or not we want to show a component, apply some css classes, etc. But how to make it stop? We don't want to arbitrarily decide to stop the loading indicator, we want to stop it when the server responds to our request.
Well, because alpine can listen to custom events, we can easily have it listen for the phx:page-loading-stop event on the window by modifying our form:
<form
x-data="{ loading: false }"
x-on:phx:page-loading-stop.window="loading = false"
phx-change="search_club" phx-target="<%= @myself %>" class="...">
Now once we have our response, we can correctly update the loading state! This means our client gets immediate feedback when starting or modifying their query, even though we're only asking Phoenix to send an update every 1.5 seconds.
Here's a full example where we can decide to switch out our search icon with a loading indicator while we wait for our response:
<div class="bg-white py-2 px-4 space-y-6 max-w-xl sm:p-4">
<div class="flex-1 flex justify-between px-4 md:px-0">
<div class="flex-1 flex">
<form x-data="{ loading: false }" x-on:phx:page-loading-stop.window="loading = false" phx-change="search_club" phx-target="<%= @myself %>" class="w-full flex md:ml-0">
<label for="search_field" class="sr-only">Search</label>
<div class="relative w-full text-gray-400 focus-within:text-gray-600">
<div class="pointer-events-none absolute inset-y-0 left-0 flex items-center">
<svg x-show="loading == false" class="flex-shrink-0 h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
<path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd" />
</svg>
<svg x-show="loading == true" class="animate-spin -ml-1 mr-3 h-5 w-5 text-gray-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
</div>
<input x-on:input="loading = true" phx-page-loading value="<%= @search_query %>" phx-debounce="1500" name="search_field" placeholder="Search for stuff" type="search" class="hidden h-full w-full border-transparent py-2 pl-8 pr-3 text-sm text-gray-900 placeholder-gray-500 focus:outline-none focus:ring-0 focus:border-transparent focus:placeholder-gray-400 sm:block">
</div>
</form>
</div>
</div>
</div>