lepikhinb / momentum-modal

MIT License
442 stars 26 forks source link

Possible to pass a URL to redirect function? #53

Open mansoorkhan96 opened 1 year ago

mansoorkhan96 commented 1 year ago

I have a situation where i redirect users conditionally. Would love know if there is a possibility to optionally pass a URL to the redirect() function of useModal() so that it redirects to intended page instead of baseRoute.

Here is what i am trying as a word around.

const props = defineProps({ redirectUrl: String })

const { show, close, redirect } = useModal()

const handleRedirect = () => {
    if (props.redirectUrl) {
        router.get(props.redirectUrl)
    } else {
        redirect()
    }
}
<TransitionChild @after-leave="handleRedirect">
...
</TransitionChild>
matthewknill commented 1 year ago

I think the above should work okay, however, I've found that I need to set the vnode to false otherwise it does some weird stuff where the scroll doesn't work (found by having a look at what the redirect() function does).

Try the following:

const props = defineProps({ redirectUrl: String })

const { show, close, redirect, vnode } = useModal()

const handleRedirect = () => {
    if (props.redirectUrl) {
        vnode.value = false
        router.get(props.redirectUrl)
    } else {
        redirect()
    }
}

I agree that a url parameter for the redirect function would be good though.