Handling multiple modals.
How can we handle multiple modals within the Nuxt app. I have separate modals for signup, login, password reset and some other things. But I'm unable to manage them globally. I have tried
useState
but it throws the error cannot stringify functions. Here are my implementations:
usemodal works as a controller for single modal
define-modals defines all the modals available.
Inject/provide were not also helpful as state is not SSR friendly in that case.9 Replies
Could I ask, can and do your modals appear in parallel? Or can only one modal be open at any one time?
Only one modal at one time but one modal can open other modal
In that case, you’re better off just storing modal states in
modals
and use a unified composable that toggles a modal based on a key.Yups I believe that can work but I have moved to defining a plug-in. Plugin or a composable either way it's gonna work the same
I thought of doing so but the store seems unnecessary here
It is just a singleton. And you get nothing better than singletons to centralize logic.
Singleton a nice take but how do you think we should move with that, through a plugin or composable
How do these plugins even work nowdays? I have dozen of custom plugins and they were great because they augmented
this
in every component... But there is no this
in <script setup>
so instead of being convenient, it just forces you to add 2 more lines (if example here is any indication). So you only really get the benefits when using plugged singleton in templates and in scripts based on options syntax. So it seems composables are just more straighforward and good enough for new projects.
That said, I see nothing wrong in using both - having $modals
for immediate use in @click
handlers and composable for clean import anywhere else. It's not that you have too choose where the logic goes. Plugin file can just import composable itself AFAIK.Nested modals are not supported in v2, but they will be in v3. In the meantime, I created the following utility in case it helps:
then you can using e.g. as follows:
and you can chain them and use a closure to keep state of slideover 1, and pass it again once reopening it in onSubmit handler for slideover 2. The slideover components should then accept an
onSubmit
handler function or similar to call after being done