A Livewire modal that stays open through re-renders
A Blade modal for Livewire 3 and 4: open and close it from the component, keep it open on validation errors, reset its form on close. No Alpine.
php artisan bladewell:add modal text-input button
Uses Modal, Text input and Button. New to Bladewell? Install it first.
Why modals close on their own
Livewire re-renders a component by comparing the new HTML with the page and patching the difference. A modal whose open state lives only in the browser, as a class or an attribute set by a script, can be patched back to closed. It happens on the very render that should show "The email has already been taken." The visitor sees the modal vanish and never sees the error.
Bladewell's modal is a native <dialog> that keeps its own state through every Livewire 3 and 4 render, checked in a real browser. It needs no Alpine, and nothing from Livewire, so the same tag works on a plain Blade page.
A form in a modal
Any element with data-modal-open opens it, and data-modal-close closes it, no wire:click or server round trip. The Save button sits in the footer, outside the form, so it names the form with form="profile-form".
<div>
<x-widget.button data-modal-open="edit-profile">Edit profile</x-widget.button>
<x-widget.modal id="edit-profile" title="Edit profile">
<form id="profile-form" wire:submit="save" class="grid gap-4">
<x-widget.text-input label="Name" wire:model="name" required />
<x-widget.text-input label="Email" type="email" wire:model="email" required />
</form>
<x-slot:footer>
<x-widget.button variant="neutral" data-modal-close>Cancel</x-widget.button>
<x-widget.button type="submit" form="profile-form">Save</x-widget.button>
</x-slot:footer>
</x-widget.modal>
</div>
The component validates as usual. A failed save re-renders with the errors under their fields and the modal still open. A good one closes it from the server.
namespace App\Livewire;
use Illuminate\Validation\Rule;
use Livewire\Component;
class EditProfile extends Component
{
public string $name = '';
public string $email = '';
public function mount(): void
{
$this->fill(auth()->user()->only('name', 'email'));
}
public function save(): void
{
$validated = $this->validate([
'name' => ['required', 'string', 'max:120'],
'email' => ['required', 'string', 'email', 'max:255', Rule::unique('users')->ignore(auth()->id())],
]);
$user = auth()->user();
$user->fill($validated);
// A new address isn't verified yet: with MustVerifyEmail, it has to be confirmed again.
if ($user->isDirty('email')) {
$user->email_verified_at = null;
}
$user->save();
// Closes it from the server, once the save went through.
$this->dispatch('modal-close', id: 'edit-profile');
}
public function render()
{
return view('livewire.edit-profile');
}
}
Open and close it from PHP
Dispatch modal-open or modal-close with the modal's id, from any action. Use it to open a modal once a record has loaded, or to close one after a save.
// Anywhere in the component: open it, or close it.
$this->dispatch('modal-open', id: 'edit-profile');
$this->dispatch('modal-close', id: 'edit-profile');
Start empty every time
A "New project" modal shouldn't reopen with the last project's name in it. reset-on-close puts its forms back to how the page last drew them when it closes, and tells wire:model, so the properties follow. Before anything is saved, that's empty. After a render, such as a Create that failed validation, it's what that render drew, so also reset the property in PHP once a save has gone through: $this->reset('projectName').
<x-widget.modal id="new-project" title="New project" reset-on-close>
<form id="project-form" wire:submit="create" class="grid gap-4">
<x-widget.text-input label="Name" wire:model="projectName" required />
</form>
<x-slot:footer>
<x-widget.button variant="neutral" data-modal-close>Cancel</x-widget.button>
<x-widget.button type="submit" form="project-form">Create</x-widget.button>
</x-slot:footer>
</x-widget.modal>
A modal only your code can close
While something runs that mustn't be interrupted, disable-close removes the X, Esc and the backdrop click. Dispatching modal-close from the component still closes it, because the app asked.
<x-widget.modal id="importing" title="Importing your contacts" disable-close>
<p>This takes a minute. Keep the page open.</p>
</x-widget.modal>
"Are you sure?"
Give the button a confirm question. It asks in a dialog styled like your modals, starting on Cancel so Enter can't confirm by accident, and runs wire:click, or submits the form, only once confirmed. No script needed.
<x-widget.button variant="danger" wire:click="delete" confirm="Delete this project?" confirm-message="Its files go too.">Delete</x-widget.button>
Livewire's own wire:confirm still works, with the browser's plain dialog. To ask from a script instead, call modal.confirm(): it resolves true or false. See the confirm example.
Without Livewire
On a plain Blade page the form posts as normal. Validate it into a named error bag with $request->validateWithBag('updateProfile', …), then pass :open="$errors->updateProfile->any()" and bag="updateProfile" on its fields. The modal opens again after a failed submit with the errors and old input in place, and only for its own form: a failed search or newsletter form elsewhere on the page leaves it closed. Old input isn't kept per form, though: if that other form also has an email field, what was typed there fills this one too, so give the fields different names.
{{-- Opens again after a failed submit, with the errors in place. The controller validates into its own bag,
$request->validateWithBag('updateProfile', [...]), so a failed form elsewhere on the page doesn't open it. --}}
<x-widget.modal id="edit-profile" title="Edit profile" :open="$errors->updateProfile->any()">
<form id="profile-form" method="POST" action="{{ route('profile.update') }}" class="grid gap-4">
@csrf
@method('PATCH')
<x-widget.text-input name="name" label="Name" :value="$user->name" bag="updateProfile" required />
<x-widget.text-input name="email" type="email" label="Email" :value="$user->email" bag="updateProfile" required />
</form>
<x-slot:footer>
<x-widget.button variant="neutral" data-modal-close>Cancel</x-widget.button>
<x-widget.button type="submit" form="profile-form">Save</x-widget.button>
</x-slot:footer>
</x-widget.modal>
Drawers, full-screen dialogs and bottom sheets on phones are the same component. See the modal's page.