-
Notifications
You must be signed in to change notification settings - Fork 615
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(Modal): open programmatically #1319
Conversation
The latest updates on your projects. Learn more about Vercel for Git βοΈ
|
Thanks for the initiative! π The initial implementation looks good to me, I'd say this is the way to go. |
When calling Lines 27 to 42 in 68e7de3
If yes, how would you proceed to make the values reactive, and what would the API look like user-facing ? |
Just an idea but wouldn't it be easier to make those values reactive to pass the component and the props directly to |
I'm not sure, a very basic usecase would be to open a modal A or B given a branch in your code. And as a composable should be called during the setup function, you can't know at this moment what modal you would like to display. Options could be passed to const modal = useModal()
if (success) {
modal.open(ModalSuccess, data)
} else {
modal.open(ModalError, error)
} |
Yeah my solution would require to instantiate |
Demo of nested modals using |
Amazing work, thanks a lot!! ππ |
I made sure to have |
hey @noook! look forward this for a long time, great job! one more suggestion, can you add async/await support? like also would be awesome if we open nested modals. currently it supports only one modal, right? please let me know your thoughts, if look forward to contribute if agreed with me! |
@yulafezmesi It does support nested modal. You have two ways of achieving it: // app.vue
const modal = useModal()
modal.open(YourModal)
// YourModal.vue
modal.open(AnotherModal) // This will swap the component displayed, and will briefly close then reopen the modal You can check the demo here: https://github.com/nuxt/ui/assets/19751938/57188536-bbde-4747-a5cf-73ee062b0519 The second way of achieving it is with the events listener: // app.vue
const modal = useModal()
modal.open(YourModal, {
// prop and emit types inferred from your component :)
onConfirm() {
modal.open(AnotherModal)
}
})
// YourModal.vue
const props = defineProps<{
confirm: []
}>()
const onConfirm = () => emit('confirm') The second example demonstrates perfectly how you could handle a callback when clicking on a confirm button within your component. This mimics the behaviour of https://vueuse.org/core/useConfirmDialog/, except it does not use async/await. If you really need to await something from the modal, I think you could wrap it like this: const modal = useModal()
function openConfirmModal() {
return new Promise((resolve, reject) => {
modal.open(YourConfirmModal, {
onConfirm: (eventArg) => resolve(eventArg),
onClose: () => reject()
})
})
}
async function start() {
try {
await openConfirmModal()
console.log('user did confirm')
} catch {
console.log('user did not confirm')
}
} |
@noook that you for the work around, totally make sense to async usage like this. however, really wondering swapping components causes losing state for previous component? I mean i can easly back to old component where the trigger second modal? |
The component should get destroyed. Also, the current implementation limits us to open a modal with unreactive props. If saving the state is part of your feature, maybe the usage of a store should be relevant here |
what i want to achieve by nested modals basically this: Screen.Recording.2024-02-09.at.23.11.18.mov |
Oh this is not possible at all with the current implementation sorry. Earlier I meant chained instead of nested |
wouldn't be nice if can get support this? i'm thinking we can use array of |
@noook -- Is there a way to close the modal from another component? |
@anthonyfranc i need to check. I think the reason why this is happening is because I don't let elapse enough time before reseting the modal state's component |
@noook nice feature. |
@avinean Feel free to open an issue so we can discuss it there instead. As of now I don't have anything in mind regarding the implementation of this feature |
Did you happen to figure out the issue with this? |
this example closes the parent modal, and opens then nested modal, and breaks the app. |
Hello, any solution to this problem? |
It works like this: modal.open(ModalUpgrade, {
title: "Upgrade your account",
description:
"You need to upgrade your account to create more workspaces.",
async onUpgrade() {
await modal.reset();
modal.open(ModalSubscription);
},
}); Note: |
π Linked issue
Resolves #285
β Type of change
π Description
Following the discussion in #285, we'd like to display a modal programmatically.
useModal
composable to control the displaying of a modal from a componentπ Checklist