Discover how to make modals feel like part of your page's navigation without messy code!
Why Modal pattern with intercepting routes in NextJS? - Purpose & Use Cases
Imagine you want to show a popup window (modal) on top of your current page when a user clicks a link, but still keep the original page visible behind it.
You try to do this by manually changing the URL and controlling the popup yourself.
Manually managing modals with URL changes is tricky and error-prone.
You have to track the previous page, handle back button behavior, and sync UI state with the URL yourself.
This often leads to bugs where the modal doesn't close properly or the page reloads unexpectedly.
The modal pattern with intercepting routes in Next.js lets you show modals linked to URLs without leaving the current page.
It intercepts route changes and renders the modal on top, keeping the background page intact and handling navigation smoothly.
router.push('/page'); showModal(true); // manual state and URL sync
useInterceptingRoutes(); // modal tied to route, automatic handling
This pattern enables seamless modal dialogs that integrate naturally with browser navigation and URLs, improving user experience and app flow.
On an e-commerce site, clicking a product image opens a modal with details without leaving the product list page, and the URL updates so users can share or bookmark the modal view.
Manual modal and URL syncing is complex and fragile.
Intercepting routes lets Next.js handle modals tied to URLs smoothly.
Users get better navigation and shareable modal states.
Practice
Solution
Step 1: Understand modal with intercepting routes
This pattern lets you open a modal that overlays the current page content without navigating away.Step 2: Identify the benefit
Because the modal is shown on top, the user stays on the same page, improving user experience.Final Answer:
It allows showing a popup without leaving the current page. -> Option BQuick Check:
Modal pattern = popup without page change [OK]
- Thinking the page reloads when modal opens
- Assuming URL never changes
- Believing keyboard navigation is disabled
Solution
Step 1: Identify modal control method
Next.js modals with intercepting routes often use URL query parameters to track if modal is open or closed.Step 2: Eliminate incorrect options
React class components are outdated; server-side rendering doesn't control modal visibility dynamically; CSS hover is unrelated to routing.Final Answer:
Using URL query parameters to toggle modal state -> Option AQuick Check:
Modal visibility = URL query param [OK]
- Confusing client state with URL state
- Using class components instead of hooks
- Relying on CSS hover for modal control
?modal=open:
const router = useRouter();
const isModalOpen = router.query.modal === 'open';
return (
<>
{isModalOpen && <ModalComponent />}
<MainPageContent />
</>
);
What happens when the URL changes from /page to /page?modal=open?Solution
Step 1: Understand the code logic
The code checks if the query parameter 'modal' equals 'open'. If yes, it renders the modal component alongside the main content.Step 2: Analyze URL change effect
Changing URL to include '?modal=open' setsisModalOpentrue, so modal appears over the main page without replacing it.Final Answer:
The modal component appears over the main page content. -> Option DQuick Check:
Query param 'modal=open' = modal visible [OK]
- Thinking modal replaces main content
- Ignoring query parameters in rendering
- Assuming page reloads on query change
const router = useRouter();
const isModalOpen = router.query.modal === 'open';
function closeModal() {
router.push('/page');
}
return (
<>
{isModalOpen && <Modal onClose={closeModal} />}
<PageContent />
</>
);
What is a likely bug in this code regarding accessibility?Solution
Step 1: Review modal accessibility needs
When a modal opens, keyboard focus should move inside it for screen reader and keyboard users.Step 2: Check code for focus management
The code changes URL and renders modal but does not handle moving keyboard focus to modal.Final Answer:
Keyboard focus is not moved to the modal when it opens. -> Option CQuick Check:
Modal accessibility = move keyboard focus [OK]
- Ignoring keyboard focus management
- Assuming URL push closes modal automatically
- Confusing rendering with accessibility
/gallery?modal=photo123 and closes by removing the query param, without losing the gallery scroll position. Which approach best achieves this?Solution
Step 1: Understand shallow routing in Next.js
Shallow routing updates the URL without running data fetching methods or reloading the page, preserving scroll and state.Step 2: Apply shallow routing to modal open/close
Usingrouter.pushwithshallow: truechanges query params to open/close modal without losing scroll position.Final Answer:
Use router.push with shallow: true to update URL without full reload and keep scroll. -> Option AQuick Check:
Shallow routing = URL change without reload [OK]
- Using router.replace without shallow reloads page
- Using window.location.href causes full reload
- Ignoring URL for modal state control
Ready to prove it?
Pass the test and this pattern moves unknown
