Introduction
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.
Step 2: Creating a Modal Component
Let’s build a reusable Modal Component that can handle dynamic content and actions.
1. Generate the Component
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.
2. Define the Component Logic
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');
}
}
Key Points:
$isOpen: Tracks the modal’s visibility.openModalandcloseModal: Handle showing and hiding the modal dynamically.Listeners: Allow other components to trigger the modal.
Step 3: Building the Blade View
Open modal.blade.php and design the modal layout:
@if ($isOpen)
{{ $title }}
{{ $content }}
@endif
Key Features:
Conditional Rendering: Displays the modal only when
$isOpenistrue.Dynamic Content: Updates the title and content based on the
openModalmethod.Overlay and Styling: Adds a dark background and center alignment for the modal.
Step 4: Triggering the Modal from Another Component
Example: Open the Modal with a Button
You can trigger the modal from any other Livewire component or Blade view using the emit method:
How It Works:
The button emits the
openModalevent with the title and content as parameters.The
Modalcomponent listens for the event and updates its properties.
Step 5: Adding Advanced Features
1. Form Handling in Modals
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();
}
2. Animations for Modal Transitions
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 }}
3. Multiple Modal Types
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;
}
Final Output
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.
Key Features Covered Today
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.

