Best way to manage modals in Vue
Since bootstrap was released there was one preferred way to create and manage modals in your app. Putting HTML markup on your page, giving an id and small piece of javascript to open it by event like click.
The problem
But let’s say you have a table with dynamic content, it might have 5 or 5000 items. So, having one modal for one record in the table, you will have the same number of modals in the markup. Your page will bloat and takes longer to load.
The solution
In my projects I have a tons of modals. Mostly for managing items in tables but also for other tasks, like login, confirming actions, etc. I didn’t want to copy/paste my modals everywhere when I want to use them. Instead we can use only one instance of modal component for everything. To make it possible we need to use global store.
I prefer to keep methods to change store’s state in composable function. So we will use only this function to manage our modal from anywhere. Let’s create it.
We have default options here to open modal and all methods to manage it. The most important thing here is that we divided dismiss and close into two separate methods. The main difference between them is that close method can get data that we can pass from modal up to parent component where we opened this modal.
The last thing you have to do is to implement your own modal component. There are many ways to implement it, I give you a basic idea how you can do it.
You usually have to place it in your App.vue or default layout.
So now we can open our modal! Create the content of modal in separate component and pass it in options of open
method. Inside of this Login.vue example component, you have to call close
method from useModal()
composable function to close the modal with some data from login form.
That’s it! We created flexible modal system for a project of any complexity. If you have any question you can reach me out in comments below or via social media.
Thanks!