What is the modal pattern with intercepting routes in Next.js?
It is a way to show a modal dialog by intercepting navigation routes, so the modal appears over the current page without a full page reload.
Click to reveal answer
intermediate
How does Next.js detect when to show a modal using intercepting routes?
Next.js compares the current route with the previous route and shows the modal if the new route is a child or overlay route, keeping the background page visible.
Click to reveal answer
intermediate
Why use intercepting routes for modals instead of traditional modal state?
Interception keeps the URL in sync with the UI, allows browser navigation (back/forward), and supports deep linking directly to modal content.
Click to reveal answer
beginner
What React hook is commonly used to track route changes for modal interception in Next.js?
The usePathname hook from 'next/navigation' is used to detect route changes and decide when to show or hide the modal.
Click to reveal answer
advanced
What accessibility considerations should you keep in mind when implementing modals with intercepting routes?
Ensure focus is trapped inside the modal, provide ARIA labels, allow keyboard navigation, and restore focus to the background page when modal closes.
Click to reveal answer
What does the modal pattern with intercepting routes primarily help with in Next.js?
AShowing modals without full page reloads
BImproving server-side rendering speed
CAutomatically generating routes
DStyling modals with CSS
✗ Incorrect
The pattern allows modals to appear over the current page by intercepting route changes, avoiding full page reloads.
Which Next.js hook helps detect route changes for modal interception?
AusePathname
BuseState
CuseEffect
DuseContext
✗ Incorrect
usePathname provides access to the current pathname needed to detect navigation changes.
What is a key benefit of using intercepting routes for modals?
ADisables page scrolling permanently
BAutomatically styles modals
CPrevents modals from closing
DSupports browser back and forward buttons
✗ Incorrect
Intercepting routes keep URL in sync, so browser navigation works naturally with modals.
When a modal is open via intercepting routes, what remains visible?
AOnly the modal content
BThe background page behind the modal
CA blank white screen
DThe browser console
✗ Incorrect
The background page stays visible behind the modal to maintain context.
Which accessibility feature is important for modals with intercepting routes?
ADisabling keyboard navigation
BRemoving ARIA labels
CFocus trapping inside the modal
DHiding modal from screen readers
✗ Incorrect
Focus trapping ensures keyboard users stay inside the modal until it closes.
Explain how the modal pattern with intercepting routes works in Next.js and why it improves user experience.
Think about how the URL and page content stay in sync when opening a modal.
You got /5 concepts.
Describe key accessibility practices to follow when implementing modals with intercepting routes in Next.js.
Consider how users with keyboards or screen readers interact with modals.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using the modal pattern with intercepting routes in Next.js?
easy
A. It automatically reloads the entire page when opening a modal.
B. It allows showing a popup without leaving the current page.
C. It disables keyboard navigation while the modal is open.
D. It prevents any URL changes when interacting with the modal.
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 B
Quick Check:
Modal pattern = popup without page change [OK]
Hint: Modals with intercepting routes keep you on the same page [OK]
Common Mistakes:
Thinking the page reloads when modal opens
Assuming URL never changes
Believing keyboard navigation is disabled
2. Which Next.js feature is commonly used to control modal visibility with intercepting routes?
easy
A. Using URL query parameters to toggle modal state
B. Using React class components with state
C. Using server-side rendering to open modals
D. Using CSS hover effects to show modals
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 A
Quick Check:
Modal visibility = URL query param [OK]
Hint: Modal open state is often in URL query parameters [OK]
Common Mistakes:
Confusing client state with URL state
Using class components instead of hooks
Relying on CSS hover for modal control
3. Given this Next.js snippet controlling a modal with query param ?modal=open:
What is a likely bug in this code regarding accessibility?
medium
A. The URL does not update when the modal opens.
B. The modal never closes because router.push is incorrect.
C. Keyboard focus is not moved to the modal when it opens.
D. The modal content is rendered outside the React tree.
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 C
Quick Check:
Modal accessibility = move keyboard focus [OK]
Hint: Always move keyboard focus into modal on open [OK]
Common Mistakes:
Ignoring keyboard focus management
Assuming URL push closes modal automatically
Confusing rendering with accessibility
5. You want to implement a modal in Next.js that opens on /gallery?modal=photo123 and closes by removing the query param, without losing the gallery scroll position. Which approach best achieves this?
hard
A. Use router.push with shallow: true to update URL without full reload and keep scroll.
B. Use router.replace without shallow to reload page and reset scroll.
C. Use window.location.href to change URL and force reload.
D. Use CSS to hide modal and ignore URL changes.
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
Using router.push with shallow: true changes 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 A
Quick Check:
Shallow routing = URL change without reload [OK]
Hint: Use shallow routing to keep scroll when changing URL [OK]
Common Mistakes:
Using router.replace without shallow reloads page
Using window.location.href causes full reload
Ignoring URL for modal state control
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.