nawasara/modal
Reusable Blade + Livewire modal components for Laravel. Includes a Blade x-nawasara-modal::modal and a universal Livewire modal you place once in your layout, then open from anywhere with openModal() to load any Livewire component with params.
Installation:
composer require nawasara/modal
The package is auto-discovered, so no additional configuration is required.
First Use Case - Blade Modal:
<x-nawasara-modal::modal id="quick-example" title="Hello World">
This is a simple modal.
</x-nawasara-modal::modal>
<button @click="openModal('quick-example')">Open Modal</button>
<script src="//unpkg.com/alpinejs" defer></script>
First Use Case - Livewire Modal:
resources/views/layouts/app.blade.php):
<livewire:nawasara-modal.livewire-modal />
openModal({
id: 'livewire-modal',
title: 'Livewire Example',
component: 'your.livewire.component',
params: { key: 'value' }
});
Verify Setup:
Component Placement:
<x-nawasara-modal::modal> in your Blade view where the modal should appear (typically near the end of the <body> for proper stacking).id attributes for each modal to avoid conflicts.Content Structure:
<x-nawasara-modal::modal id="form-modal" title="Create User">
<x-slot:default>
<form>
<!-- Form fields -->
</form>
</x-slot:default>
<x-slot:footer>
<button type="submit">Save</button>
<button @click="open = false">Cancel</button>
</x-slot:footer>
</x-nawasara-modal::modal>
Triggering Modals:
<button @click="openModal('form-modal')">Create User</button>
<button @click="openModal('form-modal', { userId: 123 })">Edit User</button>
Styling and Theming:
php artisan vendor:publish --provider="Nawasara\Modal\ModalServiceProvider" --tag="modal-views"
resources/views/vendor/nawasara-modal/.Universal Component:
<livewire:nawasara-modal.livewire-modal /> once in your layout (e.g., app.blade.php).Opening Modals:
openModal JavaScript function to open any Livewire component:
openModal({
id: 'user-modal',
title: 'User Profile',
component: 'user-profile-modal',
params: { userId: 1 }
});
component value should match a registered Livewire component (e.g., UserProfileModal).Livewire Component Setup:
UserProfileModal):
namespace App\Livewire;
use Livewire\Component;
class UserProfileModal extends Component
{
public $userId;
public function mount($userId)
{
$this->userId = $userId;
}
public function render()
{
return view('livewire.user-profile-modal');
}
}
params passed from openModal.Handling Modal Data:
public function mount($userId)
{
$this->userId = $userId;
// Fetch data based on $userId
}
// Inside your Livewire component's JS
window.closeModal('user-modal');
Dynamic Content:
public function updateProfile()
{
// Update logic
$this->emit('modalClosed', 'user-modal');
}
Alpine.js Integration:
<script src="//unpkg.com/alpinejs" defer></script>
npm install alpinejs
Then import it in your JavaScript entry file.Livewire and Blade Hybrid:
Modal Stacking:
openModal function to track the modal stack:
let modalStack = [];
function openModal(config) {
modalStack.push(config.id);
// Existing openModal logic
}
function closeModal(id) {
modalStack = modalStack.filter(stackId => stackId !== id);
// Existing closeModal logic
}
Form Handling:
<x-nawasara-modal::modal id="form-modal" title="Submit Feedback">
<livewire:feedback-form />
</x-nawasara-modal::modal>
public function submit()
{
// Save logic
$this->emit('closeModal', 'form-modal');
}
Accessibility:
<x-nawasara-modal::modal
id="accessible-modal"
title="Accessible Modal"
aria-label="Important notification"
aria-describedby="modal-description"
>
<div id="modal-description">Modal content here.</div>
</x-nawasara-modal::modal>
Tab and Enter support).Livewire Component Registration:
app/Providers/AppServiceProvider.php:
public function boot()
{
$this->app->make(\Livewire\LivewireServiceProvider::class)->boot();
}
Modal ID Conflicts:
id attributes for modals causes the second modal to overwrite the first.id values for each modal.Alpine.js Scope Issues:
x-data to ensure Alpine reactivity:
<div x-data="{ open: false }">
<button @click="open = true">Open</button>
<x-nawasara-modal::modal id="dynamic-modal" x-show="open" x-transition>
<!-- Modal content -->
</x-nawasara-modal::modal>
</div>
Livewire Modal Performance:
wire:ignore for static content.mount method.public function mount($userId)
{
$this->user = User::find($userId); // Load data on mount
}
CSS Conflicts:
php artisan vendor:publish --provider="Nawasara\Modal\ModalServiceProvider" --tag="modal-views"
Then customize the published Blade files.openModal function is defined.How can I help you explore Laravel packages today?