Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

juanborras's avatar

Send an event to a modal

Hey there!

How can I send a close event to an open livewire modal?

Thanks!

1 like
5 replies
LaryAI's avatar
Level 58

To send a close event to an open Livewire modal, you can use Livewire's event system to emit an event from your Livewire component and listen for it in your JavaScript to close the modal. Here's a step-by-step solution:

  1. Emit an Event from Livewire Component:

    In your Livewire component, you can emit an event when you want to close the modal. For example, you might want to emit this event after a form is submitted successfully.

    <?php
    
    use Livewire\Component;
    
    class YourComponent extends Component
    {
        public function submitForm()
        {
            // Your form submission logic here
    
            // Emit the event to close the modal
            $this->emit('closeModal');
        }
    
        public function render()
        {
            return view('livewire.your-component');
        }
    }
    
  2. Listen for the Event in JavaScript:

    In your Blade view or a separate JavaScript file, you can listen for the closeModal event and then close the modal using JavaScript. If you're using a library like Bootstrap or Tailwind UI for modals, you can use their specific methods to close the modal.

    <script>
        document.addEventListener('livewire:load', function () {
            Livewire.on('closeModal', () => {
                // Assuming you're using Bootstrap's modal
                $('#yourModalId').modal('hide');
    
                // If using Tailwind UI or another library, use the appropriate method
                // Example for Tailwind UI:
                // document.getElementById('yourModalId').classList.add('hidden');
            });
        });
    </script>
    
  3. Ensure Your Modal is Set Up Correctly:

    Make sure your modal has the correct ID or class that you're targeting in your JavaScript. Here's a simple example using Bootstrap:

    <!-- Modal -->
    <div class="modal fade" id="yourModalId" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <!-- Your modal content here -->
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="button" class="btn btn-primary">Save changes</button>
                </div>
            </div>
        </div>
    </div>
    

By following these steps, you can effectively send a close event to an open Livewire modal and handle it using JavaScript. Adjust the modal closing logic according to the modal library you are using.

vincent15000's avatar

Do you want to send the event from the controller ? from the view ? from JS ?

Have you thought about using Livewire with AlpineJS to do that ?

juanborras's avatar

@vincent15000 Hey! fist thank you for taking the time :D :D It's filamentphp modal, so I would like to "send" a "close" event to such modal. I've read filament it's working with livewire, so I thought there would be some way to do it from filament

1 like
vincent15000's avatar

@juanborras You should move your post to the Filament channel, you will have more answers.

I don't know Filament.

Please or to participate in this conversation.