Improve user feedback with wire:target in Laravel Livewire v3

Providing clear feedback to users during interactions is crucial for creating a responsive and user-friendly application. Laravel Livewire v3 introduces the wire:target directive, which allows you to specify which element should trigger loading indicators. This feature enhances user experience by providing precise feedback during data processing or form submissions.

Understanding wire:target

The wire:target directive lets you control which element triggers the loading state, ensuring that users receive appropriate feedback during interactions. This is especially useful for complex forms or actions where you want to show loading indicators only for specific elements.

Basic Usage

Here’s a basic example to illustrate how wire:target works:

<button wire:click="save" wire:loading.attr="disabled" wire:target="save">
    Save
</button>
<span wire:loading wire:target="save">
    Saving...
</span>

In this example, the wire:loading directive disables the save button and shows the "Saving..." message only while the save method is being processed.

Real-Life Example

Consider a scenario where you have a form with multiple actions, and you want to provide specific loading indicators for each action. Here’s how you can implement this using wire:target:

use Livewire\Component;

class UserProfile extends Component
{
    public $name;
    public $email;
    public $password;

    public function saveProfile()
    {
        // Simulate profile save with a delay
        sleep(2);

        // Save profile data
        // User::update(['name' => $this->name, 'email' => $this->email]);
    }

    public function changePassword()
    {
        // Simulate password change with a delay
        sleep(2);

        // Change password
        // User::update(['password' => bcrypt($this->password)]);
    }

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

    <button type="submit" wire:loading.attr="disabled" wire:target="saveProfile">
        Save Profile
    </button>

    <div wire:loading wire:target="saveProfile">
        <span>Saving profile...</span>
        <img src="/images/spinner.gif" alt="Loading">
    </div>
</form>

<form wire:submit.prevent="changePassword">
    <input type="password" wire:model="password" placeholder="New Password">

    <button type="submit" wire:loading.attr="disabled" wire:target="changePassword">
        Change Password
    </button>

    <div wire:loading wire:target="changePassword">
        <span>Changing password...</span>
        <img src="/images/spinner.gif" alt="Loading">
    </div>
</form>

In this example, separate loading indicators are shown for the profile save and password change actions, providing users with specific feedback for each action.

Conclusion

The wire:target directive in Laravel Livewire v3 is a powerful tool for improving user feedback and interactivity in your applications. By leveraging this feature, you can provide clear and specific loading indicators, enhancing the overall user experience. Give wire:target a try in your next Laravel Livewire project and see how it can make a difference.

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