capevace/livewire-optimistic-ui
Installation:
composer require capevace/livewire-optimistic-ui
Publish the config (optional):
php artisan vendor:publish --provider="Capevace\LivewireOptimisticUI\LivewireOptimisticUIServiceProvider"
First Use Case:
<x-optimistic::injector> component to your Livewire blade file.x-optimistic directive on elements to mark them as optimistic updates.<x-optimistic::injector>
<div x-optimistic>Loading state...</div>
</x-optimistic::injector>
Key Files to Review:
resources/views/vendor/livewire-optimistic-ui/ (published views)config/livewire-optimistic-ui.php (configuration)Form Submission:
<form @submit.prevent="
$optimistic.addTask(text);
$wire.submit('store', { text });
">
wire:loading.delay for smooth transitions.List Management:
x-optimistic on list items for add/remove/edit operations:
<div x-optimistic x-optimistic.removed.remove>
{{ $task['text'] }}
</div>
State Management:
$optimistic object:
$optimistic.addTask('New Task'); // Add before Livewire response
$optimistic.deleteTask('123'); // Remove before Livewire response
@input.debounce for edit operations:
@input.debounce.500="$optimistic.editTask('{{ $task['id'] }}', text)"
x-if:
<div x-optimistic x-if="$optimistic.isPending('editTask-{{ $task['id'] }}')">
Saving...
</div>
wire:submit/wire:click:
wire:submit.prevent="store"
@submit.prevent="$optimistic.addTask(text); $wire.submit()"
Missing wire:key:
wire:key on dynamic list items to prevent hydration issues:
<div wire:key="{{ $task['id'] }}">...</div>
State Collisions:
editTask-{{ $task['id'] }}) to avoid conflicts.Alpine vs. Livewire Conflicts:
$optimistic is initialized before Livewire’s wire:init:
<x-optimistic::injector wire:init="initOptimistic">
<!-- Content -->
</x-optimistic::injector>
$optimistic undefined).$optimistic.getStates() in browser console to debug pending operations.x-optimistic directives to isolate issues.Custom States:
$optimistic object in Alpine:
$optimistic.customState = (id, data) => { /* ... */ };
Global Config:
config/livewire-optimistic-ui.php:
'default_duration' => 3000, // Auto-remove after 3 seconds
Server-Side Sync:
wire:error:
@error('store') $optimistic.revert('addTask') @enderror
How can I help you explore Laravel packages today?