In the last blog Pagination in livewire 3.0, we explored implementing pagination in Livewire 3.0. Today, we’ll focus on creating dynamic modals in Livewire 3.0. Modals are an essential part of modern web applications, allowing you to display forms, confirmations, or additional content without navigating away from the page. Livewire makes it simple to build dynamic, reusable modals that integrate seamlessly with your application.
Step 1: What Are Dynamic Modals?
Dynamic modals are reusable pop-ups that can render different content or perform various actions based on the context in which they are used. With Livewire, you can create modals that dynamically update their content and interact with the backend in real time.
Let’s build a reusable Modal Component that can handle dynamic content and actions.
Run the following command to create the Livewire component:
php artisan make:livewire Modal
This will generate:
app/Livewire/Modal.php
– The PHP class for the component.
resources/views/livewire/modal.blade.php
– The Blade view for the component.
Open Modal.php
and update the logic to handle modal visibility and dynamic content:
title = $title;
$this->content = $content;
$this->isOpen = true;
}
public function closeModal()
{
$this->isOpen = false;
}
public function render()
{
return view('livewire.modal');
}
}
$isOpen
: Tracks the modal’s visibility.
openModal
and closeModal
: Handle showing and hiding the modal dynamically.
Listeners: Allow other components to trigger the modal.
Open modal.blade.php
and design the modal layout:
@if ($isOpen)
{{ $title }}
{{ $content }}
@endif
Conditional Rendering: Displays the modal only when $isOpen
is true
.
Dynamic Content: Updates the title and content based on the openModal
method.
Overlay and Styling: Adds a dark background and center alignment for the modal.
You can trigger the modal from any other Livewire component or Blade view using the emit
method:
The button emits the openModal
event with the title and content as parameters.
The Modal
component listens for the event and updates its properties.
To add a form inside the modal, update modal.blade.php
:
Update Modal.php
to handle the form submission:
public $formData = ['name' => ''];
public function submitForm()
{
// Validate and process form data
$this->validate([
'formData.name' => 'required|min:3',
]);
// Perform action (e.g., save to database)
session()->flash('success', 'Form submitted successfully!');
$this->closeModal();
}
Use Alpine.js for smooth open/close animations:
div x-data="{ isOpen: @entangle('isOpen') }" x-show="isOpen" class="fixed inset-0 flex items-center justify-center bg-gray-800 bg-opacity-50 transition-opacity">
{{ $title }}
{{ $content }}
Extend the openModal
method to handle multiple modal types:
public function openModal($type, $data)
{
if ($type === 'confirmation') {
$this->title = 'Confirmation';
$this->content = $data['message'];
} elseif ($type === 'form') {
$this->title = 'Form Submission';
$this->formData = $data;
}
$this->isOpen = true;
}
With these steps, your modal component will:
Dynamically display content and handle actions.
Support form handling and validation.
Integrate advanced features like animations and multiple modal types.
Creating reusable dynamic modal components in Livewire.
Handling dynamic content and events with modals.
Adding advanced features like form handling and animations.
Managing multiple modal types with custom logic.
410 C, Jaina Tower-1, District Center, Janak Puri New Delhi-110058, India.
© Copyright 2025 Wontonee. All Right Reserved.