Creating Dynamic Modals in Livewire 3.0

Dynamic Modals in Livewire 3.0

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:

  1. app/Livewire/Modal.php – The PHP class for the component.

  2. 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:


<?php

namespace App\Livewire;

use Livewire\Component;

class Modal extends Component
{
    public $isOpen = false;
    public $title;
    public $content;

    protected $listeners = ['openModal', 'closeModal'];

    public function openModal($title, $content)
    {
        $this->title = $title;
        $this->content = $content;
        $this->isOpen = true;
    }

    public function closeModal()
    {
        $this->isOpen = false;
    }

    public function render()
    {
        return view('livewire.modal');
    }
}

Key Points:

  1. $isOpen: Tracks the modal’s visibility.

  2. openModal and closeModal: Handle showing and hiding the modal dynamically.

  3. Listeners: Allow other components to trigger the modal.

Step 3: Building the Blade View

Open modal.blade.php and design the modal layout:


<div>
    @if ($isOpen)
        <div class="fixed inset-0 flex items-center justify-center bg-gray-800 bg-opacity-50">
            <div class="bg-white rounded shadow p-6 w-1/3">
                <h2 class="text-xl font-bold mb-4">{{ $title }}</h2>
                <p>{{ $content }}</p>

                <button class="mt-4 px-4 py-2 bg-blue-500 text-white rounded" wire:click="closeModal">
                    Close
                </button>
            </div>
        </div>
    @endif
</div>

Key Features:

  1. Conditional Rendering: Displays the modal only when $isOpen is true.

  2. Dynamic Content: Updates the title and content based on the openModal method.

  3. 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:


<button wire:click="$emit('openModal', 'Confirmation', 'Are you sure you want to proceed?')">
    Open Modal
</button>

How It Works:

  1. The button emits the openModal event with the title and content as parameters.

  2. The Modal component 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:


<form wire:submit.prevent="submitForm">
    <input type="text" wire:model="formData.name" placeholder="Enter your name">
    <button type="submit" class="mt-4 px-4 py-2 bg-green-500 text-white rounded">Submit</button>
</form>

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">
    <div x-show="isOpen" x-transition class="bg-white rounded shadow p-6 w-1/3">
        <h2 class="text-xl font-bold mb-4">{{ $title }}</h2>
        <p>{{ $content }}</p>

        <button class="mt-4 px-4 py-2 bg-blue-500 text-white rounded" wire:click="closeModal">
            Close
        </button>
    </div>
</div>

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:

  1. Dynamically display content and handle actions.

  2. Support form handling and validation.

  3. Integrate advanced features like animations and multiple modal types.


Key Features Covered Today

  1. Creating reusable dynamic modal components in Livewire.

  2. Handling dynamic content and events with modals.

  3. Adding advanced features like form handling and animations.

  4. Managing multiple modal types with custom logic.

© Copyright 2025 Wontonee. All Right Reserved.

×How can I help you?