How would I use to display a Modal component? #927
Replies: 1 comment
|
TL;DR: if you want to use the Back button to dismiss the modal, you can use Simply calling <CloseButton onClick={() => {
if (history.length === 1) {
// We landed on this page directly with the modal open, eg clicking a link
setModalOpen(false, { history: 'replace' })
} else {
router.back()
}
}} />Longer versionThat is a good question, and a classic case where using the Let's start by the various states you can get your app into, assuming the modal is on page
If the user were to press Back for each of these cases, they should land:
Now if the user were to close the modal instead (using a X button for example), they should land:
Using the Back button at this stage should not re-open the modal, so this is a replace operation: <CloseButton onClick={() => setModalOpen(false, { history: 'replace' })} />But now we have an issue: if opening the modal is a push operation, then closing it with the X then doing Back breaks navigation: you'd have two So we need to have a consistent behaviour, and use the back navigation on the X button too. But only if we have a place to go to (ie: not a New Tab page). |
Uh oh!
There was an error while loading. Please reload this page.
I want to control a modal open state by a query param. But when the user goes back the navigation, I want to make a router back instead of a push and let the user go back in the history to dismiss the modal.
All reactions