microweber-deps/wire-elements-modal
Installation:
composer require wire-elements/modal
Ensure your project uses Livewire v3 (check laravel/livewire version in composer.json).
Publish Assets (if needed):
npm install && npm run dev
The package includes Tailwind CSS by default—customize via your existing tailwind.config.js.
First Modal Usage:
@livewire('modal')
<button wire:click="$dispatch('openModal', {component: 'user.create'})">
Create User
</button>
Define a Modal Component:
Create a Livewire component (e.g., app/Http/Livewire/User/Create.php) and register it in ModalServiceProvider (see Implementation Patterns).
$dispatch('closeModal') in the component’s methods.Example:
<!-- Button to open modal -->
<button wire:click="$dispatch('openModal', {component: 'user.edit', id: 1})">
Edit User
</button>
<!-- Modal content (auto-rendered by the package) -->
Register your modal components in the service provider (auto-discovered via config/livewire.php):
// app/Providers/ModalServiceProvider.php
public function register()
{
$this->app->make(\WireElements\Modal\ModalService::class)->register([
'user.create' => \App\Http\Livewire\User\Create::class,
'user.edit' => \App\Http\Livewire\User\Edit::class,
]);
}
Pass data via the openModal event payload:
<button wire:click="$dispatch('openModal', {
component: 'user.view',
data: { id: 5, name: 'John Doe' }
})">
View Profile
</button>
Access in the component:
public $modalData;
protected $listeners = ['openModal' => 'handleModal'];
public function handleModal($data)
{
$this->modalData = $data['data'];
}
Extend the default modal template by publishing views:
php artisan vendor:publish --tag=wire-elements-modal-views
Customize resources/views/vendor/wire-elements-modal/modal.blade.php.
Use unique keys to stack modals:
<button wire:click="$dispatch('openModal', {
component: 'settings.profile',
key: 'profile-settings'
})">
Profile Settings
</button>
Combine with Livewire forms for seamless CRUD:
// In your modal component
public function save()
{
$this->validate([...]);
User::create($this->formData);
$this->dispatch('closeModal');
$this->dispatch('modalSaved');
}
Livewire v3 Compatibility:
$emit calls are replaced with $dispatch (see README).wire:ignore on modal containers to prevent re-renders:
<div wire:ignore x-data="{ open: false }">
@livewire('modal')
</div>
Component Not Found:
ModalServiceProvider.Styling Conflicts:
<div class="bg-white dark:bg-gray-800 p-6 rounded-lg shadow-lg">
{{ $slot }}
</div>
Event Listener Leaks:
mount() to avoid memory leaks:
public function mount()
{
$this->removeListener('openModal');
}
wire:click="$dispatch('openModal', {component: 'test'})" and inspect browser console for errors.dd($this->modalData) in your component’s handleModal method.php artisan view:clear && php artisan config:clear
Custom Modal Sizes:
Add size classes via the openModal payload:
$dispatch('openModal', {
component: 'user.create',
size: 'lg' // Maps to Tailwind's w-full max-w-lg
})
Extend the template to handle size:
@if($size === 'lg')
<div class="max-w-2xl">
@else
<div class="max-w-md">
@endif
Modal Footers: Pass footer content via the payload:
$dispatch('openModal', {
component: 'user.delete',
footer: '<button wire:click="confirm">Delete</button>'
})
Render in the template:
@if(isset($footer))
<div class="modal-footer">
{!! $footer !!}
</div>
@endif
Animation Hooks: Use Alpine.js to add transitions:
<div x-transition
x-show="open"
@before-enter="open = true"
@after-leave="open = false">
@livewire('modal')
</div>
How can I help you explore Laravel packages today?