Server-driven UI

Let the controller decide what the page does.

Modals, validation, list updates and toasts from one route, one view and a plain response.

  1. 1

    One view, two presentations

    Link with data-turbo-frame="modal" and the view opens in a modal. Visit the URL directly and the same view renders as a full page.

    resources/views/tasks/edit.blade.php
    <hw:frame-or-page frame="modal" layout="app">
        <hw:modal.title>Edit task</hw:modal.title>
    
        <hw:form :action="route('tasks.update', $task)" method="put" track-frame-src>
            <hw:field name="title" label="Title" required>
                <hw:input :value="$task->title" />
            </hw:field>
    
            <hw:button type="submit">Save changes</hw:button>
        </hw:form>
    </hw:frame-or-page>

    Opens the real editor in a modal.

    Open the editor
  2. 2

    Validation stays where the form is

    Extend TurboFormRequest and keep track-frame-src on the form. A failed submit comes back inside the modal, with old input and errors in place.

    app/Http/Requests/TaskRequest.php
    use Emaia\LaravelHotwireTurbo\Http\Requests\TurboFormRequest;
    
    class TaskRequest extends TurboFormRequest
    {
        public function rules(): array
        {
            return [
                'title' => ['required', 'string', 'max:120'],
            ];
        }
    }

    Save an empty title, then a valid one.

    Open the editor
  3. 3

    Answer with what changed

    Replace the row, close the modal that submitted and confirm with a toast. Without JavaScript the same action redirects, and the toast still arrives.

    app/Http/Controllers/TaskController.php
    public function update(TaskRequest $request, Task $task): mixed
    {
        $task->update($request->validated());
    
        if ($origin = $request->overlayOrigin()) {
            return turbo_stream()
                ->replace($task, view('tasks._card', compact('task')))
                ->closeOverlay($origin)
                ->toast('success', 'Task updated');
        }
    
        return to_route('tasks.show', $task)
            ->toast('success', 'Task updated');
    }

    Save a valid title and watch the three streams land.

    Open the editor
  4. 4

    Ask only when it matters

    The server knows when a delete needs a second look. If the task has subtasks, the action answers with a confirmation instead of deleting.

    app/Http/Controllers/TaskController.php
    public function destroy(Request $request, Task $task): mixed
    {
        if ($task->subtasks()->exists() && ! $request->boolean('confirmed')) {
            return response()->modal()->open(
                view('tasks._confirm-destroy', compact('task')),
                source: route('tasks.confirm-destroy', $task),
            );
        }
    
        $task->delete();
    
        $streams = turbo_stream()->remove($task)->toast('success', 'Task deleted');
    
        if ($origin = $request->overlayOrigin()) {
            $streams->closeOverlay($origin);
        }
    
        return $streams;
    }

    Delete a demo task that has 3 subtasks.

  5. 5

    Feel instant, stay correct

    Show the task as done the moment it is clicked. The server response reconciles it, and a rejected change rolls back to the real state.

    resources/views/tasks/_card.blade.php
    <hw:form :action="route('tasks.complete', $task)" method="patch"
             data-controller="optimistic--form">
        <hw:optimistic :target="dom_id($task, 'status')" action="update">
            <hw:badge>Done</hw:badge>
        </hw:optimistic>
    
        <hw:button type="submit" size="sm">Mark as done</hw:button>
    </hw:form>

    Includes an update the server rejects.

    Try it

What doesn't change

  • Routes and route model binding
  • Form Requests and policies
  • Redirects and flash messages
  • Works without JavaScript

When the browser needs behavior

Masks, uploads, hotkeys and charts live in Stimulus controllers — lazy-loaded and yours to extend.

Continue to controllers