Since Next.js doesn’t support exit animations yet (Relevant issue), we need to manually create the animation event.
For this example I’m using the Sheet component from shadcn/ui, but you can use the Dialog from Radix directly with your own style.
Initial modal component
This is our initial component using shadcn/ui Drawer component, based on the Next.js docs.
Where we just call the router.back() to close the modal.
Adding a closing animation
To achieve this, we need to control the open state to perform the router.back() after the closing animation ends, using onAnimationEndCapture.
Result
Now when pressing the close button or the ESC key, the modal will exit with an animation then go back to the previous URL.
We still can’t animate when the React component is unmounted (eg: when using the browser’s back button), for that we’ll have to wait for native framework support.
For now this cover the rest of usecases.