In this episode, we dive right in and set up some basic modal functionality using Laravel, Livewire, and Alpine.js. If you're following along, make sure you have a fresh Laravel project set up with Breeze (choosing the Livewire and Alpine option), but really, as long as Livewire is installed, you're good to go.
We start by creating a new Livewire component that represents our first modal. This component is added globally to our layout, which means the modal functionality will be available across the whole app.
Next, we build a reusable Blade component as a wrapper for our modals. This helps standardize the look and feel of every modal we create, so you don't have to repeat yourself. We use a slot to inject the actual modal content and style the wrapper so the modal sits nicely in the center of the page.
To toggle the modal's visibility, we set up a visible
property on our Livewire component and pass that into the modal wrapper. We hook Alpine.js up to this with @entangle
, so changing the value from anywhere (either in Alpine or Livewire) will show or hide the modal. We also set up some show
and hide
methods on the component to control the modal easily.
Crucially, we wire everything up so you can show or hide the modal from anywhere in your app. We do this by setting up Livewire $on
event listeners for show
and hide
. Then, we use Alpine to dispatch these events to the modal component. We also organize our modal components neatly into subdirectories to keep things tidy and make targeting individual modals via events easy.
The end result is a super flexible modal system that can be triggered globally, works across your app, and is easy to extend later. Now that the basics are covered, we’ll polish things up and add enhancements in the next episode!