I’ve tried using Parallel Routes to create a modal but am a bit confused on how to ignore the page from getting a new modal from the slot.
In this case, the color page has a lot of color harmonies that have links to other color pages and should not open a modal.
1. Homepage
Color palettes based on hue.
2. Color Red Modal
Every time a user clicks on another link color, it changes the content inside the modal.
3. Color Red Page
On this page the modal or slot should be ignored.
4. Color Rose
It should open a color page without opening a modal.
5. Color Rose Modal
How does the slot not know that the page it is in is the current page?
6. Color Rose Image (API Response)
Attempt to open another page to clear the previous state.
7. Color Rose Page
The previous state is lost because <Link />
has the replace=true
prop, otherwise it would still be there but without the modal.
Closing the Modal
Yes, we can close the modal by calling router.back()
, what if we can close them using router.push("/")
then redirect to homepage and close the modal.
Why using replace=true
?
In the image above you can see the arrow_back button to return to the previous state, and the hidden button as an overlay to close the modal. If I don’t replace the page, the hidden button don’t work like the arrow_back button. both call the same function.
const router = useRouter();
const handleClose = () => {
router.back();
};
Using router.push()
as alternative to close modal
This solution it will help edge cases that we don’t know, such as using <Link />
with replace=true
and don’t have previous state. See above image with number (3-4 and 5).
const router = useRouter();
const handleClose = () => {
if(typeof window !== "undefined"){
if(window.history.length <= 1){
router.push('/');
} else {
router.back();
}
}
};
Add prop modal=true
as optional to open modal
This solution should prevent opening modal within page for the same page. See above image with number (4 and 5).
<Link href="/color/ff0080" modal={isModal ? true : false} {...} />
By adding modal=false
prop the modal should be ignored and <Link />
should behave as usual.
Sorry to the team reading this messy feedback, but I can’t wait for this <Link modal={isModal ? true : false} />
. Thank You!