In this episode, we tackle a common annoyance when working with modals that use their own route. You might have noticed that when you close a modal (like a login dialog), the URL often stays the same, even after the modal disappears. This means if a user refreshes the page after closing the modal, it pops right back up – not what we want!
So, we walk through how to fix this so that once the modal has finished closing (including any nice fade-out transitions), the user is automatically redirected back to the page they started from. To do this, we use the afterLeave
event provided by Headless UI’s transition component. This way, the redirect only happens after the modal has fully transitioned out, which makes everything look smooth and intentional.
Finally, we add a simple redirect function, check out the improved behavior, and make sure that refreshing the page after closing the modal doesn’t bring it back. The result? A much nicer user experience with just a tiny bit of extra code!