Completed tasks
6
Synchronization
Waiting for an action
Finalize the launch brief
Resolve the remaining positioning questions and circulate the final brief to product, sales, and support.
data-optimistic automatically.
Search 71 components and 72 controllers by name, category or description.
Inline optimistic Turbo Stream payload for forms, links and custom Turbo triggers
The UI applies three Turbo Stream actions before the request completes. A short demo-only delay makes the provisional state visible. The code below is abridged to the optimistic wiring — the counters and the latency switch around it are demo scaffolding.
Completed tasks
6
Synchronization
Waiting for an action
Resolve the remaining positioning questions and circulate the final brief to product, sales, and support.
data-optimistic automatically.
{{-- The region the server owns: its Turbo Stream response re-renders exactly this id. --}}
<div id="optimistic-task-state-{{ $task->getKey() }}" aria-live="polite" class="min-w-0 flex-1">
@include('components-demo.turbo.optimistic-task-state', ['task' => $task])
</div>
{{-- The client predicts the count the server is about to return. --}}
@php($optimisticCompletedCount = $completedCount + ($isDone ? -1 : 1))
{{-- optimistic--form applies every hw:optimistic below as a Turbo Stream on submit,
and the real response then reconciles — or rolls back — each one. --}}
<hw:form
id="optimistic-task-form-{{ $task->getKey() }}"
:action="route('components.optimistic.tasks.toggle', $task)"
method="patch"
data-controller="optimistic--form"
class="grid justify-items-end gap-3"
>
{{-- Same id the server replaces, rendered here as the provisional version. --}}
<hw:optimistic target="optimistic-task-state-{{ $task->getKey() }}" action="update">
@include('components-demo.turbo.optimistic-task-preview', ['task' => $task])
</hw:optimistic>
{{-- A predicted value can simply be written inline. --}}
<hw:optimistic target="optimistic-completed-count" action="update">
{{ $optimisticCompletedCount }}
</hw:optimistic>
{{-- targets takes a CSS selector and updates every element that matches it. --}}
<hw:optimistic targets="[data-optimistic-sync-state]" action="update">
Synchronizing optimistic state...
</hw:optimistic>
<hw:button
id="optimistic-task-submit-{{ $task->getKey() }}"
type="submit"
size="sm"
data-optimistic-focus-target="submit"
>
<hw:spinner class="hidden size-3.5 form-busy:block" />
<span class="inline-flex items-center gap-1.5 form-busy:hidden">
<x-dynamic-component :component="'lucide-'.$toggleIcon" class="size-3.5" />
{{ $toggleLabel }}
</span>
</hw:button>
@include('components-demo.turbo.optimistic-demo-controls')
</hw:form>
This is a conceptual server excerpt: success and rejection both render the same canonical interactive partial used by the preview.
Templates render the likely result synchronously in the browser.
Laravel validates and toggles the authoritative model state.
The response confirms the prediction or restores the original state.
public function toggleOptimisticTask(Request $request, Task $task): TurboResponse
{
$request->validate([
'reject' => ['nullable', 'boolean'],
'simulate_latency' => ['nullable', 'boolean'],
]);
if ($request->boolean('simulate_latency')) {
usleep(650_000);
}
if ($request->boolean('reject')) {
$completedCount = $this->completedTaskCount();
return turbo_stream()
->replace("optimistic-task-{$task->getKey()}", view('components-demo.examples.optimistic.interactive', compact('task', 'completedCount')), 'morph')
->update('optimistic-completed-count', (string) $completedCount)
->updateAll('[data-optimistic-sync-state]', 'Rolled back to server state')
->toast('error', 'Update rejected', 'The server restored the authoritative task state.')
->withResponse(422);
}
$task->update([
'status' => $task->status === TaskStatus::Done ? TaskStatus::Pending : TaskStatus::Done,
]);
$task->refresh();
$completedCount = $this->completedTaskCount();
return turbo_stream()
->replace("optimistic-task-{$task->getKey()}", view('components-demo.examples.optimistic.interactive', compact('task', 'completedCount')), 'morph')
->update('optimistic-completed-count', (string) $completedCount)
->updateAll('[data-optimistic-sync-state]', 'Server confirmed just now')
->toast('success', 'Task synchronized', "{$task->title} now matches the server state.")
->withResponse();
}