Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Modal Dialogs with Livewire v4 Teleport

Build accessible modal dialogs using teleport.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Modal Component
  2. Component Class

Teleport renders content outside the component DOM.

Modal Component

blade
<button wire:click="$set('show', true)">Open</button>

@teleport('body')
    @if($show)
        <div class="modal-backdrop fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center">
            <div class="modal bg-white p-6 rounded-lg shadow-xl">
                <h2 class="text-xl font-bold mb-4">Modal Title</h2>
                <p>Modal content goes here...</p>
                <button wire:click="$set('show', false)" class="mt-4 px-4 py-2 bg-gray-200 rounded">Close</button>
            </div>
        </div>
    @endif
@endteleport

Component Class

php
class ModalDialog extends Component
{
    public bool $show = false;
    
    public function openModal(): void
    {
        $this->show = true;
    }
    
    public function closeModal(): void
    {
        $this->show = false;
    }
}
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Bookmark Feature with Livewire v41 February 20261 min read