Enhance your Livewire components with wire:loading.delay

Providing a smooth and responsive user experience is crucial in modern web applications. One common challenge is managing loading states, especially during quick interactions where flickering loading indicators can be distracting. Livewire offers a powerful directive, wire:loading.delay, to address this issue by delaying the display of loading indicators. This feature improves the overall user experience by preventing unnecessary flickering and providing a more polished interface.

Understanding wire:loading.delay

The wire:loading.delay directive delays the display of loading indicators for a specified time. This is particularly useful for interactions that are expected to complete quickly, as it prevents the loading indicator from appearing and disappearing too rapidly. The delay ensures that the loading indicator is only shown if the action takes longer than the specified delay time.

Basic Usage

Here’s a basic example to illustrate how wire:loading.delay works:

<button wire:click="save" wire:loading.delay>
    Save
</button>

In this example, the Save button is wired with the save method in the Livewire component. The wire:loading.delay directive ensures that the loading indicator (which can be customized using CSS) is only shown if the save method takes longer than the default delay (usually 200ms) to complete.

Real-Life Example

Consider a scenario where you have a form with a submit button, and you want to improve the user experience by using the wire:loading.delay directive to manage the loading state. Here’s how you can implement this:

use Livewire\Component;

class ContactForm extends Component
{
    public $name;
    public $email;
    public $message;

    public function submit()
    {
        // Simulate a delay
        sleep(1);

        // Handle the form submission
        // For example: Contact::create(['name' => $this->name, 'email' => $this->email, 'message' => $this->message]);

        session()->flash('message', 'Thank you for your message!');
    }

    public function render()
    {
        return view('livewire.contact-form');
    }
}
<!-- Blade Template (livewire/contact-form.blade.php) -->
<form wire:submit.prevent="submit">
    <input type="text" wire:model="name" placeholder="Name">
    <input type="email" wire:model="email" placeholder="Email">
    <textarea wire:model="message" placeholder="Message"></textarea>

    <button type="submit" wire:loading.delay>
        Send
    </button>

    <div wire:loading.delay>
        <span>Sending...</span>
    </div>

    @if (session()->has('message'))
        <div>{{ session('message') }}</div>
    @endif
</form>

In this example, the loading indicator "Sending..." is only displayed if the form submission takes longer than the default delay time. This prevents the loading indicator from flickering if the submission is processed quickly.

Pro Tip

Customize the delay time to suit your specific needs by adding a delay time in milliseconds:

<button wire:click="save" wire:loading.delay.500ms>
    Save
</button>

In this example, the loading indicator will be displayed if the save method takes longer than 500 milliseconds to complete.

Conclusion

The wire:loading.delay directive in Livewire is a valuable tool for enhancing user experience by managing loading states more effectively. By delaying the display of loading indicators, you can prevent flickering and provide a smoother, more polished interface. Incorporate this directive into your Livewire components to improve the responsiveness and overall user experience of your application.

Subscribe to Harris Raftopoulos

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe